|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Popy\Calendar\Formatter; |
|
4
|
|
|
|
|
5
|
|
|
use DateTimeInterface; |
|
6
|
|
|
use Popy\Calendar\FormatterInterface; |
|
7
|
|
|
use Popy\Calendar\ConverterInterface; |
|
8
|
|
|
use Popy\Calendar\Parser\FormatLexerInterface; |
|
9
|
|
|
use Popy\Calendar\ValueObject\DateRepresentationInterface; |
|
10
|
|
|
use Popy\Calendar\ValueObject\DateRepresentation\Date; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Agnostic formatter implementation. |
|
14
|
|
|
*/ |
|
15
|
|
|
class AgnosticFormatter implements FormatterInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Format lexer |
|
19
|
|
|
* |
|
20
|
|
|
* @var FormatLexerInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $lexer; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Date converter. |
|
26
|
|
|
* |
|
27
|
|
|
* @var ConverterInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $converter; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Symbol Formatter |
|
33
|
|
|
* |
|
34
|
|
|
* @var SymbolFormatterInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $formatter; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Class constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param FormatLexerInterface $lexer Format lexer. |
|
42
|
|
|
* @param ConverterInterface $converter Date converter. |
|
43
|
|
|
* @param SymbolFormatterInterface $formatter Symbol formatter. |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(FormatLexerInterface $lexer, ConverterInterface $converter, SymbolFormatterInterface $formatter) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->lexer = $lexer; |
|
48
|
|
|
$this->converter = $converter; |
|
49
|
|
|
$this->formatter = $formatter; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritDoc |
|
54
|
|
|
*/ |
|
55
|
|
|
public function format(DateTimeInterface $input, $format) |
|
56
|
|
|
{ |
|
57
|
|
|
$date = Date::buildFromDateTimeInterface($input); |
|
58
|
|
|
|
|
59
|
|
|
$date = $this->converter->to($date); |
|
60
|
|
|
|
|
61
|
|
|
return $this->formatDateRepresentation($date, $format); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @inheritDoc |
|
66
|
|
|
*/ |
|
67
|
|
|
public function formatDateRepresentation(DateRepresentationInterface $input, $format) |
|
68
|
|
|
{ |
|
69
|
|
|
$res = ''; |
|
70
|
|
|
$tokens = $this->lexer->tokenizeFormat($format); |
|
71
|
|
|
|
|
72
|
|
|
foreach ($tokens as $token) { |
|
73
|
|
|
$res .= $this->formatter->formatSymbol($input, $token, $this); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $res; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|