Completed
Pull Request — master (#15)
by
unknown
04:37
created

JsonSchemaDateHandler::serializeDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 2
1
<?php
2
namespace GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler;
3
4
use JMS\Serializer\Context;
5
use JMS\Serializer\GraphNavigator;
6
use JMS\Serializer\Handler\SubscribingHandlerInterface;
7
use JMS\Serializer\JsonDeserializationVisitor;
8
use JMS\Serializer\JsonSerializationVisitor;
9
use RuntimeException;
10
11
class JsonSchemaDateHandler implements SubscribingHandlerInterface
12
{
13
14
    protected $defaultTimezone;
15
16 View Code Duplication
    public static function getSubscribingMethods()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
    {
18
        return array(
19
            array(
20
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
21
                'format' => 'json',
22
                'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Date',
23
                'method' => 'deserializeDate'
24
            ),
25
            array(
26
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
27
                'format' => 'json',
28
                'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Date',
29
                'method' => 'serializeDate'
30
            ),
31
            array(
32
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
33
                'format' => 'json',
34
                'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\DateTime',
35
                'method' => 'deserializeDateTime'
36
            ),
37
            array(
38
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
39
                'format' => 'json',
40
                'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\DateTime',
41
                'method' => 'serializeDateTime'
42
            ),
43
            array(
44
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
45
                'format' => 'json',
46
                'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Time',
47
                'method' => 'deserializeTime'
48
            ),
49
            array(
50
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
51
                'format' => 'json',
52
                'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Time',
53
                'method' => 'serializeTime'
54
            ),
55
            array(
56
                'type' => 'DateInterval',
57
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
58
                'format' => 'json',
59
                'method' => 'deserializeDateIntervalXml',
60
            ),
61
        );
62
    }
63
64
    public function __construct($defaultTimezone = 'UTC')
65
    {
66
        $this->defaultTimezone = new \DateTimeZone($defaultTimezone);
67
68
    }
69
70
    public function deserializeDateIntervalXml(JsonDeserializationVisitor $visitor, $data, array $type){
0 ignored issues
show
Unused Code introduced by
The parameter $visitor 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...
Unused Code introduced by
The parameter $type 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...
71
        return $this->createDateInterval((string)$data);
72
    }
73
74
    public function serializeDate(JsonSerializationVisitor $visitor, \DateTime $date, array $type, Context $context)
0 ignored issues
show
Unused Code introduced by
The parameter $visitor 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...
Unused Code introduced by
The parameter $type 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...
Unused Code introduced by
The parameter $context 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...
75
    {
76
        return $date->format('Y-m-d');
77
    }
78
79
    public function deserializeDate(JsonDeserializationVisitor $visitor, $data, array $type)
0 ignored issues
show
Unused Code introduced by
The parameter $visitor 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...
80
    {
81
        if (!preg_match('/^(\d{4})-(\d{2})-(\d{2})(Z|([+-]\d{2}:\d{2}))?$/', $data)) {
82
            throw new RuntimeException(sprintf('Invalid date "%s", expected valid XML Schema date string.', $data));
83
        }
84
85
        return $this->parseDateTime($data, $type);
86
    }
87
88
    public function serializeDateTime(JsonSerializationVisitor $visitor, \DateTime $date, array $type, Context $context)
0 ignored issues
show
Unused Code introduced by
The parameter $visitor 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...
Unused Code introduced by
The parameter $type 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...
Unused Code introduced by
The parameter $context 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...
89
    {
90
91
        return $date->format(\DateTime::W3C);
92
    }
93
94
    public function deserializeDateTime(JsonDeserializationVisitor $visitor, $data, array $type)
0 ignored issues
show
Unused Code introduced by
The parameter $visitor 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...
95
    {
96
        return $this->parseDateTime($data, $type);
97
98
    }
99
100
    public function serializeTime(JsonSerializationVisitor $visitor, \DateTime $date, array $type, Context $context)
0 ignored issues
show
Unused Code introduced by
The parameter $visitor 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...
Unused Code introduced by
The parameter $type 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...
Unused Code introduced by
The parameter $context 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...
101
    {
102
        $v = $date->format('H:i:s');
103
        if ($date->getTimezone()->getOffset($date) !== $this->defaultTimezone->getOffset($date)) {
104
            $v .= $date->format('P');
105
        }
106
        return $v;
107
    }
108
109
    public function deserializeTime(JsonDeserializationVisitor $visitor, $data, array $type)
0 ignored issues
show
Unused Code introduced by
The parameter $visitor 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...
Unused Code introduced by
The parameter $type 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...
110
    {
111
        $data = (string)$data;
112
113
        return new \DateTime($data, $this->defaultTimezone);
114
    }
115
116 View Code Duplication
    private function parseDateTime($data, array $type)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
    {
118
        $timezone = isset($type['params'][1]) ? new \DateTimeZone($type['params'][1]) : $this->defaultTimezone;
119
        $datetime = new \DateTime((string)$data, $timezone);
120
        if (false === $datetime) {
121
            throw new RuntimeException(sprintf('Invalid datetime "%s", expected valid XML Schema dateTime string.', $data));
122
        }
123
124
        return $datetime;
125
    }
126
127 View Code Duplication
    private function createDateInterval($interval){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
        $f = 0.0;
129
        if (preg_match('~\.\d+~',$interval,$match)) {
130
            $interval = str_replace($match[0], "", $interval);
131
            $f = (float)$match[0];
132
        }
133
        $di = new \DateInterval($interval);
134
        // milliseconds are only available from >=7.1
135
        if(isset($di->f)){
136
            $di->f= $f;
0 ignored issues
show
Bug introduced by
The property f does not seem to exist in DateInterval.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
137
        }
138
139
        return $di;
140
    }
141
}
142
143