Completed
Pull Request — master (#10)
by Christian
03:55
created

TimestampNormalizer::supportsNormalization()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Xabbuh\XApi\Serializer\Normalizer;
4
5
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
6
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
7
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
8
9
/**
10
 * Normalizes and denormalizes xAPI statement timestamps.
11
 *
12
 * @author Christian Flothmann <[email protected]>
13
 */
14
final class TimestampNormalizer implements DenormalizerInterface, NormalizerInterface
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 1
    public function denormalize($data, $class, $format = null, array $context = array())
20
    {
21 1
        return new \DateTime($data);
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 50
    public function supportsDenormalization($data, $type, $format = null)
28
    {
29 50
        return '\DateTime' === $type;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 1
    public function normalize($object, $format = null, array $context = array())
36
    {
37 1
        if (!($object instanceof \DateTime || $object instanceof \DateTimeInterface)) {
38
            throw new InvalidArgumentException(sprintf('Expected \DateTime object or object implementing \DateTimeInterface (got "%s").', is_object($object) ? get_class($object) : gettype($object)));
39
        }
40
41 1
        return $object->format('c');
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 49
    public function supportsNormalization($data, $format = null)
48
    {
49 49
        return $data instanceof \DateTime || $data instanceof \DateTimeInterface;
50
    }
51
}
52