Completed
Push — master ( 2b67c6...21b23e )
by Christian
02:46
created

Statement::withTimestamp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 1
cp 0
cc 1
eloc 4
nc 1
nop 1
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 string 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
    public function __construct($id, Actor $actor, Verb $verb, Object $object, Result $result = null, Actor $authority = null, \DateTime $created = null, \DateTime $stored = null, Context $context = null)
67 4
    {
68 4
        $this->id = $id;
69 4
        $this->actor = $actor;
70 4
        $this->verb = $verb;
71 4
        $this->object = $object;
72
        $this->result = $result;
73
        $this->authority = $authority;
74
        $this->created = $created;
75
        $this->stored = $stored;
76
        $this->context = $context;
77
    }
78 2
79
    /**
80 2
     * @param string $id
81
     *
82
     * @return Statement
83
     */
84
    public function withId($id)
85
    {
86
        $statement = clone $this;
87
        $statement->id = $id;
88 3
89
        return $statement;
90 3
    }
91
92
    public function withActor(Actor $actor)
93
    {
94
        $statement = clone $this;
95
        $statement->actor = $actor;
96
97
        return $statement;
98 3
    }
99
100 3
    public function withVerb(Verb $verb)
101
    {
102
        $statement = clone $this;
103
        $statement->verb = $verb;
104
105
        return $statement;
106
    }
107
108 3
    public function withObject(Object $object)
109
    {
110 3
        $statement = clone $this;
111
        $statement->object = $object;
112
113
        return $statement;
114
    }
115
116
    public function withResult(Result $result)
117
    {
118
        $statement = clone $this;
119
        $statement->result = $result;
120
121
        return $statement;
122
    }
123
124
    /**
125
     * Creates a new Statement based on the current one containing an Authority
126
     * that asserts the Statement true.
127
     *
128 2
     * @param Actor $authority The Authority asserting the Statement true
129
     *
130 2
     * @return Statement The new Statement
131
     */
132
    public function withAuthority(Actor $authority)
133
    {
134
        $statement = clone $this;
135
        $statement->authority = $authority;
136
137
        return $statement;
138
    }
139
140
    public function withTimestamp(\DateTime $timestamp)
141
    {
142
        $statement = clone $this;
143
        $statement->created = $timestamp;
144
145
        return $statement;
146
    }
147
148
    public function withStored(\DateTime $stored)
149
    {
150
        $statement = clone $this;
151
        $statement->stored = $stored;
152
153
        return $statement;
154
    }
155
156
    public function withContext(Context $context)
157
    {
158
        $statement = clone $this;
159
        $statement->context = $context;
160
161
        return $statement;
162
    }
163
164
    /**
165
     * Returns the Statement's unique identifier.
166
     *
167
     * @return string The identifier
168
     */
169
    public function getId()
170 2
    {
171
        return $this->id;
172 2
    }
173
174 2
    /**
175
     * Returns the Statement's {@link Verb}.
176
     *
177
     * @return Verb The Verb
178
     */
179
    public function getVerb()
180
    {
181
        return $this->verb;
182
    }
183
184 1
    /**
185
     * Returns the Statement's {@link Actor}.
186 1
     *
187 1
     * @return Actor The Actor
188
     */
189 1
    public function getActor()
190 1
    {
191
        return $this->actor;
192
    }
193
194
    /**
195
     * Returns the Statement's {@link Object}.
196
     *
197
     * @return \Xabbuh\XApi\Model\Object The Object
198
     */
199
    public function getObject()
200
    {
201
        return $this->object;
202 2
    }
203
204 2
    /**
205
     * Returns the {@link Activity} {@link Result}.
206
     *
207
     * @return Result The Result
208
     */
209
    public function getResult()
210
    {
211
        return $this->result;
212
    }
213
214
    /**
215
     * Returns the Authority that asserted the Statement true.
216 2
     *
217
     * @return Actor The Authority
218 2
     */
219
    public function getAuthority()
220
    {
221
        return $this->authority;
222 2
    }
223
224
    /**
225
     * Returns the timestamp of when the events described in this statement
226 2
     * occurred.
227
     *
228
     * @return \DateTime The timestamp
229
     */
230 2
    public function getTimestamp()
231
    {
232
        return $this->created;
233
    }
234 2
235
    /**
236
     * Returns the timestamp of when the events described in this statement
237
     * occurred.
238 2
     *
239
     * @return \DateTime The timestamp
240
     */
241
    public function getCreated()
242 2
    {
243
        return $this->created;
244
    }
245
246 2
    /**
247 1
     * Returns the timestamp of when this statement was recorded by the LRS.
248
     *
249
     * @return \DateTime The timestamp
250 1
     */
251
    public function getStored()
252
    {
253
        return $this->stored;
254 1
    }
255 1
256
    /**
257
     * Returns the context that gives the statement more meaning.
258
     *
259
     * @return Context|null
260
     */
261
    public function getContext()
262
    {
263
        return $this->context;
264
    }
265
266
    /**
267
     * Tests whether or not this Statement is a void Statement (i.e. it voids
268
     * another Statement).
269
     *
270
     * @return bool True if the Statement voids another Statement, false otherwise
271
     */
272
    public function isVoidStatement()
273
    {
274
        return $this->verb->isVoidVerb();
275
    }
276
277
    /**
278
     * Returns a {@link StatementReference} for the Statement.
279
     *
280
     * @return StatementReference The reference
281
     */
282
    public function getStatementReference()
283
    {
284
        $reference = new StatementReference($this->id);
285
286
        return $reference;
287
    }
288
289
    /**
290
     * Returns a Statement that voids the current Statement.
291
     *
292
     * @param Actor $actor The Actor voiding this Statement
293
     *
294
     * @return Statement The voiding Statement
295
     */
296
    public function getVoidStatement(Actor $actor)
297
    {
298
        return new Statement(
299
            null,
300
            $actor,
301
            Verb::createVoidVerb(),
302
            $this->getStatementReference()
303
        );
304
    }
305
306
    /**
307
     * Checks if another statement is equal.
308
     *
309
     * Two statements are equal if and only if all of their properties are equal.
310
     *
311
     * @param Statement $statement The statement to compare with
312
     *
313
     * @return bool True if the statements are equal, false otherwise
314
     */
315
    public function equals(Statement $statement)
316
    {
317
        if ($this->id !== $statement->id) {
318
            return false;
319
        }
320
321
        if (!$this->actor->equals($statement->actor)) {
322
            return false;
323
        }
324
325
        if (!$this->verb->equals($statement->verb)) {
326
            return false;
327
        }
328
329
        if (!$this->object->equals($statement->object)) {
330
            return false;
331
        }
332
333
        if (null === $this->result && null !== $statement->result) {
334
            return false;
335
        }
336
337
        if (null !== $this->result && null === $statement->result) {
338
            return false;
339
        }
340
341
        if (null !== $this->result && !$this->result->equals($statement->result)) {
342
            return false;
343
        }
344
345
        if (null === $this->authority && null !== $statement->authority) {
346
            return false;
347
        }
348
349
        if (null !== $this->authority && null === $statement->authority) {
350
            return false;
351
        }
352
353
        if (null !== $this->authority && !$this->authority->equals($statement->authority)) {
354
            return false;
355
        }
356
357
        if ($this->created != $statement->created) {
358
            return false;
359
        }
360
361
        return true;
362
    }
363
}
364