Skip to content

Commit 1142b2f

Browse files
committed
allow to deserialize a JSON encoded collection of statements
1 parent 4e7794f commit 1142b2f

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

Serializer/StatementSerializer.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,16 @@ public function deserializeStatement($data)
5858
'json'
5959
);
6060
}
61+
62+
/**
63+
* {@inheritDoc}
64+
*/
65+
public function deserializeStatements($data)
66+
{
67+
return $this->serializer->deserialize(
68+
$data,
69+
'array<Xabbuh\XApi\Storage\MongoDB\Document\Statement>',
70+
'json'
71+
);
72+
}
6173
}

Serializer/StatementSerializerInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,13 @@ public function serializeStatements(array $statements);
4646
* @return StatementInterface The parsed statement
4747
*/
4848
public function deserializeStatement($data);
49+
50+
/**
51+
* Parses a serialized collection of statements.
52+
*
53+
* @param string $data The serialized statements
54+
*
55+
* @return StatementInterface[] The parsed statements
56+
*/
57+
public function deserializeStatements($data);
4958
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[
2+
{
3+
"id": "12345678-1234-5678-8234-567812345678",
4+
"actor": {
5+
"mbox": "mailto:[email protected]",
6+
"objectType": "Agent"
7+
},
8+
"verb": {
9+
"id": "http://adlnet.gov/expapi/verbs/created",
10+
"display": {
11+
"en-US": "created"
12+
}
13+
},
14+
"object": {
15+
"id": "http://example.adlnet.gov/xapi/example/activity",
16+
"objectType": "Activity"
17+
}
18+
},
19+
{
20+
"id": "12345678-1234-5678-8234-567812345679",
21+
"actor": {
22+
"mbox": "mailto:[email protected]",
23+
"objectType": "Agent"
24+
},
25+
"verb": {
26+
"id": "http://adlnet.gov/expapi/verbs/created",
27+
"display": {
28+
"en-US": "created"
29+
}
30+
},
31+
"object": {
32+
"id": "http://example.adlnet.gov/xapi/example/activity",
33+
"objectType": "Activity"
34+
}
35+
}
36+
]

Tests/Serializer/StatementSerializerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,25 @@ public function testDeserializeWithResult()
154154
$this->assertTrue($result->getCompletion());
155155
}
156156

157+
public function testDeserializeStatementCollection()
158+
{
159+
/** @var \Xabbuh\XApi\Common\Model\Statement[] $statements */
160+
$statements = $this->statementSerializer->deserializeStatements(
161+
$this->loadFixture('statement_collection')
162+
);
163+
164+
$this->assertTrue(is_array($statements));
165+
$this->assertCount(2, $statements);
166+
$this->assertEquals(
167+
'12345678-1234-5678-8234-567812345678',
168+
$statements[0]->getId()
169+
);
170+
$this->assertEquals(
171+
'12345678-1234-5678-8234-567812345679',
172+
$statements[1]->getId()
173+
);
174+
}
175+
157176
public function testSerializeMinimalStatement()
158177
{
159178
$statement = new Statement();

0 commit comments

Comments
 (0)