/
TypeStyle.php
94 lines (82 loc) · 3.53 KB
/
TypeStyle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace Concrete\Core\StyleCustomizer\Style;
use Concrete\Core\StyleCustomizer\Normalizer\NormalizedVariableCollection;
use Concrete\Core\StyleCustomizer\Normalizer\VariableInterface;
use Concrete\Core\StyleCustomizer\Style\Value\TypeValue;
use Concrete\Core\StyleCustomizer\Style\Value\ValueInterface;
use Concrete\Core\StyleCustomizer\WebFont\WebFontCollection;
class TypeStyle extends Style
{
const PARSE_SUBJECT_VARIABLE_COLLECTION = 'collection';
const PARSE_SUBJECT_REQUEST = 'request';
use WebFontCollectionStyleTrait;
private function getStyleTypes(): array
{
return [
[ColorStyle::class, 'color', t('Color')],
[FontFamilyStyle::class, 'font-family', t('Font Family')],
[SizeStyle::class, 'font-size', t('Font Size')],
[FontWeightStyle::class, 'font-weight', t('Font Weight')],
[FontStyleStyle::class, 'font-style', t('Font Style')],
[TextDecorationStyle::class, 'text-decoration', t('Text Decoration')],
[TextTransformStyle::class, 'text-transform', t('Text Transform')],
];
}
protected function parseStyleValueForVariable(string $variableName, string $parseType, $subject): TypeValue
{
$typeValue = new TypeValue();
foreach ($this->getStyleTypes() as $styleType) {
$style = app($styleType[0]);
$style->setVariable($variableName . '-' . $styleType[1]);
$style->setName($this->getName() . ' ' . $styleType[2]);
if ($style instanceof FontFamilyStyle) {
$style->setWebFonts($this->getWebFonts());
}
if ($parseType === self::PARSE_SUBJECT_VARIABLE_COLLECTION) {
$value = $style->createValueFromVariableCollection($subject);
} else if ($parseType === self::PARSE_SUBJECT_REQUEST) {
$value = $style->createValueFromRequestDataCollection($subject);
}
if ($value) {
$styleValue = new StyleValue($style, $value);
$typeValue->addSubStyleValue($styleValue);
}
}
return $typeValue;
}
public function createValueFromVariableCollection(NormalizedVariableCollection $collection): ?ValueInterface
{
$typeValue = $this->parseStyleValueForVariable($this->getVariableToInspect(), self::PARSE_SUBJECT_VARIABLE_COLLECTION, $collection);
if ($typeValue->hasSubStyleValues()) {
return $typeValue;
}
return null;
}
public function createValueFromRequestDataCollection(array $styles): ?ValueInterface
{
$typeValue = $this->parseStyleValueForVariable($this->getVariable(), self::PARSE_SUBJECT_REQUEST, $styles);
if (!$typeValue->hasSubStyleValues()) {
// Legacy backward compatibility hack. See above
$typeValue = $this->parseStyleValueForVariable($this->getVariable() . '-type', self:: PARSE_SUBJECT_REQUEST, $styles);
}
if ($typeValue->hasSubStyleValues()) {
return $typeValue;
}
return null;
}
/**
* @param TypeValue $value
* @return VariableInterface|null
*/
public function createVariableFromValue(ValueInterface $value): ?VariableInterface
{
throw new \RuntimeException(t('The TypeStyle class is a container style – it cannot convert to variables.'));
}
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
$data = parent::jsonSerialize();
$data['fonts'] = $this->getWebFonts();
return $data;
}
}