Completed
Push — master ( ef3fef...114410 )
by Christian
03:26
created

Statement::withCreated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 2
cts 2
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
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
 * An Experience API {@link https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md#statement Statement}.
16
 *
17
 * @author Christian Flothmann <[email protected]>
18
 */
19
final class Statement
20
{
21
    /**
22
     * @var StatementId|null The unique identifier
23
     */
24
    private $id;
25
26
    /**
27
     * @var Verb $verb The {@link Verb}
28
     */
29
    private $verb;
30
31
    /**
32
     * @var Actor The {@link Actor}
33
     */
34
    private $actor;
35
36
    /**
37
     * @var Object The {@link Object}
38
     */
39
    private $object;
40
41
    /**
42
     * @var Result The {@link Activity} {@link Result}
43
     */
44
    private $result;
45
46
    /**
47
     * @var Actor The Authority that asserted the Statement true
48
     */
49
    private $authority;
50
51
    /**
52
     * @var \DateTime The timestamp of when the events described in this statement occurred
53
     */
54
    private $created;
55
56
    /**
57
     * @var \DateTime The timestamp of when this statement was recorded by the LRS
58
     */
59
    private $stored;
60
61 4
    /**
62
     * @var Context|null A context giving the statement more meaning
63 4
     */
64 4
    private $context;
65 4
66 4
    private $attachments;
67 4
68 4
    private $version;
69 4
70 4
    /**
71 4
     * @param StatementId|null  $id
72
     * @param Actor             $actor
73
     * @param Verb              $verb
74
     * @param Object            $object
75
     * @param Result|null       $result
76
     * @param Actor|null        $authority
77
     * @param \DateTime|null    $created
78 2
     * @param \DateTime|null    $stored
79
     * @param Context|null      $context
80 2
     * @param Attachment[]|null $attachments
81
     * @param string            $version
82
     */
83
    public function __construct(StatementId $id = null, Actor $actor, Verb $verb, Object $object, Result $result = null, Actor $authority = null, \DateTime $created = null, \DateTime $stored = null, Context $context = null, array $attachments = null, $version = '1.0.0')
84
    {
85
        $this->id = $id;
86
        $this->actor = $actor;
87
        $this->verb = $verb;
88 3
        $this->object = $object;
89
        $this->result = $result;
90 3
        $this->authority = $authority;
91
        $this->created = $created;
92
        $this->stored = $stored;
93
        $this->context = $context;
94
        $this->attachments = null !== $attachments ? array_values($attachments) : null;
95
        $this->version = $version;
96
    }
97
98 3
    public function withId(StatementId $id = null)
99
    {
100 3
        $statement = clone $this;
101
        $statement->id = $id;
102
103
        return $statement;
104
    }
105
106
    public function withActor(Actor $actor)
107
    {
108 3
        $statement = clone $this;
109
        $statement->actor = $actor;
110 3
111
        return $statement;
112
    }
113
114
    public function withVerb(Verb $verb)
115
    {
116
        $statement = clone $this;
117
        $statement->verb = $verb;
118
119
        return $statement;
120
    }
121
122
    public function withObject(Object $object)
123
    {
124
        $statement = clone $this;
125
        $statement->object = $object;
126
127
        return $statement;
128 2
    }
129
130 2
    public function withResult(Result $result = null)
131
    {
132
        $statement = clone $this;
133
        $statement->result = $result;
134
135
        return $statement;
136
    }
137
138
    /**
139
     * Creates a new Statement based on the current one containing an Authority
140
     * that asserts the Statement true.
141
     *
142
     * @param Actor $authority The Authority asserting the Statement true
143
     *
144
     * @return Statement The new Statement
145
     */
146
    public function withAuthority(Actor $authority = null)
147
    {
148
        $statement = clone $this;
149
        $statement->authority = $authority;
150
151
        return $statement;
152
    }
153
154
    /**
155
     * @deprecated since 1.2, to be removed in 3.0
156
     */
157
    public function withTimestamp(\DateTime $timestamp = null)
158
    {
159
        @trigger_error(sprintf('The "%s()" method is deprecated since 1.2 and will be removed in 3.0. Use "%s::withCreated()" instead.', __METHOD__, __CLASS__), E_USER_DEPRECATED);
160
161
        $statement = clone $this;
162
        $statement->created = $timestamp;
163
164
        return $statement;
165
    }
166
167
    public function withCreated(\DateTime $created = null)
168
    {
169
        $statement = clone $this;
170 2
        $statement->created = $created;
171
172 2
        return $statement;
173
    }
174 2
175
    public function withStored(\DateTime $stored = null)
176
    {
177
        $statement = clone $this;
178
        $statement->stored = $stored;
179
180
        return $statement;
181
    }
182
183
    public function withContext(Context $context = null)
184 1
    {
185
        $statement = clone $this;
186 1
        $statement->context = $context;
187 1
188
        return $statement;
189 1
    }
190 1
191
    /**
192
     * @param Attachment[]|null $attachments
193
     *
194
     * @return self
195
     */
196
    public function withAttachments(array $attachments = null)
197
    {
198
        $statement = clone $this;
199
        $statement->attachments = null !== $attachments ? array_values($attachments) : null;
200
201
        return $statement;
202 2
    }
203
204 2
    /**
205
     * @param string $version
206
     *
207
     * @return self
208
     */
209
    public function withVersion($version)
210
    {
211
        $statement = clone $this;
212
        $statement->version = $version;
213
214
        return $statement;
215
    }
216 2
217
    /**
218 2
     * Returns the Statement's unique identifier.
219
     *
220
     * @return StatementId|null The identifier
221
     */
222 2
    public function getId()
223
    {
224
        return $this->id;
225
    }
226 2
227
    /**
228
     * Returns the Statement's {@link Verb}.
229
     *
230 2
     * @return Verb The Verb
231
     */
232
    public function getVerb()
233
    {
234 2
        return $this->verb;
235
    }
236
237
    /**
238 2
     * Returns the Statement's {@link Actor}.
239
     *
240
     * @return Actor The Actor
241
     */
242 2
    public function getActor()
243
    {
244
        return $this->actor;
245
    }
246 2
247 1
    /**
248
     * Returns the Statement's {@link Object}.
249
     *
250 1
     * @return \Xabbuh\XApi\Model\Object The Object
251
     */
252
    public function getObject()
253
    {
254 1
        return $this->object;
255 1
    }
256
257
    /**
258
     * Returns the {@link Activity} {@link Result}.
259
     *
260
     * @return Result The Result
261
     */
262
    public function getResult()
263
    {
264
        return $this->result;
265
    }
266
267
    /**
268
     * Returns the Authority that asserted the Statement true.
269
     *
270
     * @return Actor The Authority
271
     */
272
    public function getAuthority()
273
    {
274
        return $this->authority;
275
    }
276
277
    /**
278
     * Returns the timestamp of when the events described in this statement
279
     * occurred.
280
     *
281
     * @return \DateTime The timestamp
282
     *
283
     * @deprecated since 1.2, to be removed in 3.0
284
     */
285
    public function getTimestamp()
286
    {
287
        @trigger_error(sprintf('The "%s()" method is deprecated since 1.2 and will be removed in 3.0. Use "%s::getCreated()" instead.', __METHOD__, __CLASS__), E_USER_DEPRECATED);
288
289
        return $this->created;
290
    }
291
292
    /**
293
     * Returns the timestamp of when the events described in this statement
294
     * occurred.
295
     *
296
     * @return \DateTime The timestamp
297
     */
298
    public function getCreated()
299
    {
300
        return $this->created;
301
    }
302
303
    /**
304
     * Returns the timestamp of when this statement was recorded by the LRS.
305
     *
306
     * @return \DateTime The timestamp
307
     */
308
    public function getStored()
309
    {
310
        return $this->stored;
311
    }
312
313
    /**
314
     * Returns the context that gives the statement more meaning.
315
     *
316
     * @return Context|null
317
     */
318
    public function getContext()
319
    {
320
        return $this->context;
321
    }
322
323
    public function getAttachments()
324
    {
325
        return $this->attachments;
326
    }
327
328
    public function getVersion()
329
    {
330
        return $this->version;
331
    }
332
333
    /**
334
     * Tests whether or not this Statement is a void Statement (i.e. it voids
335
     * another Statement).
336
     *
337
     * @return bool True if the Statement voids another Statement, false otherwise
338
     */
339
    public function isVoidStatement()
340
    {
341
        return $this->verb->isVoidVerb();
342
    }
343
344
    /**
345
     * Returns a {@link StatementReference} for the Statement.
346
     *
347
     * @return StatementReference The reference
348
     */
349
    public function getStatementReference()
350
    {
351
        $reference = new StatementReference($this->id);
0 ignored issues
show
Bug introduced by
It seems like $this->id can be null; however, __construct() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
352
353
        return $reference;
354
    }
355
356
    /**
357
     * Returns a Statement that voids the current Statement.
358
     *
359
     * @param Actor $actor The Actor voiding this Statement
360
     *
361
     * @return Statement The voiding Statement
362
     */
363
    public function getVoidStatement(Actor $actor)
364
    {
365
        return new Statement(
366
            null,
367
            $actor,
368
            Verb::createVoidVerb(),
369
            $this->getStatementReference()
370
        );
371
    }
372
373
    /**
374
     * Checks if another statement is equal.
375
     *
376
     * Two statements are equal if and only if all of their properties are equal.
377
     *
378
     * @param Statement $statement The statement to compare with
379
     *
380
     * @return bool True if the statements are equal, false otherwise
381
     */
382
    public function equals(Statement $statement)
383
    {
384
        if (null !== $this->id xor null !== $statement->id) {
385
            return false;
386
        }
387
388
        if (null !== $this->id && null !== $statement->id && !$this->id->equals($statement->id)) {
389
            return false;
390
        }
391
392
        if (!$this->actor->equals($statement->actor)) {
393
            return false;
394
        }
395
396
        if (!$this->verb->equals($statement->verb)) {
397
            return false;
398
        }
399
400
        if (!$this->object->equals($statement->object)) {
401
            return false;
402
        }
403
404
        if (null === $this->result && null !== $statement->result) {
405
            return false;
406
        }
407
408
        if (null !== $this->result && null === $statement->result) {
409
            return false;
410
        }
411
412
        if (null !== $this->result && !$this->result->equals($statement->result)) {
413
            return false;
414
        }
415
416
        if (null === $this->authority && null !== $statement->authority) {
417
            return false;
418
        }
419
420
        if (null !== $this->authority && null === $statement->authority) {
421
            return false;
422
        }
423
424
        if (null !== $this->authority && !$this->authority->equals($statement->authority)) {
425
            return false;
426
        }
427
428
        if ($this->created != $statement->created) {
429
            return false;
430
        }
431
432
        if (null !== $this->context xor null !== $statement->context) {
433
            return false;
434
        }
435
436
        if (null !== $this->context && null !== $statement->context && !$this->context->equals($statement->context)) {
437
            return false;
438
        }
439
440
        if (null !== $this->attachments xor null !== $statement->attachments) {
441
            return false;
442
        }
443
444
        if (null !== $this->attachments && null !== $statement->attachments) {
445
            if (count($this->attachments) !== count($statement->attachments)) {
446
                return false;
447
            }
448
449
            foreach ($this->attachments as $key => $attachment) {
450
                if (!$attachment->equals($statement->attachments[$key])) {
451
                    return false;
452
                }
453
            }
454
        }
455
456
        return true;
457
    }
458
}
459