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 StatementObject |
20
|
|
|
{ |
21
|
|
|
private $verb; |
22
|
|
|
private $actor; |
23
|
|
|
private $object; |
24
|
|
|
private $result; |
25
|
|
|
private $created; |
26
|
|
|
private $context; |
27
|
|
|
private $attachments; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Attachment[]|null $attachments |
31
|
|
|
*/ |
32
|
|
|
public function __construct(Actor $actor, Verb $verb, StatementObject $object, Result $result = null, Context $context = null, \DateTime $created = null, array $attachments = null) |
33
|
|
|
{ |
34
|
|
|
if ($object instanceof SubStatement) { |
35
|
|
|
throw new \InvalidArgumentException('Nesting sub statements is forbidden by the xAPI spec.'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->actor = $actor; |
39
|
|
|
$this->verb = $verb; |
40
|
|
|
$this->object = $object; |
41
|
|
|
$this->result = $result; |
42
|
|
|
$this->created = $created; |
43
|
|
|
$this->context = $context; |
44
|
|
|
$this->attachments = null !== $attachments ? array_values($attachments) : null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function withActor(Actor $actor): self |
48
|
|
|
{ |
49
|
|
|
$subStatement = clone $this; |
50
|
|
|
$subStatement->actor = $actor; |
51
|
|
|
|
52
|
|
|
return $subStatement; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function withVerb(Verb $verb): self |
56
|
|
|
{ |
57
|
|
|
$subStatement = clone $this; |
58
|
|
|
$subStatement->verb = $verb; |
59
|
|
|
|
60
|
|
|
return $subStatement; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function withObject(StatementObject $object): self |
64
|
|
|
{ |
65
|
|
|
$subStatement = clone $this; |
66
|
|
|
$subStatement->object = $object; |
67
|
|
|
|
68
|
|
|
return $subStatement; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function withResult(Result $result): self |
72
|
|
|
{ |
73
|
|
|
$subStatement = clone $this; |
74
|
|
|
$subStatement->result = $result; |
75
|
|
|
|
76
|
|
|
return $subStatement; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function withCreated(\DateTime $created = null): self |
80
|
|
|
{ |
81
|
|
|
$statement = clone $this; |
82
|
|
|
$statement->created = $created; |
83
|
|
|
|
84
|
|
|
return $statement; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function withContext(Context $context): self |
88
|
|
|
{ |
89
|
|
|
$subStatement = clone $this; |
90
|
|
|
$subStatement->context = $context; |
91
|
|
|
|
92
|
|
|
return $subStatement; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param Attachment[]|null $attachments |
97
|
|
|
*/ |
98
|
|
|
public function withAttachments(array $attachments = null): self |
99
|
|
|
{ |
100
|
|
|
$statement = clone $this; |
101
|
|
|
$statement->attachments = null !== $attachments ? array_values($attachments) : null; |
102
|
|
|
|
103
|
|
|
return $statement; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns the Statement's {@link Verb}. |
108
|
|
|
*/ |
109
|
|
|
public function getVerb(): Verb |
110
|
|
|
{ |
111
|
|
|
return $this->verb; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns the Statement's {@link Actor}. |
116
|
|
|
*/ |
117
|
|
|
public function getActor(): Actor |
118
|
|
|
{ |
119
|
|
|
return $this->actor; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns the Statement's {@link StatementObject}. |
124
|
|
|
*/ |
125
|
|
|
public function getObject(): StatementObject |
126
|
|
|
{ |
127
|
|
|
return $this->object; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns the {@link Activity} {@link Result}. |
132
|
|
|
*/ |
133
|
|
|
public function getResult(): ?Result |
134
|
|
|
{ |
135
|
|
|
return $this->result; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns the timestamp of when the events described in this statement |
140
|
|
|
* occurred. |
141
|
|
|
*/ |
142
|
|
|
public function getCreated(): ?\DateTime |
143
|
|
|
{ |
144
|
|
|
return $this->created; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns the {@link Statement} {@link Context}. |
149
|
|
|
*/ |
150
|
|
|
public function getContext(): ?Context |
151
|
|
|
{ |
152
|
|
|
return $this->context; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return Attachment[]|null |
157
|
|
|
*/ |
158
|
|
|
public function getAttachments(): ?array |
159
|
|
|
{ |
160
|
|
|
return $this->attachments; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Tests whether or not this Statement is a void Statement (i.e. it voids |
165
|
|
|
* another Statement). |
166
|
|
|
*/ |
167
|
|
|
public function isVoidStatement(): bool |
168
|
|
|
{ |
169
|
|
|
return $this->verb->isVoidVerb(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
|
|
public function equals(StatementObject $statement): bool |
176
|
|
|
{ |
177
|
|
|
if (!$statement instanceof SubStatement) { |
178
|
|
|
return false; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if (!$this->actor->equals($statement->actor)) { |
182
|
|
|
return false; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if (!$this->verb->equals($statement->verb)) { |
186
|
|
|
return false; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if (!$this->object->equals($statement->object)) { |
190
|
|
|
return false; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
if (null === $this->result && null !== $statement->result) { |
194
|
|
|
return false; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
if (null !== $this->result && null === $statement->result) { |
198
|
|
|
return false; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
if (null !== $this->result && !$this->result->equals($statement->result)) { |
|
|
|
|
202
|
|
|
return false; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
if ($this->created != $statement->created) { |
206
|
|
|
return false; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
if (null !== $this->context xor null !== $statement->context) { |
210
|
|
|
return false; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
if (null !== $this->context && null !== $statement->context && !$this->context->equals($statement->context)) { |
214
|
|
|
return false; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
if (null !== $this->attachments xor null !== $statement->attachments) { |
218
|
|
|
return false; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
if (null !== $this->attachments && null !== $statement->attachments) { |
222
|
|
|
if (count($this->attachments) !== count($statement->attachments)) { |
223
|
|
|
return false; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
foreach ($this->attachments as $key => $attachment) { |
227
|
|
|
if (!$attachment->equals($statement->attachments[$key])) { |
228
|
|
|
return false; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return true; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: