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\Statement as StatementModel; |
15
|
|
|
use Xabbuh\XApi\Model\StatementId; |
16
|
|
|
use Xabbuh\XApi\Model\StatementReference; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* A {@link Statement} mapped to a storage backend. |
20
|
|
|
* |
21
|
|
|
* @author Christian Flothmann <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Statement |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $id; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Object |
32
|
|
|
*/ |
33
|
|
|
public $actor; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Verb |
37
|
|
|
*/ |
38
|
|
|
public $verb; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Object |
42
|
|
|
*/ |
43
|
|
|
public $object; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Result |
47
|
|
|
*/ |
48
|
|
|
public $result; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var Object |
52
|
|
|
*/ |
53
|
|
|
public $authority; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var int |
57
|
|
|
*/ |
58
|
|
|
public $created; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var int |
62
|
|
|
*/ |
63
|
|
|
public $stored; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var Context |
67
|
|
|
*/ |
68
|
|
|
public $context; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var bool |
72
|
|
|
*/ |
73
|
|
|
public $hasAttachments; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var Attachment[]|null |
77
|
|
|
*/ |
78
|
|
|
public $attachments; |
79
|
|
|
|
80
|
|
|
public static function fromModel(StatementModel $model) |
81
|
|
|
{ |
82
|
|
|
$statement = new self(); |
83
|
|
|
$statement->id = $model->getId()->getValue(); |
84
|
|
|
$statement->actor = Object::fromModel($model->getActor()); |
85
|
|
|
$statement->verb = Verb::fromModel($model->getVerb()); |
86
|
|
|
$statement->object = Object::fromModel($model->getObject()); |
87
|
|
|
|
88
|
|
|
if (null !== $model->getTimestamp()) { |
89
|
|
|
$statement->created = $model->getTimestamp()->getTimestamp(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (null !== $result = $model->getResult()) { |
93
|
|
|
$statement->result = Result::fromModel($result); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (null !== $authority = $model->getAuthority()) { |
97
|
|
|
$statement->authority = Object::fromModel($authority); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (null !== $context = $model->getContext()) { |
101
|
|
|
$statement->context = Context::fromModel($context); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (null !== $attachments = $model->getAttachments()) { |
105
|
|
|
$statement->hasAttachments = true; |
106
|
|
|
$statement->attachments = array(); |
107
|
|
|
|
108
|
|
|
foreach ($attachments as $attachment) { |
109
|
|
|
$mappedAttachment = Attachment::fromModel($attachment); |
110
|
|
|
$mappedAttachment->statement = $statement; |
111
|
|
|
$statement->attachments[] = $mappedAttachment; |
112
|
|
|
} |
113
|
|
|
} else { |
114
|
|
|
$statement->hasAttachments = false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $statement; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getModel() |
121
|
|
|
{ |
122
|
|
|
$result = null; |
123
|
|
|
$authority = null; |
124
|
|
|
$created = null; |
125
|
|
|
$stored = null; |
126
|
|
|
$context = null; |
127
|
|
|
$attachments = null; |
128
|
|
|
|
129
|
|
|
if (null !== $this->result) { |
130
|
|
|
$result = $this->result->getModel(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (null !== $this->authority) { |
134
|
|
|
$authority = $this->authority->getModel(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if (null !== $this->created) { |
138
|
|
|
$created = new \DateTime('@'.$this->created); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if (null !== $this->stored) { |
142
|
|
|
$stored = new \DateTime('@'.$this->stored); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (null !== $this->context) { |
146
|
|
|
$context = $this->context->getModel(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if ($this->hasAttachments) { |
150
|
|
|
$attachments = array(); |
151
|
|
|
|
152
|
|
|
foreach ($this->attachments as $attachment) { |
|
|
|
|
153
|
|
|
$attachments[] = $attachment->getModel(); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return new StatementModel( |
158
|
|
|
StatementId::fromString($this->id), |
159
|
|
|
$this->actor->getModel(), |
160
|
|
|
$this->verb->getModel(), |
161
|
|
|
$this->object->getModel(), |
162
|
|
|
$result, |
163
|
|
|
$authority, |
164
|
|
|
$created, |
165
|
|
|
$stored, |
166
|
|
|
$context, |
167
|
|
|
$attachments |
|
|
|
|
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.