| 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 |
|
| 11 |
/** |
| 12 |
* Wrap another matcher in a callback to verify the matches |
| 13 |
*/ |
| 14 |
class CheckedMatcher extends Matcher { |
| 15 |
/** @var Matcher */ |
| 16 |
private $matcher; |
| 17 |
|
| 18 |
/** @var callable */ |
| 19 |
protected $check; |
| 20 |
|
| 21 |
/** |
| 22 |
* @param Matcher $matcher Base matcher |
| 23 |
* @param callable $check Function to check the match is really valid. |
| 24 |
* Prototype is bool func( ComponentValueList $values, GrammarMatch $match, array $options ) |
| 25 |
*/ |
| 26 |
public function __construct( Matcher $matcher, callable $check ) { |
| 27 |
$this->matcher = $matcher; |
| 28 |
$this->check = $check; |
| 29 |
} |
| 30 |
|
| 31 |
/** @inheritDoc */ |
| 32 |
protected function generateMatches( ComponentValueList $values, $start, array $options ) { |
| 33 |
foreach ( $this->matcher->generateMatches( $values, $start, $options ) as $match ) { |
| 34 |
if ( ( $this->check )( $values, $match, $options ) ) { |
| 35 |
yield $this->makeMatch( $values, $start, $match->getNext(), $match ); |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
} |