Completed
Pull Request — master (#51)
by Christian
02:16
created

Statement::getVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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
    public function withTimestamp(\DateTime $timestamp = null)
155
    {
156
        $statement = clone $this;
157
        $statement->created = $timestamp;
158
159
        return $statement;
160
    }
161
162
    public function withStored(\DateTime $stored = null)
163
    {
164
        $statement = clone $this;
165
        $statement->stored = $stored;
166
167
        return $statement;
168
    }
169
170 2
    public function withContext(Context $context = null)
171
    {
172 2
        $statement = clone $this;
173
        $statement->context = $context;
174 2
175
        return $statement;
176
    }
177
178
    /**
179
     * @param Attachment[]|null $attachments
180
     *
181
     * @return self
182
     */
183
    public function withAttachments(array $attachments = null)
184 1
    {
185
        $statement = clone $this;
186 1
        $statement->attachments = null !== $attachments ? array_values($attachments) : null;
187 1
188
        return $statement;
189 1
    }
190 1
191
    /**
192
     * @param string $version
193
     *
194
     * @return self
195
     */
196
    public function withVersion($version)
197
    {
198
        $statement = clone $this;
199
        $statement->version = $version;
200
201
        return $statement;
202 2
    }
203
204 2
    /**
205
     * Returns the Statement's unique identifier.
206
     *
207
     * @return StatementId|null The identifier
208
     */
209
    public function getId()
210
    {
211
        return $this->id;
212
    }
213
214
    /**
215
     * Returns the Statement's {@link Verb}.
216 2
     *
217
     * @return Verb The Verb
218 2
     */
219
    public function getVerb()
220
    {
221
        return $this->verb;
222 2
    }
223
224
    /**
225
     * Returns the Statement's {@link Actor}.
226 2
     *
227
     * @return Actor The Actor
228
     */
229
    public function getActor()
230 2
    {
231
        return $this->actor;
232
    }
233
234 2
    /**
235
     * Returns the Statement's {@link Object}.
236
     *
237
     * @return \Xabbuh\XApi\Model\Object The Object
238 2
     */
239
    public function getObject()
240
    {
241
        return $this->object;
242 2
    }
243
244
    /**
245
     * Returns the {@link Activity} {@link Result}.
246 2
     *
247 1
     * @return Result The Result
248
     */
249
    public function getResult()
250 1
    {
251
        return $this->result;
252
    }
253
254 1
    /**
255 1
     * Returns the Authority that asserted the Statement true.
256
     *
257
     * @return Actor The Authority
258
     */
259
    public function getAuthority()
260
    {
261
        return $this->authority;
262
    }
263
264
    /**
265
     * Returns the timestamp of when the events described in this statement
266
     * occurred.
267
     *
268
     * @return \DateTime The timestamp
269
     */
270
    public function getTimestamp()
271
    {
272
        return $this->created;
273
    }
274
275
    /**
276
     * Returns the timestamp of when the events described in this statement
277
     * occurred.
278
     *
279
     * @return \DateTime The timestamp
280
     */
281
    public function getCreated()
282
    {
283
        return $this->created;
284
    }
285
286
    /**
287
     * Returns the timestamp of when this statement was recorded by the LRS.
288
     *
289
     * @return \DateTime The timestamp
290
     */
291
    public function getStored()
292
    {
293
        return $this->stored;
294
    }
295
296
    /**
297
     * Returns the context that gives the statement more meaning.
298
     *
299
     * @return Context|null
300
     */
301
    public function getContext()
302
    {
303
        return $this->context;
304
    }
305
306
    public function getAttachments()
307
    {
308
        return $this->attachments;
309
    }
310
311
    public function getVersion()
312
    {
313
        return $this->version;
314
    }
315
316
    /**
317
     * Tests whether or not this Statement is a void Statement (i.e. it voids
318
     * another Statement).
319
     *
320
     * @return bool True if the Statement voids another Statement, false otherwise
321
     */
322
    public function isVoidStatement()
323
    {
324
        return $this->verb->isVoidVerb();
325
    }
326
327
    /**
328
     * Returns a {@link StatementReference} for the Statement.
329
     *
330
     * @return StatementReference The reference
331
     */
332
    public function getStatementReference()
333
    {
334
        $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...
335
336
        return $reference;
337
    }
338
339
    /**
340
     * Returns a Statement that voids the current Statement.
341
     *
342
     * @param Actor $actor The Actor voiding this Statement
343
     *
344
     * @return Statement The voiding Statement
345
     */
346
    public function getVoidStatement(Actor $actor)
347
    {
348
        return new Statement(
349
            null,
350
            $actor,
351
            Verb::createVoidVerb(),
352
            $this->getStatementReference()
353
        );
354
    }
355
356
    /**
357
     * Checks if another statement is equal.
358
     *
359
     * Two statements are equal if and only if all of their properties are equal.
360
     *
361
     * @param Statement $statement The statement to compare with
362
     *
363
     * @return bool True if the statements are equal, false otherwise
364
     */
365
    public function equals(Statement $statement)
366
    {
367
        if (null !== $this->id xor null !== $statement->id) {
368
            return false;
369
        }
370
371
        if (null !== $this->id && null !== $statement->id && !$this->id->equals($statement->id)) {
372
            return false;
373
        }
374
375
        if (!$this->actor->equals($statement->actor)) {
376
            return false;
377
        }
378
379
        if (!$this->verb->equals($statement->verb)) {
380
            return false;
381
        }
382
383
        if (!$this->object->equals($statement->object)) {
384
            return false;
385
        }
386
387
        if (null === $this->result && null !== $statement->result) {
388
            return false;
389
        }
390
391
        if (null !== $this->result && null === $statement->result) {
392
            return false;
393
        }
394
395
        if (null !== $this->result && !$this->result->equals($statement->result)) {
396
            return false;
397
        }
398
399
        if (null === $this->authority && null !== $statement->authority) {
400
            return false;
401
        }
402
403
        if (null !== $this->authority && null === $statement->authority) {
404
            return false;
405
        }
406
407
        if (null !== $this->authority && !$this->authority->equals($statement->authority)) {
408
            return false;
409
        }
410
411
        if ($this->created != $statement->created) {
412
            return false;
413
        }
414
415
        if (null !== $this->context xor null !== $statement->context) {
416
            return false;
417
        }
418
419
        if (null !== $this->context && null !== $statement->context && !$this->context->equals($statement->context)) {
420
            return false;
421
        }
422
423
        if (null !== $this->attachments xor null !== $statement->attachments) {
424
            return false;
425
        }
426
427
        if (null !== $this->attachments && null !== $statement->attachments) {
428
            if (count($this->attachments) !== count($statement->attachments)) {
429
                return false;
430
            }
431
432
            foreach ($this->attachments as $key => $attachment) {
433
                if (!$attachment->equals($statement->attachments[$key])) {
434
                    return false;
435
                }
436
            }
437
        }
438
439
        return true;
440
    }
441
}
442