Skip to content

Commit 19de794

Browse files
committed
take attachment contents into account
1 parent 2ff7e38 commit 19de794

File tree

9 files changed

+96
-17
lines changed

9 files changed

+96
-17
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
CHANGELOG
22
=========
33

4+
2.0.0
5+
-----
6+
7+
Added support for deserializing raw attachment contents. In order to achieve this
8+
an optional `$attachments` argument has been added to the `deserializeStatement()`
9+
and `deserializeStatements()` methods of the `StatementSerializer` class.
10+
11+
When being passed, this argument must be an array mapping SHA-2 hashes to an
12+
array which in turn maps the `type` and `content` keys to the attachment's
13+
content type and raw content data respectively.
14+
415
1.0.0
516
-----
617

UPGRADE.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
UPGRADE
2+
=======
3+
4+
Upgrading from 1.x to 2.0
5+
-------------------------
6+
7+
Added support for deserializing raw attachment contents. In order to achieve this
8+
an optional `$attachments` argument has been added to the `deserializeStatement()`
9+
and `deserializeStatements()` methods of the `StatementSerializer` class as well
10+
as to the `StatementResultSerializer::deserializeStatementResult()`.
11+
12+
When being passed, this argument must be an array mapping SHA-2 hashes to an
13+
array which in turn maps the `type` and `content` keys to the attachment's
14+
content type and raw content data respectively.

composer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@
1414
"require": {
1515
"php": ">=5.3.0",
1616
"php-xapi/model": "^1.0",
17-
"php-xapi/serializer": "^1.0",
17+
"php-xapi/serializer": "^2.0",
1818
"symfony/serializer": "~2.8|~3.0"
1919
},
2020
"require-dev": {
21-
"php-xapi/json-test-fixtures": "^1.0",
21+
"php-xapi/json-test-fixtures": "^2.0",
2222
"php-xapi/test-fixtures": "^1.0",
2323
"phpspec/phpspec": "~2.3"
2424
},
25+
"minimum-stability": "dev",
2526
"provide": {
26-
"php-xapi/serializer-implementation": "1.0"
27+
"php-xapi/serializer-implementation": "2.0"
2728
},
2829
"autoload": {
2930
"psr-4": {
@@ -38,7 +39,7 @@
3839
},
3940
"extra": {
4041
"branch-alias": {
41-
"dev-master": "1.0.x-dev"
42+
"dev-master": "2.0.x-dev"
4243
}
4344
}
4445
}

spec/Normalizer/AttachmentNormalizerSpec.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ function it_supports_normalizing_attachment_objects()
2121
'text/plain',
2222
18,
2323
'bd1a58265d96a3d1981710dab8b1e1ed04a8d7557ea53ab0cf7b44c04fd01545',
24-
LanguageMap::create(array('en-US' => 'Text attachment'))
24+
LanguageMap::create(array('en-US' => 'Text attachment')),
25+
null,
26+
null,
27+
'some text content'
2528
);
2629

2730
$this->supportsNormalization($attachment)->shouldReturn(true);

src/Normalizer/AttachmentNormalizer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
6363
$display = $this->denormalizeData($data['display'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
6464
$description = null;
6565
$fileUrl = null;
66+
$content = null;
6667

6768
if (isset($data['description'])) {
6869
$description = $this->denormalizeData($data['description'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
@@ -72,7 +73,11 @@ public function denormalize($data, $class, $format = null, array $context = arra
7273
$fileUrl = IRL::fromString($data['fileUrl']);
7374
}
7475

75-
return new Attachment(IRI::fromString($data['usageType']), $data['contentType'], $data['length'], $data['sha2'], $display, $description, $fileUrl);
76+
if (isset($context['xapi_attachments'][$data['sha2']])) {
77+
$content = $context['xapi_attachments'][$data['sha2']]['content'];
78+
}
79+
80+
return new Attachment(IRI::fromString($data['usageType']), $data['contentType'], $data['length'], $data['sha2'], $display, $description, $fileUrl, $content);
7681
}
7782

7883
public function supportsDenormalization($data, $type, $format = null)

src/Normalizer/StatementResultNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function supportsNormalization($data, $format = null)
5858
*/
5959
public function denormalize($data, $class, $format = null, array $context = array())
6060
{
61-
$statements = $this->denormalizeData($data['statements'], 'Xabbuh\XApi\Model\Statement[]');
61+
$statements = $this->denormalizeData($data['statements'], 'Xabbuh\XApi\Model\Statement[]', $format, $context);
6262
$moreUrlPath = null;
6363

6464
if (isset($data['more'])) {

src/StatementResultSerializer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,15 @@ public function serializeStatementResult(StatementResult $statementResult)
4343
/**
4444
* {@inheritDoc}
4545
*/
46-
public function deserializeStatementResult($data)
46+
public function deserializeStatementResult($data, array $attachments = array())
4747
{
4848
return $this->serializer->deserialize(
4949
$data,
5050
'Xabbuh\XApi\Model\StatementResult',
51-
'json'
51+
'json',
52+
array(
53+
'xapi_attachments' => $attachments,
54+
)
5255
);
5356
}
5457
}

src/StatementSerializer.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,30 @@ public function serializeStatements(array $statements)
5151
/**
5252
* {@inheritDoc}
5353
*/
54-
public function deserializeStatement($data)
54+
public function deserializeStatement($data, array $attachments = array())
5555
{
5656
return $this->serializer->deserialize(
5757
$data,
5858
'Xabbuh\XApi\Model\Statement',
59-
'json'
59+
'json',
60+
array(
61+
'xapi_attachments' => $attachments,
62+
)
6063
);
6164
}
6265

6366
/**
6467
* {@inheritDoc}
6568
*/
66-
public function deserializeStatements($data)
69+
public function deserializeStatements($data, array $attachments = array())
6770
{
6871
return $this->serializer->deserialize(
6972
$data,
7073
'Xabbuh\XApi\Model\Statement[]',
71-
'json'
74+
'json',
75+
array(
76+
'xapi_attachments' => $attachments,
77+
)
7278
);
7379
}
7480
}

tests/SerializerTest.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,23 +134,59 @@ public function testSerializeAttachment(Attachment $attachment, $expectedJson)
134134

135135
public function serializeAttachmentData()
136136
{
137-
return $this->buildSerializeTestCases('Attachment');
137+
$tests = array();
138+
139+
foreach (get_class_methods('Xabbuh\XApi\DataFixtures\AttachmentFixtures') as $method) {
140+
if (false !== strpos($method, 'ForQuery')) {
141+
continue;
142+
}
143+
144+
$jsonFixture = json_decode(call_user_func(array('XApi\Fixtures\Json\AttachmentJsonFixtures', $method)));
145+
146+
$tests[$method] = array(
147+
call_user_func(array('Xabbuh\XApi\DataFixtures\AttachmentFixtures', $method)),
148+
json_encode($jsonFixture->metadata),
149+
);
150+
}
151+
152+
return $tests;
138153
}
139154

140155
/**
141156
* @dataProvider deserializeAttachmentData
142157
*/
143-
public function testDeserializeAttachment($json, Attachment $expectedAttachment)
158+
public function testDeserializeAttachment($json, $content, Attachment $expectedAttachment)
144159
{
145-
$attachment = $this->serializer->deserialize($json, 'Xabbuh\XApi\Model\Attachment', 'json');
160+
$context = array();
161+
162+
if (null !== $content) {
163+
$context['xapi_attachments'] = array(
164+
hash('sha256', $content) => array(
165+
'content' => $content,
166+
)
167+
);
168+
}
169+
170+
$attachment = $this->serializer->deserialize($json, 'Xabbuh\XApi\Model\Attachment', 'json', $context);
146171

147172
$this->assertInstanceOf('Xabbuh\XApi\Model\Attachment', $attachment);
148173
$this->assertTrue($expectedAttachment->equals($attachment), 'Deserialized attachment has the expected properties');
149174
}
150175

151176
public function deserializeAttachmentData()
152177
{
153-
return $this->buildDeserializeTestCases('Attachment');
178+
$tests = array();
179+
180+
foreach (get_class_methods('XApi\Fixtures\Json\AttachmentJsonFixtures') as $method) {
181+
$jsonFixture = json_decode(call_user_func(array('XApi\Fixtures\Json\AttachmentJsonFixtures', $method)));
182+
$tests[$method] = array(
183+
json_encode($jsonFixture->metadata),
184+
isset($jsonFixture->content) ? $jsonFixture->content : null,
185+
call_user_func(array('Xabbuh\XApi\DataFixtures\AttachmentFixtures', $method)),
186+
);
187+
}
188+
189+
return $tests;
154190
}
155191

156192
/**

0 commit comments

Comments
 (0)