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 XApi\Repository\Doctrine\Mapping; |
13
|
|
|
|
14
|
|
|
use Xabbuh\XApi\Model\Statement as StatementModel; |
15
|
|
|
use Xabbuh\XApi\Model\StatementId; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* A {@link Statement} mapped to a storage backend. |
19
|
|
|
* |
20
|
|
|
* @author Christian Flothmann <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Statement |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
public $id; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Object |
31
|
|
|
*/ |
32
|
|
|
public $actor; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Verb |
36
|
|
|
*/ |
37
|
|
|
public $verb; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Object |
41
|
|
|
*/ |
42
|
|
|
public $object; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var Result |
46
|
|
|
*/ |
47
|
|
|
public $result; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var Object |
51
|
|
|
*/ |
52
|
|
|
public $authority; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var int |
56
|
|
|
*/ |
57
|
|
|
public $created; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var int |
61
|
|
|
*/ |
62
|
|
|
public $stored; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var Context |
66
|
|
|
*/ |
67
|
|
|
public $context; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var bool |
71
|
|
|
*/ |
72
|
|
|
public $hasAttachments; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var Attachment[]|null |
76
|
|
|
*/ |
77
|
|
|
public $attachments; |
78
|
|
|
|
79
|
|
|
public static function fromModel(StatementModel $model) |
80
|
|
|
{ |
81
|
|
|
$statement = new self(); |
82
|
|
|
$statement->id = $model->getId()->getValue(); |
83
|
|
|
$statement->actor = Object::fromModel($model->getActor()); |
84
|
|
|
$statement->verb = Verb::fromModel($model->getVerb()); |
85
|
|
|
$statement->object = Object::fromModel($model->getObject()); |
86
|
|
|
|
87
|
|
|
if (null !== $model->getTimestamp()) { |
88
|
|
|
$statement->created = $model->getTimestamp()->getTimestamp(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (null !== $result = $model->getResult()) { |
92
|
|
|
$statement->result = Result::fromModel($result); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (null !== $authority = $model->getAuthority()) { |
96
|
|
|
$statement->authority = Object::fromModel($authority); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (null !== $context = $model->getContext()) { |
100
|
|
|
$statement->context = Context::fromModel($context); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (null !== $attachments = $model->getAttachments()) { |
104
|
|
|
$statement->hasAttachments = true; |
105
|
|
|
$statement->attachments = array(); |
106
|
|
|
|
107
|
|
|
foreach ($attachments as $attachment) { |
108
|
|
|
$mappedAttachment = Attachment::fromModel($attachment); |
109
|
|
|
$mappedAttachment->statement = $statement; |
110
|
|
|
$statement->attachments[] = $mappedAttachment; |
111
|
|
|
} |
112
|
|
|
} else { |
113
|
|
|
$statement->hasAttachments = false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $statement; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getModel() |
120
|
|
|
{ |
121
|
|
|
$result = null; |
122
|
|
|
$authority = null; |
123
|
|
|
$created = null; |
124
|
|
|
$stored = null; |
125
|
|
|
$context = null; |
126
|
|
|
$attachments = null; |
127
|
|
|
|
128
|
|
|
if (null !== $this->result) { |
129
|
|
|
$result = $this->result->getModel(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if (null !== $this->authority) { |
133
|
|
|
$authority = $this->authority->getModel(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (null !== $this->created) { |
137
|
|
|
$created = new \DateTime('@'.$this->created); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if (null !== $this->stored) { |
141
|
|
|
$stored = new \DateTime('@'.$this->stored); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if (null !== $this->context) { |
145
|
|
|
$context = $this->context->getModel(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if ($this->hasAttachments) { |
149
|
|
|
$attachments = array(); |
150
|
|
|
|
151
|
|
|
foreach ($this->attachments as $attachment) { |
|
|
|
|
152
|
|
|
$attachments[] = $attachment->getModel(); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return new StatementModel( |
157
|
|
|
StatementId::fromString($this->id), |
158
|
|
|
$this->actor->getModel(), |
159
|
|
|
$this->verb->getModel(), |
160
|
|
|
$this->object->getModel(), |
161
|
|
|
$result, |
162
|
|
|
$authority, |
163
|
|
|
$created, |
164
|
|
|
$stored, |
165
|
|
|
$context, |
166
|
|
|
$attachments |
|
|
|
|
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.