Skip to content

Commit c4614fc

Browse files
committed
adding the xAPI Document model
1 parent 951e61b commit c4614fc

File tree

12 files changed

+397
-11
lines changed

12 files changed

+397
-11
lines changed

Model/Document.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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\Common\Model;
13+
14+
/**
15+
* An xAPI document.
16+
*
17+
* @author Christian Flothmann <[email protected]>
18+
*/
19+
class Document implements DocumentInterface
20+
{
21+
/**
22+
* @var array The data
23+
*/
24+
protected $data = array();
25+
26+
/**
27+
* {@inheritDoc}
28+
*/
29+
public function offsetExists($offset)
30+
{
31+
return isset($this->data[$offset]);
32+
}
33+
34+
/**
35+
* {@inheritDoc}
36+
*/
37+
public function offsetGet($offset)
38+
{
39+
return $this->data[$offset];
40+
}
41+
42+
/**
43+
* {@inheritDoc}
44+
*/
45+
public function offsetSet($offset, $value)
46+
{
47+
$this->data[$offset] = $value;
48+
}
49+
50+
/**
51+
* {@inheritDoc}
52+
*/
53+
public function offsetUnset($offset)
54+
{
55+
unset($this->data[$offset]);
56+
}
57+
58+
/**
59+
* {@inheritDoc}
60+
*/
61+
public function getData()
62+
{
63+
return $this->data;
64+
}
65+
}

Model/DocumentInterface.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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\Common\Model;
13+
14+
/**
15+
* An xAPI document.
16+
*
17+
* @author Christian Flothmann <[email protected]>
18+
*/
19+
interface DocumentInterface extends \ArrayAccess
20+
{
21+
/**
22+
* Returns the document's data.
23+
*
24+
* @return array The data
25+
*/
26+
public function getData();
27+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\Common\Serializer\Event;
13+
14+
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
15+
use JMS\Serializer\EventDispatcher\PreDeserializeEvent;
16+
17+
/**
18+
* Wraps a JSON encoded xAPI document into a Document instance.
19+
*
20+
* @author Christian Flothmann <[email protected]>
21+
*/
22+
class DocumentDataWrapper implements EventSubscriberInterface
23+
{
24+
/**
25+
* {@inheritDoc}
26+
*/
27+
public static function getSubscribedEvents()
28+
{
29+
return array(
30+
array(
31+
'event' => 'serializer.pre_deserialize',
32+
'method' => 'wrapData',
33+
),
34+
);
35+
}
36+
37+
public function wrapData(PreDeserializeEvent $event)
38+
{
39+
$type = $event->getType();
40+
41+
if ('Xabbuh\XApi\Common\Model\Document' === $type['name']) {
42+
$data = $event->getData();
43+
$event->setData(array('data' => $data));
44+
}
45+
}
46+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\Common\Serializer\Handler;
13+
14+
use JMS\Serializer\Context;
15+
use JMS\Serializer\GraphNavigator;
16+
use JMS\Serializer\Handler\SubscribingHandlerInterface;
17+
use JMS\Serializer\JsonSerializationVisitor;
18+
use Xabbuh\XApi\Common\Model\Document;
19+
20+
/**
21+
* Unwraps the data of an xAPI document during the serialization process.
22+
*
23+
* @author Christian Flothmann <[email protected]>
24+
*/
25+
class DocumentDataUnwrapper implements SubscribingHandlerInterface
26+
{
27+
/**
28+
* {@inheritDoc}
29+
*/
30+
public static function getSubscribingMethods()
31+
{
32+
return array(array(
33+
'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
34+
'format' => 'json',
35+
'type' => 'Xabbuh\XApi\Common\Model\Document',
36+
'method' => 'unwrapData',
37+
));
38+
}
39+
40+
public function unwrapData(JsonSerializationVisitor $visitor, Document $document, array $type, Context $context)
41+
{
42+
$visitor->setRoot($document->getData());
43+
}
44+
}

Serializer/Serializer.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
namespace Xabbuh\XApi\Common\Serializer;
1313

1414
use JMS\Serializer\EventDispatcher\EventDispatcher;
15+
use JMS\Serializer\Handler\HandlerRegistryInterface;
1516
use JMS\Serializer\SerializerBuilder;
1617
use Xabbuh\XApi\Common\Serializer\Event\ActorEventSubscriber;
18+
use Xabbuh\XApi\Common\Serializer\Event\DocumentDataWrapper;
1719
use Xabbuh\XApi\Common\Serializer\Event\ObjectEventSubscriber;
1820
use Xabbuh\XApi\Common\Serializer\Event\SetSerializedTypeEventSubscriber;
21+
use Xabbuh\XApi\Common\Serializer\Handler\DocumentDataUnwrapper;
1922

2023
/**
2124
* Entry point to setup the {@link \JMS\Serializer\Serializer JMS Serializer}
@@ -54,11 +57,24 @@ public static function registerXApiEventSubscriber(SerializerBuilder $builder)
5457
{
5558
$builder->configureListeners(function (EventDispatcher $dispatcher) {
5659
$dispatcher->addSubscriber(new ActorEventSubscriber());
60+
$dispatcher->addSubscriber(new DocumentDataWrapper());
5761
$dispatcher->addSubscriber(new ObjectEventSubscriber());
5862
$dispatcher->addSubscriber(new SetSerializedTypeEventSubscriber());
5963
});
6064
}
6165

66+
/**
67+
* Registers handlers for the xAPI models on a SerializerBuilder.
68+
*
69+
* @param SerializerBuilder $builder The SerializerBuilder
70+
*/
71+
public static function registerXApiHandler(SerializerBuilder $builder)
72+
{
73+
$builder->configureHandlers(function (HandlerRegistryInterface $handlerRegistry) {
74+
$handlerRegistry->registerSubscribingHandler(new DocumentDataUnwrapper());
75+
});
76+
}
77+
6278
/**
6379
* Registers serialization metadata and event subscribers for the xAPI
6480
* models on a SerializerBuilder.
@@ -69,6 +85,7 @@ public static function registerXApi(SerializerBuilder $builder)
6985
{
7086
static::registerXApiMetadata($builder);
7187
static::registerXApiEventSubscriber($builder);
88+
static::registerXApiHandler($builder);
7289
}
7390

7491
/**
@@ -80,8 +97,7 @@ public static function registerXApi(SerializerBuilder $builder)
8097
public static function createSerializerBuilder()
8198
{
8299
$builder = SerializerBuilder::create();
83-
static::registerXApiMetadata($builder);
84-
static::registerXApiEventSubscriber($builder);
100+
static::registerXApi($builder);
85101

86102
return $builder;
87103
}

Tests/Model/DocumentTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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\Common\Tests\Model;
13+
14+
use Xabbuh\XApi\Common\Model\Document;
15+
16+
/**
17+
* @author Christian Flothmann <[email protected]>
18+
*/
19+
class DocumentTest extends ModelTest
20+
{
21+
public function testArrayAccessMethods()
22+
{
23+
$document = new Document();
24+
25+
$this->assertFalse(isset($document['x']));
26+
27+
$document['x'] = 'foo';
28+
29+
$this->assertTrue(isset($document['x']));
30+
$this->assertEquals('foo', $document['x']);
31+
32+
$document['x'] = 'bar';
33+
34+
$this->assertEquals('bar', $document['x']);
35+
36+
unset($document['x']);
37+
38+
$this->assertFalse(isset($document['x']));
39+
}
40+
41+
public function testGetData()
42+
{
43+
$document = new Document();
44+
$document['x'] = 'foo';
45+
$document['y'] = 'bar';
46+
47+
$this->assertEquals(array('x' => 'foo', 'y' => 'bar'), $document->getData());
48+
}
49+
50+
public function testDeserializeDocument()
51+
{
52+
/** @var \Xabbuh\XApi\Common\Model\Document $document */
53+
$document = $this->deserialize($this->loadFixture('document'));
54+
55+
$this->assertEquals('foo', $document['x']);
56+
$this->assertEquals('bar', $document['y']);
57+
}
58+
59+
public function testSerializeDocument()
60+
{
61+
$document = new Document();
62+
$document['x'] = 'foo';
63+
$document['y'] = 'bar';
64+
65+
$this->serializeAndValidateData($this->loadFixture('document'), $document);
66+
}
67+
}

Tests/Model/ModelTest.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
namespace Xabbuh\XApi\Common\Tests\Model;
1313

1414
use JMS\Serializer\EventDispatcher\EventDispatcher;
15+
use JMS\Serializer\Handler\HandlerRegistryInterface;
1516
use JMS\Serializer\SerializerBuilder;
1617
use Symfony\Component\Validator\Validation;
1718
use Xabbuh\XApi\Common\Serializer\Event\ActorEventSubscriber;
19+
use Xabbuh\XApi\Common\Serializer\Event\DocumentDataWrapper;
1820
use Xabbuh\XApi\Common\Serializer\Event\ObjectEventSubscriber;
1921
use Xabbuh\XApi\Common\Serializer\Event\SetSerializedTypeEventSubscriber;
22+
use Xabbuh\XApi\Common\Serializer\Handler\DocumentDataUnwrapper;
2023

2124
/**
2225
* @author Christian Flothmann <[email protected]>
@@ -40,21 +43,22 @@ abstract class ModelTest extends \PHPUnit_Framework_TestCase
4043

4144
protected function setUp()
4245
{
43-
$this->subscribers[] = new ActorEventSubscriber();
44-
$this->subscribers[] = new ObjectEventSubscriber();
45-
$this->subscribers[] = new SetSerializedTypeEventSubscriber();
46-
$subscribers = $this->subscribers;
47-
4846
$builder = SerializerBuilder::create();
4947
$builder->addMetadataDir(
5048
__DIR__.'/../../metadata/serializer',
5149
'Xabbuh\XApi\Common\Model'
5250
);
5351
$builder->configureListeners(
54-
function (EventDispatcher $dispatcher) use ($subscribers) {
55-
foreach ($subscribers as $subscriber) {
56-
$dispatcher->addSubscriber($subscriber);
57-
}
52+
function (EventDispatcher $dispatcher) {
53+
$dispatcher->addSubscriber(new ActorEventSubscriber());
54+
$dispatcher->addSubscriber(new DocumentDataWrapper());
55+
$dispatcher->addSubscriber(new ObjectEventSubscriber());
56+
$dispatcher->addSubscriber(new SetSerializedTypeEventSubscriber());
57+
}
58+
);
59+
$builder->configureHandlers(
60+
function (HandlerRegistryInterface $registry) {
61+
$registry->registerSubscribingHandler(new DocumentDataUnwrapper());
5862
}
5963
);
6064
$this->serializer = $builder->build();

Tests/Model/fixtures/document.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"x": "foo",
3+
"y": "bar"
4+
}

0 commit comments

Comments
 (0)