Debian Codegraph @

vendor/sabberworm/php-css-parser/src/Value/ValueList.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Sabberworm\CSS\Value;
6
7 use Sabberworm\CSS\OutputFormat;
8 use Sabberworm\CSS\ShortClassNameProvider;
9
10 /**
11 * A `ValueList` represents a lists of `Value`s, separated by some separation character
12 * (mostly `,`, whitespace, or `/`).
13 *
14 * There are two types of `ValueList`s: `RuleValueList` and `CSSFunction`
15 */
16 abstract class ValueList extends Value
17 {
18 use ShortClassNameProvider;
19
20 /**
21 * @var array<Value|string>
22 *
23 * @internal since 8.8.0
24 */
25 protected $components;
26
27 /**
28 * @var non-empty-string
29 *
30 * @internal since 8.8.0
31 */
32 protected $separator;
33
34 /**
35 * @param array<Value|string>|Value|string $components
36 * @param non-empty-string $separator
37 * @param int<1, max>|null $lineNumber
38 */
39 public function __construct($components = [], $separator = ',', ?int $lineNumber = null)
40 {
41 parent::__construct($lineNumber);
42 if (!\is_array($components)) {
43 $components = [$components];
44 }
45 $this->components = $components;
46 $this->separator = $separator;
47 }
48
49 /**
50 * @param Value|string $component
51 */
52 public function addListComponent($component): void
53 {
54 $this->components[] = $component;
55 }
56
57 /**
58 * @return array<Value|string>
59 */
60 public function getListComponents(): array
61 {
62 return $this->components;
63 }
64
65 /**
66 * @param array<Value|string> $components
67 */
68 public function setListComponents(array $components): void
69 {
70 $this->components = $components;
71 }
72
73 /**
74 * @return non-empty-string
75 */
76 public function getListSeparator(): string
77 {
78 return $this->separator;
79 }
80
81 /**
82 * @param non-empty-string $separator
83 */
84 public function setListSeparator(string $separator): void
85 {
86 $this->separator = $separator;
87 }
88
89 public function render(OutputFormat $outputFormat): string
90 {
91 $formatter = $outputFormat->getFormatter();
92
93 return $formatter->implode(
94 $formatter->spaceBeforeListArgumentSeparator($this->separator) . $this->separator
95 . $formatter->spaceAfterListArgumentSeparator($this->separator),
96 $this->components
97 );
98 }
99
100 /**
101 * @return array<string, bool|int|float|string|array<mixed>|null>
102 *
103 * @internal
104 */
105 public function getArrayRepresentation(): array
106 {
107 return [
108 'class' => $this->getShortClassName(),
109 'components' => \array_map(
110 /**
111 * @parm Value|string $component
112 */
113 function ($component): array {
114 if (\is_string($component)) {
115 return ['class' => 'string', 'value' => $component];
116 }
117 return $component->getArrayRepresentation();
118 },
119 $this->components
120 ),
121 'separator' => $this->separator,
122 ];
123 }
124 }