LocalDateMapper::awake()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 26
c 0
b 0
f 0
rs 9.2222
ccs 12
cts 12
cp 1
cc 6
nc 6
nop 2
crap 6
1
<?php
2
/**
3
 * File was created 30.09.2015 07:58
4
 */
5
6
namespace PeekAndPoke\Component\Slumber\Core\Codec\Property;
7
8
use PeekAndPoke\Component\Psi\Psi\IsDateString;
9
use PeekAndPoke\Component\Slumber\Annotation\Slumber\AsLocalDate;
10
use PeekAndPoke\Component\Slumber\Core\Codec\Awaker;
11
use PeekAndPoke\Component\Slumber\Core\Codec\Slumberer;
12
use PeekAndPoke\Types\LocalDate;
13
14
/**
15
 * @author Karsten J. Gerber <[email protected]>
16
 */
17
class LocalDateMapper extends AbstractPropertyMapper
18
{
19
    /** @var AsLocalDate */
20
    private $options;
21
22
    /**
23
     * C'tor.
24
     *
25
     * @param AsLocalDate $options
26
     */
27 32
    public function __construct(AsLocalDate $options)
28
    {
29 32
        $this->options = $options;
30 32
    }
31
32
    /**
33
     * @return AsLocalDate
34
     */
35 1
    public function getOptions()
36
    {
37 1
        return $this->options;
38
    }
39
40
    /**
41
     * @param Slumberer $slumberer
42
     * @param LocalDate $value
43
     *
44
     * @return array
45
     */
46 17
    public function slumber(Slumberer $slumberer, $value)
47
    {
48 17
        if (!$value instanceof LocalDate) {
0 ignored issues
show
introduced by
$value is always a sub-type of PeekAndPoke\Types\LocalDate.
Loading history...
49 8
            return null;
50
        }
51
52
        return [
53 9
            'date' => $value->format('Y-m-d\TH:i:s.uP'),
54 9
            'tz' => $value->getTimezone()->getName(),
55
        ];
56
    }
57
58
    /**
59
     * @param Awaker $awaker
60
     * @param mixed  $value
61
     *
62
     * @return LocalDate
63
     */
64 11
    public function awake(Awaker $awaker, $value)
65
    {
66 11
        if ($value === null) {
67 1
            return null;
68
        }
69
70
        // check for a complex input with date and timezone
71 10
        if (isset($value['date'], $value['tz'])) {
72
73 5
            if (false === ($tz = @timezone_open($value['tz']))) {
74 1
                return null;
75
            }
76
77 4
            if (IsDateString::isValidDateString($value['date'])) {
78 3
                return new LocalDate($value['date'],  $tz);
79
            }
80
81 1
            return null;
82
        }
83
84
        // check for a simple input
85 5
        if (IsDateString::isValidDateString($value)) {
86 2
            return LocalDate::raw(new \DateTime($value));
87
        }
88
89 3
        return null;
90
    }
91
}
92