1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Popy\Calendar\Parser\FormatParser; |
4
|
|
|
|
5
|
|
|
use Popy\Calendar\Parser\FormatToken; |
6
|
|
|
use Popy\Calendar\Parser\FormatLexerInterface; |
7
|
|
|
use Popy\Calendar\Parser\FormatParserInterface; |
8
|
|
|
use Popy\Calendar\Parser\SymbolParserInterface; |
9
|
|
|
use Popy\Calendar\Parser\DateLexer\PregSimple; |
10
|
|
|
use Popy\Calendar\Parser\DateLexer\PregCollection; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Preg based implementation of the native DateTimeInterface format, with an |
14
|
|
|
* "extension" : | symbols delimits format alternatives. |
15
|
|
|
* |
16
|
|
|
* Uses PregCollection date lexer, so is only compatible with SymbolParsers that |
17
|
|
|
* returns Preg lexers. Otherwise, use another parser, like the basic one. |
18
|
|
|
* |
19
|
|
|
* PregCollection is usually 3x faster than a standard Collection lexer. |
20
|
|
|
*/ |
21
|
|
|
class PregExtendedNative implements FormatParserInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Format lexer. |
25
|
|
|
* |
26
|
|
|
* @var FormatLexerInterface |
27
|
|
|
*/ |
28
|
|
|
protected $lexer; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Symbol Parser. |
32
|
|
|
* |
33
|
|
|
* @var SymbolParserInterface |
34
|
|
|
*/ |
35
|
|
|
protected $symbolParser; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Class constructor. |
39
|
|
|
* |
40
|
|
|
* @param FormatLexerInterface $lexer Format lexer. |
41
|
|
|
* @param SymbolParserInterface $symbolParser Symbol Parser |
42
|
|
|
*/ |
43
|
|
|
public function __construct(FormatLexerInterface $lexer, SymbolParserInterface $symbolParser) |
44
|
|
|
{ |
45
|
|
|
$this->lexer = $lexer; |
46
|
|
|
$this->symbolParser = $symbolParser; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritDoc |
51
|
|
|
*/ |
52
|
|
|
public function parseFormat($format, $isRecursiveCall = false) |
53
|
|
|
{ |
54
|
|
|
$tokens = $this->lexer->tokenizeFormat($format); |
55
|
|
|
|
56
|
|
|
$dateParser = new PregCollection(); |
57
|
|
|
|
58
|
|
|
$eofLexer = new PregSimple(new FormatToken(null, FormatToken::TYPE_EOF)); |
59
|
|
|
|
60
|
|
|
foreach ($tokens as $token) { |
61
|
|
|
if ($token->is('|')) { |
62
|
|
|
if (!$isRecursiveCall) { |
63
|
|
|
$dateParser->register($eofLexer); |
64
|
|
|
} |
65
|
|
|
$dateParser->close(); |
66
|
|
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$lexer = null; |
70
|
|
|
|
71
|
|
|
if ($isRecursiveCall && $token->isType(FormatToken::TYPE_EOF)) { |
72
|
|
|
// Don't include EOF in recursive calls. |
73
|
|
|
break; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ( |
77
|
|
|
$token->isSymbol() |
78
|
|
|
&& null === $lexer = $this->symbolParser->parseSymbol( |
79
|
|
|
$token, |
80
|
|
|
$this |
81
|
|
|
) |
82
|
|
|
) { |
83
|
|
|
// finally, token seems litteral |
84
|
|
|
$token = $token->setLitteral(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($lexer === null) { |
88
|
|
|
$lexer = new PregSimple($token); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$dateParser->register($lexer); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$dateParser->close(); |
95
|
|
|
$dateParser->compile(); |
96
|
|
|
|
97
|
|
|
return $dateParser; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|