Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
DelimMatcher
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 generateMatches
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2declare( strict_types = 1 );
3
4/**
5 * @file
6 * @license https://opensource.org/licenses/Apache-2.0 Apache-2.0
7 */
8
9namespace Wikimedia\CSS\Grammar;
10
11use Wikimedia\CSS\Objects\ComponentValueList;
12use Wikimedia\CSS\Objects\Token;
13
14/**
15 * Matcher that matches one of a set of values.
16 *
17 * This is intended for matching specific <delim-token>s, but will work for
18 * other types (case-sensitively) too. For the more common case-insensitive
19 * identifier matching, use KeywordMatcher.
20 *
21 * @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#component-types
22 */
23class DelimMatcher extends Matcher {
24    /** @var string One of the Token::T_* constants */
25    protected $type;
26
27    /** @var string[] Values to match */
28    protected $values;
29
30    /**
31     * @param string|string[] $values Token values to match
32     * @param array $options Options
33     *  - type: (string) Token type to match. Default is Token::T_DELIM.
34     */
35    public function __construct( $values, array $options = [] ) {
36        $options += [
37            'type' => Token::T_DELIM,
38        ];
39
40        $this->values = array_map( 'strval', (array)$values );
41        $this->type = $options['type'];
42    }
43
44    /** @inheritDoc */
45    protected function generateMatches( ComponentValueList $values, $start, array $options ) {
46        $cv = $values[$start] ?? null;
47        if ( $cv instanceof Token && $cv->type() === $this->type &&
48            in_array( $cv->value(), $this->values, true )
49        ) {
50            yield $this->makeMatch( $values, $start, $this->next( $values, $start, $options ) );
51        }
52    }
53}