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
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 a token of a particular type
14 * @see https://www.w3.org/TR/2019/CR-css-values-3-20190606/#component-types
15 */
16class TokenMatcher extends Matcher {
17    /** @var string One of the Token::T_* constants */
18    protected $type;
19
20    /** @var callable|null Something to call to further validate the token */
21    protected $callback = null;
22
23    /**
24     * @param string $type Token type to match
25     * @param callable|null $callback Something to call to further validate the token.
26     *  bool callback( Token )
27     */
28    public function __construct( $type, callable $callback = null ) {
29        $this->type = $type;
30        $this->callback = $callback;
31    }
32
33    /** @inheritDoc */
34    protected function generateMatches( ComponentValueList $values, $start, array $options ) {
35        $cv = $values[$start] ?? null;
36        if ( $cv instanceof Token && $cv->type() === $this->type &&
37            ( !$this->callback || call_user_func( $this->callback, $cv ) )
38        ) {
39            yield $this->makeMatch( $values, $start, $this->next( $values, $start, $options ) );
40        }
41    }
42}