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

StandardDateFragmented   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 110
rs 10
c 0
b 0
f 0
wmc 17

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
D formatSymbol() 0 88 16
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\LocalisationInterface;
8
use Popy\Calendar\Formatter\SymbolFormatterInterface;
9
use Popy\Calendar\ValueObject\DateRepresentationInterface;
10
use Popy\Calendar\ValueObject\DateFragmentedRepresentationInterface;
11
12
/**
13
 * Standard format, handling DateFragmentedRepresentationInterface.
14
 *
15
 * Weeks and day names assume a gregorian calendar structure.
16
 */
17
class StandardDateFragmented implements SymbolFormatterInterface
18
{
19
    /**
20
     * Locale (used for day & month names)
21
     *
22
     * @var LocalisationInterface
23
     */
24
    protected $locale;
25
26
    /**
27
     * Class constructor.
28
     *
29
     * @param LocalisationInterface $locale
30
     */
31
    public function __construct(LocalisationInterface $locale)
32
    {
33
        $this->locale = $locale;
34
    }
35
36
    /**
37
     * @inheritDoc
38
     */
39
    public function formatSymbol(DateRepresentationInterface $input, FormatToken $token, FormatterInterface $formatter)
40
    {
41
        if (!$input instanceof DateFragmentedRepresentationInterface) {
42
            return;
43
        }
44
45
        if ($token->is('F')) {
46
            // F   A full textual representation of a month
47
            return (string)$this->locale->getMonthName(
48
                $input->getDateParts()->get(0)
49
            );
50
        }
51
52
        if ($token->is('M')) {
53
            // M   A short textual representation of a month, three letters
54
            return (string)$this->locale->getMonthShortName(
55
                $input->getDateParts()->get(0)
56
            );
57
        }
58
59
        if ($token->is('m')) {
60
            // m   Numeric representation of a month, with leading zeros
61
            return sprintf('%02d', $input->getDateParts()->get(0) + 1);
62
        }
63
64
        if ($token->is('n')) {
65
            // n   Numeric representation of a month, without leading zeros
66
            return $input->getDateParts()->get(0) + 1;
67
        }
68
69
        if ($token->is('t')) {
70
            // t    Number of days in the given month
71
            return (int)$input->getDateParts()->getSize(0);
72
        }
73
74
        if ($token->is('d')) {
75
            // d   Day of the month, 2 digits with leading zeros
76
            return sprintf('%02d', $input->getDateParts()->get(1) + 1);
77
        }
78
79
        if ($token->is('j')) {
80
            // j   Day of the month without leading zeros
81
            return $input->getDateParts()->get(1) + 1;
82
        }
83
84
        if ($token->is('S')) {
85
            // S   English ordinal suffix for the day of the month, 2 characters
86
            return $this->locale->getNumberOrdinalSuffix(
87
                (int)$input->getDateParts()->get(1)
88
            );
89
        }
90
91
        if ($token->is('l')) {
92
            // l (lowercase 'L')   A full textual representation of the day of the week
93
            return (string)$this->locale->getDayName(
94
                $input->getDateParts()->getTransversal(2)
95
            );
96
        }
97
98
        if ($token->is('D')) {
99
            // D   A textual representation of a day, three letters
100
            return (string)$this->locale->getDayShortName(
101
                $input->getDateParts()->getTransversal(2)
102
            );
103
        }
104
        
105
        if ($token->is('w')) {
106
            // w   Numeric representation of the day of the week   0 (for Sunday) through 6 (for Saturday)
107
            return (1 + $input->getDateParts()->getTransversal(2)) % 7;
108
        }
109
110
        if ($token->is('N')) {
111
            // N   ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) 1 (for Monday) through 7 (for Sunday)
112
            return $input->getDateParts()->getTransversal(2) + 1;
113
        }
114
115
        if ($token->is('W')) {
116
            // W   ISO-8601 week number of year, weeks starting on Monday
117
            return sprintf(
118
                '%02d',
119
                $input->getDateParts()->getTransversal(1) + 1
120
            );
121
        }
122
123
        if ($token->is('o')) {
124
            // Y   A full numeric representation of a year, 4 digits
125
            // o   ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead.
126
            return sprintf('%04d', $input->getDateParts()->getTransversal(0));
127
        }
128
    }
129
}
130