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