Skip to content

Commit 49a1eb9

Browse files
committed
add Doctrine mapping classes for all attributes
1 parent 0837430 commit 49a1eb9

File tree

12 files changed

+1173
-113
lines changed

12 files changed

+1173
-113
lines changed

CHANGELOG.md

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

7+
* Added mapping classes for all statement properties.
8+
79
* The `MappedStatement` and `MappedVerb` classes have been removed from the
810
`php-xapi/model` package. They have been replaced with the new `Statement`
911
and `Verb` classes in the `XApi\Repository\Doctrine\Mapping` namespace of

src/Mapping/Actor.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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 XApi\Repository\Doctrine\Mapping;
13+
14+
use Xabbuh\XApi\Model\Account;
15+
use Xabbuh\XApi\Model\Actor as ActorModel;
16+
use Xabbuh\XApi\Model\Agent;
17+
use Xabbuh\XApi\Model\Group;
18+
use Xabbuh\XApi\Model\InverseFunctionalIdentifier;
19+
use Xabbuh\XApi\Model\IRI;
20+
use Xabbuh\XApi\Model\IRL;
21+
22+
/**
23+
* @author Christian Flothmann <[email protected]>
24+
*/
25+
class Actor
26+
{
27+
public $identifier;
28+
29+
/**
30+
* @var string
31+
*/
32+
public $type;
33+
34+
/**
35+
* @var string|null
36+
*/
37+
public $mbox;
38+
39+
/**
40+
* @var string|null
41+
*/
42+
public $mboxSha1Sum;
43+
44+
/**
45+
* @var string|null
46+
*/
47+
public $openId;
48+
49+
/**
50+
* @var string|null
51+
*/
52+
public $accountName;
53+
54+
/**
55+
* @var string|null
56+
*/
57+
public $accountHomePage;
58+
59+
/**
60+
* @var string|null
61+
*/
62+
public $name;
63+
64+
/**
65+
* @var Actor[]|null
66+
*/
67+
public $members;
68+
69+
public static function fromModel(ActorModel $model)
70+
{
71+
$inverseFunctionalIdentifier = $model->getInverseFunctionalIdentifier();
72+
73+
$actor = new self();
74+
$actor->mboxSha1Sum = $inverseFunctionalIdentifier->getMboxSha1Sum();
75+
$actor->openId = $inverseFunctionalIdentifier->getOpenId();
76+
77+
if (null !== $mbox = $inverseFunctionalIdentifier->getMbox()) {
78+
$actor->mbox = $mbox->getValue();
79+
}
80+
81+
if (null !== $account = $inverseFunctionalIdentifier->getAccount()) {
82+
$actor->accountName = $account->getName();
83+
$actor->accountHomePage = $account->getHomePage()->getValue();
84+
}
85+
86+
if ($model instanceof Group) {
87+
$actor->type = 'group';
88+
$actor->members = array();
89+
90+
foreach ($model->getMembers() as $agent) {
91+
$actor->members[] = Actor::fromModel($agent);
92+
}
93+
} else {
94+
$actor->type = 'agent';
95+
}
96+
97+
return $actor;
98+
}
99+
100+
public function getModel()
101+
{
102+
$inverseFunctionalIdentifier = null;
103+
104+
if (null !== $this->mbox) {
105+
$inverseFunctionalIdentifier = InverseFunctionalIdentifier::withMbox(IRI::fromString($this->mbox));
106+
} elseif (null !== $this->mboxSha1Sum) {
107+
$inverseFunctionalIdentifier = InverseFunctionalIdentifier::withMboxSha1Sum($this->mboxSha1Sum);
108+
} elseif (null !== $this->openId) {
109+
$inverseFunctionalIdentifier = InverseFunctionalIdentifier::withOpenId($this->openId);
110+
} elseif (null !== $this->accountName && null !== $this->accountHomePage) {
111+
$inverseFunctionalIdentifier = InverseFunctionalIdentifier::withAccount(new Account($this->accountName, IRL::fromString($this->accountHomePage)));
112+
}
113+
114+
if ('group' === $this->type) {
115+
$members = array();
116+
117+
foreach ($this->members as $agent) {
118+
$members[] = $agent->getModel();
119+
}
120+
121+
return new Group($inverseFunctionalIdentifier, $this->name, $members);
122+
} else {
123+
return new Agent($inverseFunctionalIdentifier, $this->name);
124+
}
125+
}
126+
}

src/Mapping/Attachment.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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 XApi\Repository\Doctrine\Mapping;
13+
14+
use Xabbuh\XApi\Model\Attachment as AttachmentModel;
15+
use Xabbuh\XApi\Model\IRI;
16+
use Xabbuh\XApi\Model\LanguageMap;
17+
18+
/**
19+
* @author Christian Flothmann <[email protected]>
20+
*/
21+
class Attachment
22+
{
23+
public $identifier;
24+
25+
public $statement;
26+
27+
/**
28+
* @var string
29+
*/
30+
public $usageType;
31+
32+
/**
33+
* @var string
34+
*/
35+
public $contentType;
36+
37+
/**
38+
* @var int
39+
*/
40+
public $length;
41+
42+
/**
43+
* @var string
44+
*/
45+
public $sha2;
46+
47+
/**
48+
* @var array
49+
*/
50+
public $display;
51+
52+
/**
53+
* @var bool
54+
*/
55+
public $hasDescription;
56+
57+
/**
58+
* @var array|null
59+
*/
60+
public $description;
61+
62+
/**
63+
* @var string|null
64+
*/
65+
public $fileUrl;
66+
67+
public static function fromModel(AttachmentModel $model)
68+
{
69+
$attachment = new self();
70+
$attachment->usageType = $model->getUsageType()->getValue();
71+
$attachment->contentType = $model->getContentType();
72+
$attachment->length = $model->getLength();
73+
$attachment->sha2 = $model->getSha2();
74+
$attachment->display = array();
75+
$attachment->fileUrl = $model->getFileUrl();
76+
77+
$display = $model->getDisplay();
78+
79+
foreach ($display->languageTags() as $languageTag) {
80+
$attachment->display[$languageTag] = $display[$languageTag];
81+
}
82+
83+
if (null !== $description = $model->getDescription()) {
84+
$attachment->hasDescription = true;
85+
$attachment->description = array();
86+
87+
foreach ($description->languageTags() as $languageTag) {
88+
$attachment->description[$languageTag] = $description[$languageTag];
89+
}
90+
} else {
91+
$attachment->hasDescription = false;
92+
}
93+
94+
return $attachment;
95+
}
96+
97+
public function getModel()
98+
{
99+
$description = null;
100+
101+
if ($this->hasDescription) {
102+
$description = LanguageMap::create($this->description);
103+
}
104+
105+
return new AttachmentModel(IRI::fromString($this->usageType), $this->contentType, $this->length, $this->sha2, LanguageMap::create($this->display), $description, $this->fileUrl);
106+
}
107+
}

0 commit comments

Comments
 (0)