Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
TokenMatcher
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
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
5
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 a token of a particular type
16 * @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#component-types
17 */
18class TokenMatcher extends Matcher {
19    /** @var string One of the Token::T_* constants */
20    protected $type;
21
22    /** @var callable|null Something to call to further validate the token */
23    protected $callback = null;
24
25    /**
26     * @param string $type Token type to match
27     * @param callable|null $callback Something to call to further validate the token.
28     *  bool callback( Token )
29     */
30    public function __construct( $type, ?callable $callback = null ) {
31        $this->type = $type;
32        $this->callback = $callback;
33    }
34
35    /** @inheritDoc */
36    protected function generateMatches( ComponentValueList $values, $start, array $options ) {
37        $cv = $values[$start] ?? null;
38        if ( $cv instanceof Token && $cv->type() === $this->type &&
39            ( !$this->callback || ( $this->callback )( $cv ) )
40        ) {
41            yield $this->makeMatch( $values, $start, $this->next( $values, $start, $options ) );
42        }
43    }
44}