Skip to content

Commit 9be9539

Browse files
committed
Move the handling of serializations to dedicated classes. Make these serializers available through a registry.
1 parent bf860d7 commit 9be9539

31 files changed

+1152
-565
lines changed

Serializer/ActorSerializer.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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;
13+
14+
use JMS\Serializer\SerializerInterface;
15+
use Xabbuh\XApi\Common\Model\ActorInterface;
16+
17+
/**
18+
* Serialize and deserialize {@link ActorInterface actors}.
19+
*
20+
* @author Christian Flothmann <[email protected]>
21+
*/
22+
class ActorSerializer implements ActorSerializerInterface
23+
{
24+
/**
25+
* @var SerializerInterface
26+
*/
27+
private $serializer;
28+
29+
public function __construct(SerializerInterface $serializer)
30+
{
31+
$this->serializer = $serializer;
32+
}
33+
34+
/**
35+
* {@inheritDoc}
36+
*/
37+
public function serializeActor(ActorInterface $actor)
38+
{
39+
return $this->serializer->serialize($actor, 'json');
40+
}
41+
42+
/**
43+
* {@inheritDoc}
44+
*/
45+
public function deserializeActor($data)
46+
{
47+
return $this->serializer->deserialize($data, 'Xabbuh\XApi\Common\Model\Actor', 'json');
48+
}
49+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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;
13+
14+
use Xabbuh\XApi\Common\Model\ActorInterface;
15+
16+
/**
17+
* Serialize and deserialize {@link ActorInterface actors}.
18+
*
19+
* @author Christian Flothmann <[email protected]>
20+
*/
21+
interface ActorSerializerInterface
22+
{
23+
/**
24+
* Serializes an actor into a JSON encoded string.
25+
*
26+
* @param ActorInterface $actor The actor to serialize
27+
*
28+
* @return string The serialized actor
29+
*/
30+
public function serializeActor(ActorInterface $actor);
31+
32+
/**
33+
* Parses a serialized actor.
34+
*
35+
* @param string $data The serialized actor
36+
*
37+
* @return ActorInterface The parsed actor
38+
*/
39+
public function deserializeActor($data);
40+
}

Serializer/DocumentSerializer.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\Serializer;
13+
14+
use JMS\Serializer\SerializerInterface;
15+
use Xabbuh\XApi\Common\Model\DocumentInterface;
16+
17+
/**
18+
* Serialize and deserialize {@link DocumentInterface documents}.
19+
*
20+
* @author Christian Flothmann <[email protected]>
21+
*/
22+
class DocumentSerializer implements DocumentSerializerInterface
23+
{
24+
/**
25+
* @var SerializerInterface
26+
*/
27+
private $serializer;
28+
29+
public function __construct(SerializerInterface $serializer)
30+
{
31+
$this->serializer = $serializer;
32+
}
33+
34+
/**
35+
* {@inheritDoc}
36+
*/
37+
public function serializeDocument(DocumentInterface $document)
38+
{
39+
return $this->serializer->serialize($document, 'json');
40+
}
41+
42+
/**
43+
* {@inheritDoc}
44+
*/
45+
public function deserializeActivityProfileDocument($data)
46+
{
47+
return $this->serializer->deserialize($data, 'Xabbuh\XApi\Common\Model\ActivityProfileDocument', 'json');
48+
}
49+
50+
/**
51+
* {@inheritDoc}
52+
*/
53+
public function deserializeAgentProfileDocument($data)
54+
{
55+
return $this->serializer->deserialize($data, 'Xabbuh\XApi\Common\Model\AgentProfileDocument', 'json');
56+
}
57+
58+
/**
59+
* {@inheritDoc}
60+
*/
61+
public function deserializeStateDocument($data)
62+
{
63+
return $this->serializer->deserialize($data, 'Xabbuh\XApi\Common\Model\StateDocument', 'json');
64+
}
65+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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;
13+
14+
use Xabbuh\XApi\Common\Model\DocumentInterface;
15+
16+
/**
17+
* Serialize and deserialize {@link DocumentInterface documents}.
18+
*
19+
* @author Christian Flothmann <[email protected]>
20+
*/
21+
interface DocumentSerializerInterface
22+
{
23+
/**
24+
* Serializes a document into a JSON encoded string.
25+
*
26+
* @param DocumentInterface $document The document to serialize
27+
*
28+
* @return string The serialized document
29+
*/
30+
public function serializeDocument(DocumentInterface $document);
31+
32+
/**
33+
* Parses a serialized activity profile document.
34+
*
35+
* @param string $data The serialized activity profile document
36+
*
37+
* @return DocumentInterface The parsed activity profile document
38+
*/
39+
public function deserializeActivityProfileDocument($data);
40+
41+
/**
42+
* Parses a serialized agent profile document.
43+
*
44+
* @param string $data The serialized agent profile document
45+
*
46+
* @return DocumentInterface The parsed agent profile document
47+
*/
48+
public function deserializeAgentProfileDocument($data);
49+
50+
/**
51+
* Parses a serialized state document.
52+
*
53+
* @param string $data The serialized state document
54+
*
55+
* @return DocumentInterface The parsed state document
56+
*/
57+
public function deserializeStateDocument($data);
58+
}

Serializer/SerializerRegistry.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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;
13+
14+
/**
15+
* Registry containing all the serializers.
16+
*
17+
* @author Christian Flothmann <[email protected]>
18+
*/
19+
class SerializerRegistry implements SerializerRegistryInterface
20+
{
21+
/**
22+
* @var StatementSerializerInterface The statement serializer
23+
*/
24+
private $statementSerializer;
25+
26+
/**
27+
* @var StatementResultSerializerInterface The statement result serializer
28+
*/
29+
private $statementResultSerializer;
30+
31+
/**
32+
* @var ActorSerializerInterface The actor serializer
33+
*/
34+
private $actorSerializer;
35+
36+
/**
37+
* @var DocumentSerializerInterface The document serializer
38+
*/
39+
private $documentSerializer;
40+
41+
/**
42+
* {@inheritDoc}
43+
*/
44+
public function setStatementSerializer(StatementSerializerInterface $serializer)
45+
{
46+
$this->statementSerializer = $serializer;
47+
}
48+
49+
/**
50+
* {@inheritDoc}
51+
*/
52+
public function getStatementSerializer()
53+
{
54+
return $this->statementSerializer;
55+
}
56+
57+
/**
58+
* {@inheritDoc}
59+
*/
60+
public function setStatementResultSerializer(StatementResultSerializerInterface $serializer)
61+
{
62+
$this->statementResultSerializer = $serializer;
63+
}
64+
65+
/**
66+
* {@inheritDoc}
67+
*/
68+
public function getStatementResultSerializer()
69+
{
70+
return $this->statementResultSerializer;
71+
}
72+
73+
/**
74+
* {@inheritDoc}
75+
*/
76+
public function setActorSerializer(ActorSerializerInterface $serializer)
77+
{
78+
$this->actorSerializer = $serializer;
79+
}
80+
81+
/**
82+
* {@inheritDoc}
83+
*/
84+
public function getActorSerializer()
85+
{
86+
return $this->actorSerializer;
87+
}
88+
89+
/**
90+
* {@inheritDoc}
91+
*/
92+
public function setDocumentSerializer(DocumentSerializerInterface $serializer)
93+
{
94+
$this->documentSerializer = $serializer;
95+
}
96+
97+
/**
98+
* {@inheritDoc}
99+
*/
100+
public function getDocumentSerializer()
101+
{
102+
return $this->documentSerializer;
103+
}
104+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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;
13+
14+
/**
15+
* Registry containing all the serializers.
16+
*
17+
* @author Christian Flothmann <[email protected]>
18+
*/
19+
interface SerializerRegistryInterface
20+
{
21+
/**
22+
* Sets the {@link StatementSerializerInterface statement serializer}.
23+
*
24+
* @param StatementSerializerInterface $serializer The serializer
25+
*/
26+
public function setStatementSerializer(StatementSerializerInterface $serializer);
27+
28+
/**
29+
* Returns the {@link StatementSerializerInterface statement serializer}.
30+
*
31+
* @return StatementSerializerInterface The serializer
32+
*/
33+
public function getStatementSerializer();
34+
35+
/**
36+
* Sets the {@link StatementResultSerializerInterface statement result serializer}.
37+
*
38+
* @param StatementResultSerializerInterface $serializer The serializer
39+
*/
40+
public function setStatementResultSerializer(StatementResultSerializerInterface $serializer);
41+
42+
/**
43+
* Returns the {@link StatementResultSerializerInterface statement result serializer}.
44+
*
45+
* @return StatementResultSerializerInterface The serializer
46+
*/
47+
public function getStatementResultSerializer();
48+
49+
/**
50+
* Sets the {@link ActorSerializerInterface actor serializer}.
51+
*
52+
* @param ActorSerializerInterface $serializer The serializer
53+
*/
54+
public function setActorSerializer(ActorSerializerInterface $serializer);
55+
56+
/**
57+
* Returns the {@link ActorSerializerInterface actor serializer}.
58+
*
59+
* @return ActorSerializerInterface The serializer
60+
*/
61+
public function getActorSerializer();
62+
63+
/**
64+
* Sets the {@link DocumentSerializerInterface document serializer}.
65+
*
66+
* @param DocumentSerializerInterface $serializer The serializer
67+
*/
68+
public function setDocumentSerializer(DocumentSerializerInterface $serializer);
69+
70+
/**
71+
* Returns the {@link DocumentSerializerInterface document serializer}.
72+
*
73+
* @return DocumentSerializerInterface The serializer
74+
*/
75+
public function getDocumentSerializer();
76+
}

0 commit comments

Comments
 (0)