|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the xAPI package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Christian Flothmann <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Xabbuh\XApi\Serializer\Normalizer; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
|
15
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
|
16
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Normalizes and denormalizes xAPI statement timestamps. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Christian Flothmann <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
final class TimestampNormalizer implements DenormalizerInterface, NormalizerInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
1 |
|
public function denormalize($data, $class, $format = null, array $context = array()) |
|
29
|
|
|
{ |
|
30
|
1 |
|
return new \DateTime($data); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
50 |
|
public function supportsDenormalization($data, $type, $format = null) |
|
37
|
|
|
{ |
|
38
|
50 |
|
return 'DateTime' === $type; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public function normalize($object, $format = null, array $context = array()) |
|
45
|
|
|
{ |
|
46
|
1 |
|
if (!($object instanceof \DateTime || $object instanceof \DateTimeInterface)) { |
|
47
|
|
|
throw new InvalidArgumentException(sprintf('Expected \DateTime object or object implementing \DateTimeInterface (got "%s").', is_object($object) ? get_class($object) : gettype($object))); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
return $object->format('c'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
49 |
|
public function supportsNormalization($data, $format = null) |
|
57
|
|
|
{ |
|
58
|
49 |
|
return $data instanceof \DateTime || $data instanceof \DateTimeInterface; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|