Completed
Push — master ( 8b290e...989048 )
by Pierre
05:45
created

StandardDateSolar::formatSymbol()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 10
nc 6
nop 3
dl 0
loc 23
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace Popy\Calendar\Formatter\SymbolFormatter;
4
5
use Popy\Calendar\FormatterInterface;
6
use Popy\Calendar\Parser\FormatToken;
7
use Popy\Calendar\Formatter\SymbolFormatterInterface;
8
use Popy\Calendar\Formatter\NumberConverterInterface;
9
use Popy\Calendar\ValueObject\DateRepresentationInterface;
10
use Popy\Calendar\ValueObject\DateSolarRepresentationInterface;
11
12
/**
13
 * Standard format, handling DateSolarRepresentationInterface.
14
 */
15
class StandardDateSolar implements SymbolFormatterInterface
16
{
17
    /**
18
     * Number converter.
19
     *
20
     * @var NumberConverterInterface
21
     */
22
    protected $converter;
23
24
    /**
25
     * Class constructor.
26
     *
27
     * @param NumberConverterInterface $converter Number converter.]
28
     */
29
    public function __construct(NumberConverterInterface $converter)
30
    {
31
        $this->converter = $converter;
32
    }
33
34
    /**
35
     * @inheritDoc
36
     */
37
    public function formatSymbol(DateRepresentationInterface $input, FormatToken $token, FormatterInterface $formatter)
38
    {
39
        if (!$input instanceof DateSolarRepresentationInterface) {
40
            return;
41
        }
42
        if ($token->is('y')) {
43
            // y   A two digit representation of a year
44
            return $this->converter->to($input->getYear() ?: 0);
45
        }
46
47
        if ($token->is('Y')) {
48
            // Y   A full numeric representation of a year, 4 digits
49
            return sprintf('%04d', $input->getYear());
50
        }
51
52
        if ($token->is('L')) {
53
            // L   Whether it's a leap year
54
            return (string)(int)$input->isLeapYear();
55
        }
56
57
        if ($token->is('z')) {
58
            // z   The day of the year (starting from 0)
59
            return (string)$input->getDayIndex();
60
        }
61
    }
62
}
63