/
NormalizedVariableCollection.php
56 lines (49 loc) · 1.88 KB
/
NormalizedVariableCollection.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
<?php
namespace Concrete\Core\StyleCustomizer\Normalizer;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Concrete\Core\StyleCustomizer\Normalizer\Legacy\ImageVariable as LegacyImageVariable;
/**
* A way to normalize SCSS/LESS variables that can be injected into different compilers, stored in the database
* etc...
*/
class NormalizedVariableCollection extends ArrayCollection implements \JsonSerializable, DenormalizableInterface
{
/**
* @param VariableInterface $element
* @return bool|true
*/
public function add($element)
{
return parent::add($element);
}
public function getVariable(string $name): ?VariableInterface
{
foreach ($this->toArray() as $variable) {
if ($variable->getName() == $name) {
return $variable;
}
}
return null;
}
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return $this->toArray();
}
public function denormalize(DenormalizerInterface $denormalizer, $data, $format = null, array $context = [])
{
foreach ($data as $value) {
if (isset($value['type']) && $value['type'] == 'number') {
$this->add(new NumberVariable($value['name'], $value['number'], $value['unit']));
} else if (isset($value['type']) && $value['type'] == 'image') {
$this->add(new ImageVariable($value['name'], $value['url'], $value['fID']));
} else if (isset($value['type']) && $value['type'] == 'legacy-image') {
$this->add(new LegacyImageVariable($value['name'], $value['url'], $value['fID']));
} else {
$this->add(new Variable($value['name'], $value['value']));
}
}
}
}