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 Xabbuh\XApi\Model; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* A {@link Statement} included as part of a parent Statement. |
16
|
|
|
* |
17
|
|
|
* @author Christian Flothmann <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
final class SubStatement extends Object |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Verb $verb The {@link Verb} |
23
|
|
|
*/ |
24
|
|
|
private $verb; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Actor The {@link Actor} |
28
|
|
|
*/ |
29
|
|
|
private $actor; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Object The {@link Object} |
33
|
|
|
*/ |
34
|
|
|
private $object; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Result The {@link Activity} {@link Result} |
38
|
|
|
*/ |
39
|
|
|
private $result; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \DateTime The timestamp of when the events described in this statement occurred |
43
|
|
|
*/ |
44
|
|
|
private $timestamp; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Context The {@link Statement} {@link Context} |
48
|
|
|
*/ |
49
|
|
|
private $context; |
50
|
|
|
|
51
|
|
|
private $attachments; |
52
|
|
|
|
53
|
|
|
public function __construct(Actor $actor, Verb $verb, Object $object, Result $result = null, Context $context = null, \DateTime $timestamp = null, array $attachments = null) |
54
|
|
|
{ |
55
|
|
|
if ($object instanceof SubStatement) { |
56
|
|
|
throw new \InvalidArgumentException('Nesting sub statements is forbidden by the xAPI spec.'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->actor = $actor; |
60
|
|
|
$this->verb = $verb; |
61
|
|
|
$this->object = $object; |
62
|
|
|
$this->result = $result; |
63
|
|
|
$this->timestamp = $timestamp; |
64
|
|
|
$this->context = $context; |
65
|
|
|
$this->attachments = null !== $attachments ? array_values($attachments) : null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function withActor(Actor $actor) |
69
|
|
|
{ |
70
|
|
|
$subStatement = clone $this; |
71
|
|
|
$subStatement->actor = $actor; |
72
|
|
|
|
73
|
|
|
return $subStatement; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function withVerb(Verb $verb) |
77
|
|
|
{ |
78
|
|
|
$subStatement = clone $this; |
79
|
|
|
$subStatement->verb = $verb; |
80
|
|
|
|
81
|
|
|
return $subStatement; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function withObject(Object $object) |
85
|
|
|
{ |
86
|
|
|
$subStatement = clone $this; |
87
|
|
|
$subStatement->object = $object; |
88
|
|
|
|
89
|
|
|
return $subStatement; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function withResult(Result $result) |
93
|
|
|
{ |
94
|
|
|
$subStatement = clone $this; |
95
|
|
|
$subStatement->result = $result; |
96
|
|
|
|
97
|
|
|
return $subStatement; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function withTimestamp(\DateTime $timestamp = null) |
101
|
|
|
{ |
102
|
|
|
$statement = clone $this; |
103
|
|
|
$statement->timestamp = $timestamp; |
104
|
|
|
|
105
|
|
|
return $statement; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function withContext(Context $context) |
109
|
|
|
{ |
110
|
|
|
$subStatement = clone $this; |
111
|
|
|
$subStatement->context = $context; |
112
|
|
|
|
113
|
|
|
return $subStatement; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param Attachment[]|null $attachments |
118
|
|
|
* |
119
|
|
|
* @return self |
120
|
|
|
*/ |
121
|
|
|
public function withAttachments(array $attachments = null) |
122
|
|
|
{ |
123
|
|
|
$statement = clone $this; |
124
|
|
|
$statement->attachments = null !== $attachments ? array_values($attachments) : null; |
125
|
|
|
|
126
|
|
|
return $statement; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns the Statement's {@link Verb}. |
131
|
|
|
* |
132
|
|
|
* @return Verb The Verb |
133
|
|
|
*/ |
134
|
|
|
public function getVerb() |
135
|
|
|
{ |
136
|
|
|
return $this->verb; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns the Statement's {@link Actor}. |
141
|
|
|
* |
142
|
|
|
* @return Actor The Actor |
143
|
|
|
*/ |
144
|
|
|
public function getActor() |
145
|
|
|
{ |
146
|
|
|
return $this->actor; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Returns the Statement's {@link Object}. |
151
|
|
|
* |
152
|
|
|
* @return \Xabbuh\XApi\Model\Object The Object |
153
|
|
|
*/ |
154
|
|
|
public function getObject() |
155
|
|
|
{ |
156
|
|
|
return $this->object; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Returns the {@link Activity} {@link Result}. |
161
|
|
|
* |
162
|
|
|
* @return Result The Result |
163
|
|
|
*/ |
164
|
|
|
public function getResult() |
165
|
|
|
{ |
166
|
|
|
return $this->result; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns the timestamp of when the events described in this statement |
171
|
|
|
* occurred. |
172
|
|
|
*/ |
173
|
|
|
public function getTimestamp() |
174
|
|
|
{ |
175
|
|
|
return $this->timestamp; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Returns the {@link Statement} {@link Context}. |
180
|
|
|
* |
181
|
|
|
* @return Context The Context |
182
|
|
|
*/ |
183
|
|
|
public function getContext() |
184
|
|
|
{ |
185
|
|
|
return $this->context; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function getAttachments() |
189
|
|
|
{ |
190
|
|
|
return $this->attachments; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Tests whether or not this Statement is a void Statement (i.e. it voids |
195
|
|
|
* another Statement). |
196
|
|
|
* |
197
|
|
|
* @return bool True if the Statement voids another Statement, false otherwise |
198
|
|
|
*/ |
199
|
|
|
public function isVoidStatement() |
200
|
|
|
{ |
201
|
|
|
return $this->verb->isVoidVerb(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* {@inheritdoc} |
206
|
|
|
*/ |
207
|
|
|
public function equals(Object $statement) |
208
|
|
|
{ |
209
|
|
|
if ('Xabbuh\XApi\Model\SubStatement' !== get_class($statement)) { |
210
|
|
|
return false; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** @var SubStatement $statement */ |
214
|
|
|
|
215
|
|
|
if (!$this->actor->equals($statement->actor)) { |
216
|
|
|
return false; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if (!$this->verb->equals($statement->verb)) { |
220
|
|
|
return false; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
if (!$this->object->equals($statement->object)) { |
224
|
|
|
return false; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
if (null === $this->result && null !== $statement->result) { |
228
|
|
|
return false; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
if (null !== $this->result && null === $statement->result) { |
232
|
|
|
return false; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
if (null !== $this->result && !$this->result->equals($statement->result)) { |
236
|
|
|
return false; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
if ($this->timestamp != $statement->timestamp) { |
240
|
|
|
return false; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
if (null !== $this->context xor null !== $statement->context) { |
244
|
|
|
return false; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
if (null !== $this->context && null !== $statement->context && !$this->context->equals($statement->context)) { |
248
|
|
|
return false; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
if (null !== $this->attachments xor null !== $statement->attachments) { |
252
|
|
|
return false; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
if (null !== $this->attachments && null !== $statement->attachments) { |
256
|
|
|
if (count($this->attachments) !== count($statement->attachments)) { |
257
|
|
|
return false; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
foreach ($this->attachments as $key => $attachment) { |
261
|
|
|
if (!$attachment->equals($statement->attachments[$key])) { |
262
|
|
|
return false; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
return true; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|