StandardDateSolar   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A map() 0 9 2
A determineYear() 0 4 1
A determineDayIndex() 0 5 2
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