DateIntervalSerializer::unserialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
3
/**
4
 * Author: Nil Portugués Calderó <[email protected]>
5
 * Date: 7/3/15
6
 * Time: 6:00 PM.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace NilPortugues\Serializer\Serializer\InternalClasses;
12
13
use DateInterval;
14
use NilPortugues\Serializer\Serializer;
15
use ReflectionClass;
16
17
class DateIntervalSerializer
18
{
19
    /**
20
     * @param Serializer $serializer
21
     * @param string     $className
22
     * @param array      $value
23
     *
24
     * @return object
25
     */
26
    public static function unserialize(Serializer $serializer, $className, array $value)
27
    {
28
        $ref = new ReflectionClass($className);
29
30
        return self::fillObjectProperties(self::getTypedValue($serializer, $value), $ref);
31
    }
32
33
    /**
34
     * @param array           $value
35
     * @param ReflectionClass $ref
36
     *
37
     * @return object
38
     */
39
    protected static function fillObjectProperties(array $value, ReflectionClass $ref)
40
    {
41
        $obj = $ref->newInstanceArgs([$value['construct']]);
42
        unset($value['construct']);
43
44
        foreach ($value as $k => $v) {
45
            $obj->$k = $v;
46
        }
47
48
        return $obj;
49
    }
50
51
    /**
52
     * @param Serializer $serializer
53
     * @param array      $value
54
     *
55
     * @return mixed
56
     */
57
    protected static function getTypedValue(Serializer $serializer, array $value)
58
    {
59
        foreach ($value as &$v) {
60
            $v = $serializer->unserialize($v);
61
        }
62
63
        return $value;
64
    }
65
66
    /**
67
     * @param Serializer   $serializer
68
     * @param DateInterval $dateInterval
69
     *
70
     * @return mixed
71
     */
72
    public static function serialize(Serializer $serializer, DateInterval $dateInterval)
0 ignored issues
show
Unused Code introduced by
The parameter $serializer is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73
    {
74
        return array(
75
            Serializer::CLASS_IDENTIFIER_KEY => \get_class($dateInterval),
76
            'construct' => array(
77
                Serializer::SCALAR_TYPE => 'string',
78
                Serializer::SCALAR_VALUE => \sprintf(
79
                    'P%sY%sM%sDT%sH%sM%sS',
80
                    $dateInterval->y,
81
                    $dateInterval->m,
82
                    $dateInterval->d,
83
                    $dateInterval->h,
84
                    $dateInterval->i,
85
                    $dateInterval->s
86
                ),
87
            ),
88
            'invert' => array(
89
                Serializer::SCALAR_TYPE => 'integer',
90
                Serializer::SCALAR_VALUE => (empty($dateInterval->invert)) ? 0 : 1,
91
            ),
92
            'days' => array(
93
                Serializer::SCALAR_TYPE => \gettype($dateInterval->days),
94
                Serializer::SCALAR_VALUE => $dateInterval->days,
95
            ),
96
        );
97
    }
98
}
99