1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\CiffRenderer\Parser\FieldParser; |
4
|
|
|
|
5
|
|
|
use Graze\CiffRenderer\Parser\FieldParser\FieldParserFixedText; |
6
|
|
|
use Graze\CiffRenderer\Parser\FieldParser\FieldParserInterface; |
7
|
|
|
use \DateTimeImmutable; |
8
|
|
|
use Graze\CiffRenderer\DateFormatter\DateFormatterFactory; |
9
|
|
|
use Graze\CiffRenderer\Parser\FieldParserRegistry; |
10
|
|
|
use \SimpleXMLElement; |
11
|
|
|
use Graze\CiffRenderer\Exception\UnsupportedDateFormatException; |
12
|
|
|
|
13
|
|
|
abstract class AbstractFieldParserDate extends FieldParserFixedText implements FieldParserInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var \DateTimeImmutable |
17
|
|
|
*/ |
18
|
|
|
protected $dateTimeNow; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var DateFormatterFactory |
22
|
|
|
*/ |
23
|
|
|
private $dateFormatterFactory; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param DateTimeImmutable $dateTimeNow |
27
|
|
|
* @param DateFormatterFactory $dateFormatterFactory |
28
|
|
|
* @param FieldParserRegistry $fieldParserRegistry |
29
|
|
|
* @param SimpleXMLElement $xmlHeader |
30
|
|
|
* @param SimpleXMLElement $xmlField |
31
|
|
|
* @param float $scale |
32
|
|
|
*/ |
33
|
16 |
|
public function __construct( |
34
|
|
|
DateTimeImmutable $dateTimeNow, |
35
|
|
|
DateFormatterFactory $dateFormatterFactory, |
36
|
|
|
FieldParserRegistry $fieldParserRegistry, |
37
|
|
|
SimpleXMLElement $xmlHeader, |
38
|
|
|
SimpleXMLElement $xmlField, |
39
|
|
|
$scale |
40
|
|
|
) { |
41
|
16 |
|
$this->dateTimeNow = $dateTimeNow; |
42
|
16 |
|
$this->dateFormatterFactory = $dateFormatterFactory; |
43
|
|
|
|
44
|
16 |
|
parent::__construct( |
45
|
16 |
|
$fieldParserRegistry, |
46
|
|
|
$xmlHeader, |
47
|
|
|
$xmlField, |
48
|
|
|
$scale |
49
|
|
|
); |
50
|
16 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
* @throws UnsupportedDateFormatException |
55
|
|
|
*/ |
56
|
2 |
|
public function getText() |
57
|
|
|
{ |
58
|
|
|
try { |
59
|
2 |
|
$dateFormat = $this->getDateFormat(); |
60
|
2 |
|
$dateFormatter = $this->dateFormatterFactory->getFormatter($dateFormat); |
61
|
2 |
|
return $dateFormatter->format($this->getDateTime(), $dateFormat); |
62
|
|
|
} catch (\Exception $e) { |
63
|
|
|
throw new UnsupportedDateFormatException($this->getDateFormat(), $e); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Fetch a DateTime object set to the correct date/time. |
69
|
|
|
* |
70
|
|
|
* @return \DateTimeInterface |
71
|
|
|
*/ |
72
|
|
|
abstract protected function getDateTime(); |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
2 |
|
public function getDateFormat() |
78
|
|
|
{ |
79
|
2 |
|
return (string) $this->xmlField->Data->Object->Default; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param FieldParserRegistry $fieldParserRegistry |
84
|
|
|
* @param SimpleXMLElement $xmlHeader |
85
|
|
|
* @param SimpleXMLElement $xmlField |
86
|
|
|
* @param float $scale |
87
|
|
|
* @return FieldParserInterface |
88
|
|
|
*/ |
89
|
2 |
|
public static function factory( |
90
|
|
|
FieldParserRegistry $fieldParserRegistry, |
91
|
|
|
SimpleXMLElement $xmlHeader, |
92
|
|
|
SimpleXMLElement $xmlField, |
93
|
|
|
$scale |
94
|
|
|
) { |
95
|
2 |
|
return new static( |
96
|
2 |
|
new DateTimeImmutable(), |
97
|
2 |
|
new DateFormatterFactory(), |
98
|
|
|
$fieldParserRegistry, |
99
|
|
|
$xmlHeader, |
100
|
|
|
$xmlField, |
101
|
|
|
$scale |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|