1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of byrokrat\autogiro. |
5
|
|
|
* |
6
|
|
|
* byrokrat\autogiro is free software: you can redistribute it and/or |
7
|
|
|
* modify it under the terms of the GNU General Public License as published |
8
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* byrokrat\autogiro is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License |
17
|
|
|
* along with byrokrat\autogiro. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
* |
19
|
|
|
* Copyright 2016-21 Hannes Forsgård |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
declare(strict_types=1); |
23
|
|
|
|
24
|
|
|
namespace byrokrat\autogiro\Visitor; |
25
|
|
|
|
26
|
|
|
use byrokrat\autogiro\Tree\Node; |
27
|
|
|
use byrokrat\autogiro\Tree\Obj; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Visitor that expands date nodes |
31
|
|
|
* |
32
|
|
|
* Creates DateTime object as child Node::OBJ |
33
|
|
|
*/ |
34
|
|
|
final class DateVisitor extends Visitor |
35
|
|
|
{ |
36
|
|
|
use ErrorAwareTrait; |
37
|
|
|
|
38
|
|
|
public function beforeDate(Node $node): void |
39
|
|
|
{ |
40
|
|
|
if ($node->hasChild(Node::OBJ)) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$number = (string)$node->getValueFrom(Node::NUMBER); |
45
|
|
|
|
46
|
|
|
if (!trim($number)) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$date = null; |
51
|
|
|
|
52
|
|
|
switch (strlen($number)) { |
53
|
|
|
case 6: |
54
|
|
|
$date = \DateTimeImmutable::createFromFormat('ymd', $number); |
55
|
|
|
break; |
56
|
|
|
case 8: |
57
|
|
|
$date = \DateTimeImmutable::createFromFormat('Ymd', $number); |
58
|
|
|
break; |
59
|
|
|
case 20: |
60
|
|
|
$date = \DateTimeImmutable::createFromFormat('YmdHis', substr($number, 0, -6)); |
61
|
|
|
break; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (!$date) { |
65
|
|
|
$this->getErrorObject()->addError( |
66
|
|
|
"Invalid date %s on line %s", |
67
|
|
|
$number, |
68
|
|
|
(string)$node->getLineNr() |
69
|
|
|
); |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$node->addChild(new Obj($node->getLineNr(), $date)); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|