Debian Codegraph @

vendor/wikimedia/css-sanitizer/src/Grammar/Alternative.php

raw
1 <?php
2 /**
3 * @file
4 * @license https://opensource.org/licenses/Apache-2.0 Apache-2.0
5 */
6
7 namespace Wikimedia\CSS\Grammar;
8
9 use Wikimedia\CSS\Objects\ComponentValueList;
10 use Wikimedia\CSS\Util;
11
12 /**
13 * Matcher that matches one out of a set of Matchers ("|" combiner).
14 * @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#comb-one
15 */
16 class Alternative extends Matcher {
17 /** @var Matcher[] */
18 protected $matchers;
19
20 /**
21 * @param Matcher[] $matchers
22 */
23 public function __construct( array $matchers ) {
24 Util::assertAllInstanceOf( $matchers, Matcher::class, '$matchers' );
25 $this->matchers = $matchers;
26 }
27
28 /** @inheritDoc */
29 protected function generateMatches( ComponentValueList $values, $start, array $options ) {
30 $used = [];
31 foreach ( $this->matchers as $matcher ) {
32 foreach ( $matcher->generateMatches( $values, $start, $options ) as $match ) {
33 $newMatch = $this->makeMatch( $values, $start, $match->getNext(), $match );
34 $mid = $newMatch->getUniqueID();
35 if ( !isset( $used[$mid] ) ) {
36 $used[$mid] = 1;
37 yield $newMatch;
38 }
39 }
40 }
41 }
42 }