← All changes
|
back/Libraries/Sabberworm/CSS/src/Value/CSSFunction.php
+75
-32
10.2.2
→
11.1.0
View file →
| @@ -1,73 +1,116 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -namespace Sabberworm\CSS\Value; | |
| 3 | +declare(strict_types=1); | |
| 4 | 4 | |
| 5 | -use Sabberworm\CSS\OutputFormat; | |
| 5 | +namespace AcyMailing\Libraries\Sabberworm\CSS\Value; | |
| 6 | 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 | + */ | |
| 7 | 17 | class CSSFunction extends ValueList |
| 8 | 18 | { |
| 9 | 19 | /** |
| 10 | - * @var string | |
| 20 | + * @var non-empty-string | |
| 21 | + * | |
| 22 | + * @internal since 8.8.0 | |
| 11 | 23 | */ |
| 12 | - protected $sName; | |
| 24 | + protected $name; | |
| 13 | 25 | |
| 14 | 26 | /** |
| 15 | - * @param string $sName | |
| 16 | - * @param RuleValueList|array<int, RuleValueList|CSSFunction|CSSString|LineName|Size|URL|string> $aArguments | |
| 17 | - * @param string $sSeparator | |
| 18 | - * @param int $iLineNo | |
| 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 | |
| 19 | 31 | */ |
| 20 | - public function __construct($sName, $aArguments, $sSeparator = ',', $iLineNo = 0) | |
| 32 | + public function __construct(string $name, $arguments, string $separator = ',', ?int $lineNumber = null) | |
| 21 | 33 | { |
| 22 | - if ($aArguments instanceof RuleValueList) { | |
| 23 | - $sSeparator = $aArguments->getListSeparator(); | |
| 24 | - $aArguments = $aArguments->getListComponents(); | |
| 34 | + if ($arguments instanceof RuleValueList) { | |
| 35 | + $separator = $arguments->getListSeparator(); | |
| 36 | + $arguments = $arguments->getListComponents(); | |
| 25 | 37 | } |
| 26 | - $this->sName = $sName; | |
| 27 | - $this->iLineNo = $iLineNo; | |
| 28 | - parent::__construct($aArguments, $sSeparator, $iLineNo); | |
| 38 | + $this->name = $name; | |
| 39 | + $this->setPosition($lineNumber); // TODO: redundant? | |
| 40 | + parent::__construct($arguments, $separator, $lineNumber); | |
| 29 | 41 | } |
| 30 | 42 | |
| 31 | 43 | /** |
| 32 | - * @return string | |
| 44 | + * @throws SourceException | |
| 45 | + * @throws UnexpectedEOFException | |
| 46 | + * @throws UnexpectedTokenException | |
| 47 | + * | |
| 48 | + * @internal since V8.8.0 | |
| 33 | 49 | */ |
| 34 | - public function getName() | |
| 50 | + public static function parse(ParserState $parserState, bool $ignoreCase = false): CSSFunction | |
| 35 | 51 | { |
| 36 | - return $this->sName; | |
| 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; | |
| 37 | 60 | } |
| 38 | 61 | |
| 39 | 62 | /** |
| 40 | - * @param string $sName | |
| 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 | |
| 41 | 74 | * |
| 42 | - * @return void | |
| 75 | + * @throws SourceException | |
| 76 | + * @throws UnexpectedEOFException | |
| 77 | + * @throws UnexpectedTokenException | |
| 43 | 78 | */ |
| 44 | - public function setName($sName) | |
| 79 | + private static function parseArguments(ParserState $parserState) | |
| 45 | 80 | { |
| 46 | - $this->sName = $sName; | |
| 81 | + return Value::parseValue($parserState, ['=', ' ', ',']); | |
| 47 | 82 | } |
| 48 | 83 | |
| 49 | 84 | /** |
| 50 | - * @return array<int, RuleValueList|CSSFunction|CSSString|LineName|Size|URL|string> | |
| 85 | + * @return non-empty-string | |
| 51 | 86 | */ |
| 52 | - public function getArguments() | |
| 87 | + public function getName(): string | |
| 53 | 88 | { |
| 54 | - return $this->aComponents; | |
| 89 | + return $this->name; | |
| 55 | 90 | } |
| 56 | 91 | |
| 57 | 92 | /** |
| 58 | - * @return string | |
| 93 | + * @param non-empty-string $name | |
| 59 | 94 | */ |
| 60 | - public function __toString() | |
| 95 | + public function setName(string $name): void | |
| 61 | 96 | { |
| 62 | - return $this->render(new OutputFormat()); | |
| 97 | + $this->name = $name; | |
| 63 | 98 | } |
| 64 | 99 | |
| 65 | 100 | /** |
| 66 | - * @return string | |
| 101 | + * @return array<Value|string> | |
| 67 | 102 | */ |
| 68 | - public function render(OutputFormat $oOutputFormat) | |
| 103 | + public function getArguments(): array | |
| 69 | 104 | { |
| 70 | - $aArguments = parent::render($oOutputFormat); | |
| 71 | - return "{$this->sName}({$aArguments})"; | |
| 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})"; | |
| 72 | 115 | } |
| 73 | 116 | } |