Skip to content

Commit 2bdceac

Browse files
committed
denormalize and normalize statement timestamps
This finishes pull request #9 by adding a normalizer that is able to deal with ISO 8601 formatted dates.
1 parent 3708915 commit 2bdceac

File tree

6 files changed

+127
-6
lines changed

6 files changed

+127
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
CHANGELOG
22
=========
33

4-
0.2.x (next version)
4+
0.2.2
55
-----
66

7-
* Added support for (de)serializing 'created' and 'stored' Statement properties.
7+
* Added support for (de)serializing a statement's `timestamp` and `stored`
8+
properties.
89

910
0.2.1
1011
-----

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
"symfony/serializer": "~2.8|~3.0"
1818
},
1919
"require-dev": {
20-
"php-xapi/json-test-fixtures": "^0.2.0",
21-
"php-xapi/test-fixtures": "^0.3.0",
20+
"php-xapi/json-test-fixtures": "^0.2.2",
21+
"php-xapi/test-fixtures": "^0.4.0",
2222
"phpspec/phpspec": "~2.0"
2323
},
2424
"conflict": {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace spec\Xabbuh\XApi\Serializer\Normalizer;
4+
5+
use PhpSpec\ObjectBehavior;
6+
7+
class TimestampNormalizerSpec extends ObjectBehavior
8+
{
9+
function it_is_a_normalizer()
10+
{
11+
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
12+
}
13+
14+
function it_can_normalize_datetime_objects()
15+
{
16+
$this->supportsNormalization(new \DateTime())->shouldBe(true);
17+
}
18+
19+
function it_cannot_normalize_datetime_like_string()
20+
{
21+
$this->supportsNormalization('2004-02-12T15:19:21+00:00')->shouldBe(false);
22+
}
23+
24+
function it_normalizes_datetime_objects_as_iso_8601_formatted_strings()
25+
{
26+
$date = new \DateTime();
27+
$date->setTimezone(new \DateTimeZone('UTC'));
28+
$date->setDate(2004, 2, 12);
29+
$date->setTime(15, 19, 21);
30+
31+
$this->normalize($date)->shouldReturn('2004-02-12T15:19:21+00:00');
32+
}
33+
34+
function it_throws_an_exception_when_data_other_than_datetime_objects_are_passed()
35+
{
36+
$this->shouldThrow('Symfony\Component\Serializer\Exception\InvalidArgumentException')->during('normalize', array('2004-02-12T15:19:21+00:00'));
37+
}
38+
39+
function it_is_a_denormalizer()
40+
{
41+
$this->shouldHaveType('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');
42+
}
43+
44+
function it_can_denormalize_to_datetime_objects()
45+
{
46+
$this->supportsDenormalization('2004-02-12T15:19:21+00:00', 'DateTime')->shouldBe(true);
47+
}
48+
49+
function it_denormalizes_iso_8601_formatted_strings_to_datetime_objects()
50+
{
51+
$date = $this->denormalize('2004-02-12T15:19:21+00:00', 'DateTime');
52+
53+
$date->getTimezone()->shouldBeLike(new \DateTimeZone('UTC'));
54+
$date->format('Y-m-d H:i:s')->shouldReturn('2004-02-12 15:19:21');
55+
}
56+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the xAPI package.
5+
*
6+
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
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 <christian.flothmann@xabbuh.de>
22+
*/
23+
final class TimestampNormalizer implements DenormalizerInterface, NormalizerInterface
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function denormalize($data, $class, $format = null, array $context = array())
29+
{
30+
return new \DateTime($data);
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function supportsDenormalization($data, $type, $format = null)
37+
{
38+
return 'DateTime' === $type;
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function normalize($object, $format = null, array $context = array())
45+
{
46+
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+
return $object->format('c');
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function supportsNormalization($data, $format = null)
57+
{
58+
return $data instanceof \DateTime || $data instanceof \DateTimeInterface;
59+
}
60+
}

src/Serializer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\Serializer\Encoder\JsonEncoder;
1515
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
16-
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
1716
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
1817
use Symfony\Component\Serializer\SerializerInterface;
1918
use Symfony\Component\Serializer\Serializer as SymfonySerializer;
@@ -24,6 +23,7 @@
2423
use Xabbuh\XApi\Serializer\Normalizer\ResultNormalizer;
2524
use Xabbuh\XApi\Serializer\Normalizer\StatementNormalizer;
2625
use Xabbuh\XApi\Serializer\Normalizer\StatementResultNormalizer;
26+
use Xabbuh\XApi\Serializer\Normalizer\TimestampNormalizer;
2727

2828
/**
2929
* Entry point to set up the {@link \Symfony\Component\Serializer\Serializer Symfony Serializer component}
@@ -47,10 +47,10 @@ public static function createSerializer()
4747
new ResultNormalizer(),
4848
new StatementNormalizer(),
4949
new StatementResultNormalizer(),
50+
new TimestampNormalizer(),
5051
new ArrayDenormalizer(),
5152
new FilterNullValueNormalizer(new PropertyNormalizer()),
5253
new PropertyNormalizer(),
53-
new DateTimeNormalizer()
5454
);
5555
$encoders = array(
5656
new JsonEncoder(),

tests/StatementSerializerTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public function statementProvider()
6565
StatementJsonFixtures::getMinimalStatement(),
6666
StatementFixtures::getMinimalStatement(),
6767
),
68+
'typical-statement' => array(
69+
StatementJsonFixtures::getTypicalStatement(),
70+
StatementFixtures::getTypicalStatement(),
71+
),
6872
'statement-reference' => array(
6973
StatementJsonFixtures::getStatementWithStatementRef(),
7074
StatementFixtures::getStatementWithStatementRef(),

0 commit comments

Comments
 (0)