Completed
Push — master ( 638b8d...de83b5 )
by John
01:47 queued 17s
created

DateTimeProcessor::hydrate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the KleijnWeb\PhpApi\Hydrator package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\PhpApi\Hydrator\Processors\Scalar;
10
11
use KleijnWeb\PhpApi\Descriptions\Description\Schema\ScalarSchema;
12
use KleijnWeb\PhpApi\Hydrator\DateTimeSerializer;
13
14
/**
15
 * @author John Kleijn <[email protected]>
16
 */
17
class DateTimeProcessor extends ScalarProcessor
18
{
19
    /**
20
     * @var DateTimeSerializer
21
     */
22
    protected $dateTimeSerializer;
23
24
25
    public function __construct(ScalarSchema $schema, DateTimeSerializer $dateTimeSerializer)
26
    {
27
        parent::__construct($schema);
28
        $this->dateTimeSerializer = $dateTimeSerializer;
29
    }
30
31
    /**
32
     * @param string $value
33
     * @return \DateTime
34
     */
35
    public function hydrate($value)
36
    {
37
        $value = $value === null ? $this->schema->getDefault() : $value;
38
39
        return $this->dateTimeSerializer->deserialize($value, $this->schema);
40
    }
41
42
    /**
43
     * @param mixed $value
44
     * @return string
45
     */
46
    public function dehydrate($value)
47
    {
48
        return $this->dateTimeSerializer->serialize($value, $this->schema);
49
    }
50
}
51