Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
33.33% |
1 / 3 |
CRAP | |
89.66% |
26 / 29 |
| PregChoice | |
0.00% |
0 / 1 |
|
33.33% |
1 / 3 |
9.09 | |
89.66% |
26 / 29 |
| __construct | |
0.00% |
0 / 1 |
3.05 | |
81.82% |
9 / 11 |
|||
| getExpression | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| hydrateResult | |
0.00% |
0 / 1 |
5.01 | |
94.12% |
16 / 17 |
|||
| <?php | |
| namespace Popy\Calendar\Parser\DateLexer; | |
| use InvalidArgumentException; | |
| use Popy\Calendar\Parser\FormatToken; | |
| use Popy\Calendar\Parser\DateLexerResult; | |
| class PregChoice extends AbstractPreg | |
| { | |
| /** | |
| * Registered symbol. | |
| * | |
| * @var string|null | |
| */ | |
| protected $symbol; | |
| /** | |
| * Choices. | |
| * | |
| * @var array<string> | |
| */ | |
| protected $choices; | |
| /** | |
| * Expression. | |
| * | |
| * @var string | |
| */ | |
| protected $expression; | |
| public function __construct(FormatToken $token, array $choices) | |
| { | |
| if (!$token->isSymbol()) { | |
| throw new InvalidArgumentException( | |
| 'You must supply a Symbol token' | |
| ); | |
| } | |
| $this->symbol = $token->getName(); | |
| $this->choices = $choices; | |
| $parts = []; | |
| foreach ($choices as $part) { | |
| $parts[] = '(?i:' . preg_quote($part, '/') . ')'; | |
| } | |
| $this->expression = '(' . implode('|', $parts) . ')'; | |
| } | |
| /** | |
| * @inheritDoc | |
| */ | |
| public function getExpression() | |
| { | |
| return $this->expression; | |
| } | |
| /** | |
| * @inheritDoc | |
| */ | |
| public function hydrateResult(DateLexerResult $result, $match, $offset = 1) | |
| { | |
| if (!isset($match[$offset])) { | |
| return $offset; | |
| } | |
| // Did match | |
| if ($match[$offset][1] !== -1) { | |
| $found = array_search($match[$offset][0], $this->choices); | |
| // DO THE MAGIC | |
| if ($found === false) { | |
| $res = preg_grep( | |
| '/^' . preg_quote($match[$offset][0], '/') . '$/i', | |
| $this->choices | |
| ); | |
| if (count($res)) { | |
| reset($res); | |
| $found = key($res); | |
| } | |
| } | |
| $result->set($this->symbol, $found); | |
| } | |
| return $offset + 1; | |
| } | |
| } |