|
1
|
|
|
<?php |
|
2
|
|
|
namespace Wonnova\SDK\Serializer; |
|
3
|
|
|
|
|
4
|
|
|
use JMS\Serializer\Context; |
|
5
|
|
|
use JMS\Serializer\GraphNavigator; |
|
6
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
|
7
|
|
|
use Wonnova\SDK\Common\WonnovaDateTimeParserTrait; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* This fixes inconsistences in date fields returned from the API, so that all of them are cast into DateTime objects |
|
11
|
|
|
* @author Wonnova |
|
12
|
|
|
* @link http://www.wonnova.com |
|
13
|
|
|
*/ |
|
14
|
|
|
class DateTimeHandler implements SubscribingHandlerInterface |
|
15
|
|
|
{ |
|
16
|
|
|
use WonnovaDateTimeParserTrait; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Return format: |
|
20
|
|
|
* |
|
21
|
|
|
* array( |
|
22
|
|
|
* array( |
|
23
|
|
|
* 'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
|
24
|
|
|
* 'format' => 'json', |
|
25
|
|
|
* 'type' => 'DateTime', |
|
26
|
|
|
* 'method' => 'serializeDateTimeToJson', |
|
27
|
|
|
* ), |
|
28
|
|
|
* ) |
|
29
|
|
|
* |
|
30
|
|
|
* The direction and method keys can be omitted. |
|
31
|
|
|
* |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
43 |
|
public static function getSubscribingMethods() |
|
35
|
|
|
{ |
|
36
|
|
|
$wonnovaDateTimeDeserializationStrategyConfig = [ |
|
37
|
43 |
|
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
|
38
|
43 |
|
'type' => 'WonnovaDateTime', |
|
39
|
43 |
|
'method' => 'deserializeWonnovaDateTime', |
|
40
|
43 |
|
]; |
|
41
|
|
|
|
|
42
|
|
|
return [ |
|
43
|
43 |
|
static::getArrayWithFormat($wonnovaDateTimeDeserializationStrategyConfig), |
|
|
|
|
|
|
44
|
43 |
|
static::getArrayWithFormat($wonnovaDateTimeDeserializationStrategyConfig, 'array'), |
|
|
|
|
|
|
45
|
43 |
|
]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
43 |
|
private static function getArrayWithFormat($array, $format = 'json') |
|
49
|
|
|
{ |
|
50
|
43 |
|
$array['format'] = $format; |
|
51
|
43 |
|
return $array; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
13 |
|
public function deserializeWonnovaDateTime( |
|
55
|
|
|
$visitor, |
|
|
|
|
|
|
56
|
|
|
$data, |
|
57
|
|
|
array $type, |
|
|
|
|
|
|
58
|
|
|
Context $context |
|
|
|
|
|
|
59
|
|
|
) { |
|
60
|
13 |
|
return $this->parseWonnovaDateTime($data); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: