Completed
Push — master ( 671fb0...2bdcea )
by Christian
03:47
created

TimestampNormalizer::normalize()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.25

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.2
cc 4
eloc 4
nc 2
nop 3
crap 4.25
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