← All changes
|
back/Libraries/Sabberworm/CSS/src/Value/CSSFunction.php
+116
-0
10.0.0
→
11.1.0
View file →
| @@ -1,0 +1,116 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace AcyMailing\Libraries\Sabberworm\CSS\Value; | |
| 6 | + | |
| 7 | +use AcyMailing\Libraries\Sabberworm\CSS\OutputFormat; | |
| 8 | +use AcyMailing\Libraries\Sabberworm\CSS\Parsing\ParserState; | |
| 9 | +use AcyMailing\Libraries\Sabberworm\CSS\Parsing\SourceException; | |
| 10 | +use AcyMailing\Libraries\Sabberworm\CSS\Parsing\UnexpectedEOFException; | |
| 11 | +use AcyMailing\Libraries\Sabberworm\CSS\Parsing\UnexpectedTokenException; | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * A `CSSFunction` represents a special kind of value that also contains a function name and where the values are the | |
| 15 | + * function’s arguments. It also handles equals-sign-separated argument lists like `filter: alpha(opacity=90);`. | |
| 16 | + */ | |
| 17 | +class CSSFunction extends ValueList | |
| 18 | +{ | |
| 19 | + /** | |
| 20 | + * @var non-empty-string | |
| 21 | + * | |
| 22 | + * @internal since 8.8.0 | |
| 23 | + */ | |
| 24 | + protected $name; | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * @param non-empty-string $name | |
| 28 | + * @param RuleValueList|array<Value|string> $arguments | |
| 29 | + * @param non-empty-string $separator | |
| 30 | + * @param int<1, max>|null $lineNumber | |
| 31 | + */ | |
| 32 | + public function __construct(string $name, $arguments, string $separator = ',', ?int $lineNumber = null) | |
| 33 | + { | |
| 34 | + if ($arguments instanceof RuleValueList) { | |
| 35 | + $separator = $arguments->getListSeparator(); | |
| 36 | + $arguments = $arguments->getListComponents(); | |
| 37 | + } | |
| 38 | + $this->name = $name; | |
| 39 | + $this->setPosition($lineNumber); // TODO: redundant? | |
| 40 | + parent::__construct($arguments, $separator, $lineNumber); | |
| 41 | + } | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * @throws SourceException | |
| 45 | + * @throws UnexpectedEOFException | |
| 46 | + * @throws UnexpectedTokenException | |
| 47 | + * | |
| 48 | + * @internal since V8.8.0 | |
| 49 | + */ | |
| 50 | + public static function parse(ParserState $parserState, bool $ignoreCase = false): CSSFunction | |
| 51 | + { | |
| 52 | + $name = self::parseName($parserState, $ignoreCase); | |
| 53 | + $parserState->consume('('); | |
| 54 | + $arguments = self::parseArguments($parserState); | |
| 55 | + | |
| 56 | + $result = new CSSFunction($name, $arguments, ',', $parserState->currentLine()); | |
| 57 | + $parserState->consume(')'); | |
| 58 | + | |
| 59 | + return $result; | |
| 60 | + } | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * @throws SourceException | |
| 64 | + * @throws UnexpectedEOFException | |
| 65 | + * @throws UnexpectedTokenException | |
| 66 | + */ | |
| 67 | + private static function parseName(ParserState $parserState, bool $ignoreCase = false): string | |
| 68 | + { | |
| 69 | + return $parserState->parseIdentifier($ignoreCase); | |
| 70 | + } | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * @return Value|string | |
| 74 | + * | |
| 75 | + * @throws SourceException | |
| 76 | + * @throws UnexpectedEOFException | |
| 77 | + * @throws UnexpectedTokenException | |
| 78 | + */ | |
| 79 | + private static function parseArguments(ParserState $parserState) | |
| 80 | + { | |
| 81 | + return Value::parseValue($parserState, ['=', ' ', ',']); | |
| 82 | + } | |
| 83 | + | |
| 84 | + /** | |
| 85 | + * @return non-empty-string | |
| 86 | + */ | |
| 87 | + public function getName(): string | |
| 88 | + { | |
| 89 | + return $this->name; | |
| 90 | + } | |
| 91 | + | |
| 92 | + /** | |
| 93 | + * @param non-empty-string $name | |
| 94 | + */ | |
| 95 | + public function setName(string $name): void | |
| 96 | + { | |
| 97 | + $this->name = $name; | |
| 98 | + } | |
| 99 | + | |
| 100 | + /** | |
| 101 | + * @return array<Value|string> | |
| 102 | + */ | |
| 103 | + public function getArguments(): array | |
| 104 | + { | |
| 105 | + return $this->components; | |
| 106 | + } | |
| 107 | + | |
| 108 | + /** | |
| 109 | + * @return non-empty-string | |
| 110 | + */ | |
| 111 | + public function render(OutputFormat $outputFormat): string | |
| 112 | + { | |
| 113 | + $arguments = parent::render($outputFormat); | |
| 114 | + return "{$this->name}({$arguments})"; | |
| 115 | + } | |
| 116 | +} | |