StandardDateSolar::map()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Popy\Calendar\Parser\ResultMapper;
4
5
use Popy\Calendar\Parser\DateLexerResult;
6
use Popy\Calendar\Parser\ResultMapperInterface;
7
use Popy\Calendar\ValueObject\DateRepresentationInterface;
8
use Popy\Calendar\ValueObject\DateSolarRepresentationInterface;
9
10
/**
11
 * Maps standard format year symbols to DateSolarRepresentationInterface fields.
12
 */
13
class StandardDateSolar implements ResultMapperInterface
14
{
15
    /**
16
     * @inheritDoc
17
     */
18
    public function map(DateLexerResult $parts, DateRepresentationInterface $date)
19
    {
20
        if (!$date instanceof DateSolarRepresentationInterface) {
21
            return;
22
        }
23
24
        return $date
25
            ->withYear($this->determineYear($parts), $parts->get('L'))
26
            ->withDayIndex($this->determineDayIndex($parts), null)
27
        ;
28
    }
29
30
    /**
31
     * Determine year.
32
     *
33
     * @param DateLexerResult $parts
34
     *
35
     * @return integer|null
36
     */
37
    protected function determineYear(DateLexerResult $parts)
38
    {
39
        // Assumes 'y' is properly handled in lexer
40
        return $parts->getFirst('Y', 'y');
41
    }
42
43
    /**
44
     * Determine year.
45
     *
46
     * @param DateLexerResult $parts
47
     *
48
     * @return integer|null
49
     */
50
    protected function determineDayIndex(DateLexerResult $parts)
51
    {
52
        // z   The day of the year (starting from 0)
53
        if (null !== $z = $parts->get('z')) {
54
            return (int)$z;
55
        }
56
    }
57
}
58