CSSFunction.php
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Vendor\Sabberworm\CSS\Value; |
| 4 | |
| 5 | use Automattic\WooCommerce\Vendor\Sabberworm\CSS\OutputFormat; |
| 6 | use Automattic\WooCommerce\Vendor\Sabberworm\CSS\Parsing\ParserState; |
| 7 | use Automattic\WooCommerce\Vendor\Sabberworm\CSS\Parsing\SourceException; |
| 8 | use Automattic\WooCommerce\Vendor\Sabberworm\CSS\Parsing\UnexpectedEOFException; |
| 9 | use Automattic\WooCommerce\Vendor\Sabberworm\CSS\Parsing\UnexpectedTokenException; |
| 10 | |
| 11 | /** |
| 12 | * A `CSSFunction` represents a special kind of value that also contains a function name and where the values are the |
| 13 | * function’s arguments. It also handles equals-sign-separated argument lists like `filter: alpha(opacity=90);`. |
| 14 | */ |
| 15 | class CSSFunction extends ValueList |
| 16 | { |
| 17 | /** |
| 18 | * @var string |
| 19 | * |
| 20 | * @internal since 8.8.0 |
| 21 | */ |
| 22 | protected $sName; |
| 23 | |
| 24 | /** |
| 25 | * @param string $sName |
| 26 | * @param RuleValueList|array<int, RuleValueList|CSSFunction|CSSString|LineName|Size|URL|string> $aArguments |
| 27 | * @param string $sSeparator |
| 28 | * @param int $iLineNo |
| 29 | */ |
| 30 | public function __construct($sName, $aArguments, $sSeparator = ',', $iLineNo = 0) |
| 31 | { |
| 32 | if ($aArguments instanceof RuleValueList) { |
| 33 | $sSeparator = $aArguments->getListSeparator(); |
| 34 | $aArguments = $aArguments->getListComponents(); |
| 35 | } |
| 36 | $this->sName = $sName; |
| 37 | $this->setPosition($iLineNo); // TODO: redundant? |
| 38 | parent::__construct($aArguments, $sSeparator, $iLineNo); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @param ParserState $oParserState |
| 43 | * @param bool $bIgnoreCase |
| 44 | * |
| 45 | * @return CSSFunction |
| 46 | * |
| 47 | * @throws SourceException |
| 48 | * @throws UnexpectedEOFException |
| 49 | * @throws UnexpectedTokenException |
| 50 | * |
| 51 | * @internal since V8.8.0 |
| 52 | */ |
| 53 | public static function parse(ParserState $oParserState, $bIgnoreCase = false) |
| 54 | { |
| 55 | $mResult = $oParserState->parseIdentifier($bIgnoreCase); |
| 56 | $oParserState->consume('('); |
| 57 | $aArguments = Value::parseValue($oParserState, ['=', ' ', ',']); |
| 58 | $mResult = new CSSFunction($mResult, $aArguments, ',', $oParserState->currentLine()); |
| 59 | $oParserState->consume(')'); |
| 60 | return $mResult; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @return string |
| 65 | */ |
| 66 | public function getName() |
| 67 | { |
| 68 | return $this->sName; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param string $sName |
| 73 | * |
| 74 | * @return void |
| 75 | */ |
| 76 | public function setName($sName) |
| 77 | { |
| 78 | $this->sName = $sName; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @return array<int, RuleValueList|CSSFunction|CSSString|LineName|Size|URL|string> |
| 83 | */ |
| 84 | public function getArguments() |
| 85 | { |
| 86 | return $this->aComponents; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @return string |
| 91 | * |
| 92 | * @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead. |
| 93 | */ |
| 94 | public function __toString() |
| 95 | { |
| 96 | return $this->render(new OutputFormat()); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @param OutputFormat|null $oOutputFormat |
| 101 | * |
| 102 | * @return string |
| 103 | */ |
| 104 | public function render($oOutputFormat) |
| 105 | { |
| 106 | $aArguments = parent::render($oOutputFormat); |
| 107 | return "{$this->sName}({$aArguments})"; |
| 108 | } |
| 109 | } |
| 110 |