Skip to content

Commit 6cbc661

Browse files
committed
compatibility with v0.2 model classes
1 parent 4e0b951 commit 6cbc661

5 files changed

Lines changed: 61 additions & 34 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG
44
0.2.0
55
-----
66

7+
* made the package compatible with version 0.2 of the `php-xapi/model` package
8+
79
* replaced the JMS Serializer with the Symfony Serializer component
810

911
0.1.1

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
],
1414
"require": {
1515
"php": ">=5.3.0",
16-
"php-xapi/model": "^0.1.0",
16+
"php-xapi/model": "^0.2.0",
1717
"symfony/serializer": "~2.8|~3.0"
1818
},
1919
"require-dev": {
20-
"php-xapi/test-fixtures": "^0.1.0"
20+
"php-xapi/test-fixtures": "^0.2.0"
2121
},
2222
"conflict": {
2323
"xabbuh/xapi-serializer": "*"
@@ -34,7 +34,7 @@
3434
},
3535
"extra": {
3636
"branch-alias": {
37-
"dev-master": "0.1.x-dev"
37+
"dev-master": "0.2.x-dev"
3838
}
3939
}
4040
}

src/Normalizer/ActorNormalizer.php

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
namespace Xabbuh\XApi\Serializer\Normalizer;
1313

14-
use Xabbuh\XApi\Model\Account;
1514
use Xabbuh\XApi\Model\Actor;
1615
use Xabbuh\XApi\Model\Agent;
1716
use Xabbuh\XApi\Model\Group;
17+
use Xabbuh\XApi\Model\InverseFunctionalIdentifier;
1818

1919
/**
2020
* Normalizes and denormalizes xAPI statement actors.
@@ -34,21 +34,7 @@ public function normalize($object, $format = null, array $context = array())
3434

3535
$data = array();
3636

37-
if (null !== $mbox = $object->getMbox()) {
38-
$data['mbox'] = $mbox;
39-
}
40-
41-
if (null !== $mboxSha1Sum = $object->getMboxSha1Sum()) {
42-
$data['mbox_sha1sum'] = $mboxSha1Sum;
43-
}
44-
45-
if (null !== $openId = $object->getOpenId()) {
46-
$data['openid'] = $openId;
47-
}
48-
49-
if (null !== $account = $object->getAccount()) {
50-
$data['account'] = $this->normalizeAttribute($account);
51-
}
37+
$this->normalizeInverseFunctionalIdentifier($object->getInverseFunctionalIdentifier(), $data);
5238

5339
if (null !== $name = $object->getName()) {
5440
$data['name'] = $name;
@@ -82,17 +68,14 @@ public function supportsNormalization($data, $format = null)
8268
*/
8369
public function denormalize($data, $class, $format = null, array $context = array())
8470
{
85-
$mbox = isset($data['mbox']) ? $data['mbox'] : null;
86-
$mboxSha1Sum = isset($data['mboxSha1Sum']) ? $data['mboxSha1Sum'] : null;
87-
$openId = isset($data['openid']) ? $data['openid'] : null;
71+
$iri = $this->denormalizeInverseFunctionalIdentifier($data, $format, $context);
8872
$name = isset($data['name']) ? $data['name'] : null;
89-
$account = $this->denormalizeAccount($data, $format, $context);
9073

9174
if (isset($data['objectType']) && 'Group' === $data['objectType']) {
92-
return $this->denormalizeGroup($mbox, $mboxSha1Sum, $openId, $account, $name, $data, $format, $context);
75+
return $this->denormalizeGroup($iri, $name, $data, $format, $context);
9376
}
9477

95-
return new Agent($mbox, $mboxSha1Sum, $openId, $account, $name);
78+
return new Agent($iri, $name);
9679
}
9780

9881
/**
@@ -103,16 +86,58 @@ public function supportsDenormalization($data, $type, $format = null)
10386
return 'Xabbuh\XApi\Model\Actor' === $type;
10487
}
10588

89+
private function normalizeInverseFunctionalIdentifier(InverseFunctionalIdentifier $iri = null, &$data)
90+
{
91+
if (null === $iri) {
92+
return;
93+
}
94+
95+
if (null !== $mbox = $iri->getMbox()) {
96+
$data['mbox'] = $mbox;
97+
}
98+
99+
if (null !== $mboxSha1Sum = $iri->getMboxSha1Sum()) {
100+
$data['mbox_sha1sum'] = $mboxSha1Sum;
101+
}
102+
103+
if (null !== $openId = $iri->getOpenId()) {
104+
$data['openid'] = $openId;
105+
}
106+
107+
if (null !== $account = $iri->getAccount()) {
108+
$data['account'] = $this->normalizeAttribute($account);
109+
}
110+
}
111+
112+
private function denormalizeInverseFunctionalIdentifier($data, $format = null, array $context = array())
113+
{
114+
if (isset($data['mbox'])) {
115+
return InverseFunctionalIdentifier::withMbox($data['mbox']);
116+
}
117+
118+
if (isset($data['mboxSha1Sum'])) {
119+
return InverseFunctionalIdentifier::withMboxSha1Sum($data['mboxSha1Sum']);
120+
}
121+
122+
if (isset($data['openid'])) {
123+
return InverseFunctionalIdentifier::withOpenId($data['openid']);
124+
}
125+
126+
if (isset($data['account'])) {
127+
return InverseFunctionalIdentifier::withAccount($this->denormalizeAccount($data, $format, $context));
128+
}
129+
}
130+
106131
private function denormalizeAccount($data, $format = null, array $context = array())
107132
{
108133
if (!isset($data['account'])) {
109134
return null;
110135
}
111136

112-
return $this->denormalizeData($data['account'], 'Xabbuh\XApi\Model\Account', $format,$context);
137+
return $this->denormalizeData($data['account'], 'Xabbuh\XApi\Model\Account', $format, $context);
113138
}
114139

115-
private function denormalizeGroup($mbox, $mboxSha1Sum, $openId, Account $account = null, $name, $data, $format = null, array $context = array())
140+
private function denormalizeGroup(InverseFunctionalIdentifier $iri = null, $name, $data, $format = null, array $context = array())
116141
{
117142
$members = array();
118143

@@ -122,6 +147,6 @@ private function denormalizeGroup($mbox, $mboxSha1Sum, $openId, Account $account
122147
}
123148
}
124149

125-
return new Group($mbox, $mboxSha1Sum, $openId, $account, $name, $members);
150+
return new Group($iri, $name, $members);
126151
}
127152
}

tests/ActorSerializerTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function testDeserializeAgent()
3737

3838
$this->assertInstanceOf('\Xabbuh\XApi\Model\Agent', $actor);
3939
$this->assertEquals('Christian', $actor->getName());
40-
$this->assertEquals('mailto:christian@example.com', $actor->getMbox());
40+
$this->assertEquals('mailto:christian@example.com', $actor->getInverseFunctionalIdentifier()->getMbox());
4141
}
4242

4343
public function testDeserializeAgentWithoutObjectType()
@@ -53,17 +53,18 @@ public function testDeserializeGroup()
5353
$group = $this->actorSerializer->deserializeActor(ActorJsonFixtures::getGroup());
5454

5555
$this->assertEquals('Example Group', $group->getName());
56-
$this->assertEquals('http://example.com/homePage', $group->getAccount()->getHomePage());
57-
$this->assertEquals('GroupAccount', $group->getAccount()->getName());
56+
$this->assertEquals('http://example.com/homePage', $group->getInverseFunctionalIdentifier()->getAccount()->getHomePage());
57+
$this->assertEquals('GroupAccount', $group->getInverseFunctionalIdentifier()->getAccount()->getName());
5858

5959
$members = $group->getMembers();
6060
$this->assertEquals(2, count($members));
6161

6262
$this->assertEquals('Andrew Downes', $members[0]->getName());
63-
$this->assertEquals('mailto:andrew@example.com', $members[0]->getMbox());
63+
$this->assertEquals('http://example.com/account', $members[0]->getInverseFunctionalIdentifier()->getAccount()->getHomePage());
64+
$this->assertEquals('Member of a group', $members[0]->getInverseFunctionalIdentifier()->getAccount()->getName());
6465

6566
$this->assertEquals('Aaron Silvers', $members[1]->getName());
66-
$this->assertEquals('aaron.openid.example.org', $members[1]->getOpenId());
67+
$this->assertEquals('aaron.openid.example.org', $members[1]->getInverseFunctionalIdentifier()->getOpenId());
6768
}
6869

6970
public function testSerializeAgent()

tests/Fixtures/data/group.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"member": [
99
{
1010
"name": "Andrew Downes",
11-
"mbox": "mailto:andrew@example.com",
1211
"account": {
1312
"name": "Member of a group",
1413
"homePage": "http://example.com/account"

0 commit comments

Comments
 (0)