AgnosticFormatter::formatDateRepresentation()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
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