Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
75.00% |
3 / 4 |
CRAP | |
87.50% |
14 / 16 |
Chain | |
0.00% |
0 / 1 |
|
75.00% |
3 / 4 |
8.12 | |
87.50% |
14 / 16 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
addParser | |
0.00% |
0 / 1 |
2.26 | |
60.00% |
3 / 5 |
|||
addParsers | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
parseSymbol | |
100.00% |
1 / 1 |
3 | |
100.00% |
5 / 5 |
<?php | |
namespace Popy\Calendar\Parser\SymbolParser; | |
use Popy\Calendar\Parser\FormatToken; | |
use Popy\Calendar\Parser\SymbolParserInterface; | |
use Popy\Calendar\Parser\FormatParserInterface; | |
use Popy\Calendar\Parser\DateLexer\PregSimple; | |
use Popy\Calendar\Parser\DateLexer\PregChoice; | |
use Popy\Calendar\Formatter\LocalisationInterface; | |
use Popy\Calendar\Formatter\Localisation\NativeHardcoded; | |
/** | |
* Chain implementation, stopping at the first result | |
*/ | |
class Chain implements SymbolParserInterface | |
{ | |
/** | |
* Parser chain. | |
* | |
* @var array<SymbolParserInterface> | |
*/ | |
protected $parsers = []; | |
/** | |
* Class constructor. | |
* | |
* @param iterable<SymbolParserInterface> $parsers Parser chain. | |
*/ | |
public function __construct($parsers = []) | |
{ | |
$this->addParsers($parsers); | |
} | |
/** | |
* Adds a Parser to the chain. | |
* | |
* @param SymbolParserInterface $parser | |
*/ | |
public function addParser(SymbolParserInterface $parser) | |
{ | |
if ($parser instanceof self) { | |
// Reducing recursivity | |
$this->addParsers($parser->parsers); | |
} else { | |
$this->parsers[] = $parser; | |
} | |
return $this; | |
} | |
/** | |
* Add parsers to the chain. | |
* | |
* @param iterable<SymbolParserInterface> $parsers | |
*/ | |
public function addParsers($parsers) | |
{ | |
foreach ($parsers as $parser) { | |
$this->addParser($parser); | |
} | |
return $this; | |
} | |
/** | |
* @inheritDoc | |
*/ | |
public function parseSymbol(FormatToken $token, FormatParserInterface $parser) | |
{ | |
foreach ($this->parsers as $p) { | |
if (null !== $res = $p->parseSymbol($token, $parser)) { | |
return $res; | |
} | |
} | |
} | |
} |