1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\ApiSkeleton\Model; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\Model\Collection\ModelCollection; |
8
|
|
|
use Chubbyphp\Model\Collection\ModelCollectionInterface; |
9
|
|
|
use Chubbyphp\Model\ModelInterface; |
10
|
|
|
use Ramsey\Uuid\Uuid; |
11
|
|
|
|
12
|
|
|
final class Course implements ModelInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $id; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $name; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
private $level; |
28
|
|
|
|
29
|
|
|
const LEVEL_START = 1; |
30
|
|
|
const LEVEL_MEDIUM = 2; |
31
|
|
|
const LEVEL_ADVANCED = 3; |
32
|
|
|
const LEVEL_EXPERT = 4; |
33
|
|
|
|
34
|
|
|
const LEVEL = [ |
35
|
|
|
self::LEVEL_START, |
36
|
|
|
self::LEVEL_MEDIUM, |
37
|
|
|
self::LEVEL_ADVANCED, |
38
|
|
|
self::LEVEL_EXPERT, |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var float |
43
|
|
|
*/ |
44
|
|
|
private $progress; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
private $active; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var ModelCollectionInterface |
53
|
|
|
*/ |
54
|
|
|
private $documents; |
55
|
|
|
|
56
|
|
|
private function __construct() |
57
|
|
|
{ |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string|null $id |
62
|
|
|
* |
63
|
|
|
* @return self |
64
|
|
|
*/ |
65
|
|
|
public static function create(string $id = null): self |
66
|
|
|
{ |
67
|
|
|
$self = new self(); |
68
|
|
|
$self->id = $id ?? (string) Uuid::uuid4(); |
69
|
|
|
$self->documents = new ModelCollection( |
70
|
|
|
Document::class, 'courseId', $self->id, ['name' => 'ASC'] |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
return $self; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param array $data |
78
|
|
|
* |
79
|
|
|
* @return self|ModelInterface |
80
|
|
|
*/ |
81
|
|
|
public static function fromPersistence(array $data): ModelInterface |
82
|
|
|
{ |
83
|
|
|
$self = new self(); |
84
|
|
|
$self->id = $data['id']; |
85
|
|
|
$self->name = $data['name']; |
86
|
|
|
$self->level = (int) $data['level']; |
87
|
|
|
$self->progress = (float) $data['progress']; |
88
|
|
|
$self->active = (bool) $data['active']; |
89
|
|
|
$self->documents = $data['documents']; |
90
|
|
|
|
91
|
|
|
return $self; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public function toPersistence(): array |
98
|
|
|
{ |
99
|
|
|
return [ |
100
|
|
|
'id' => $this->id, |
101
|
|
|
'name' => $this->name, |
102
|
|
|
'level' => $this->level, |
103
|
|
|
'progress' => $this->progress, |
104
|
|
|
'active' => $this->active, |
105
|
|
|
'documents' => $this->documents, |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getId(): string |
113
|
|
|
{ |
114
|
|
|
return $this->id; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function getName(): string |
121
|
|
|
{ |
122
|
|
|
return $this->name; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $name |
127
|
|
|
* |
128
|
|
|
* @return self |
129
|
|
|
*/ |
130
|
|
|
public function setName(string $name): self |
131
|
|
|
{ |
132
|
|
|
$this->name = $name; |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return int |
139
|
|
|
*/ |
140
|
|
|
public function getLevel(): int |
141
|
|
|
{ |
142
|
|
|
return $this->level; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param int $level |
147
|
|
|
* |
148
|
|
|
* @return self |
149
|
|
|
*/ |
150
|
|
|
public function setLevel(int $level): self |
151
|
|
|
{ |
152
|
|
|
$this->level = $level; |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return float |
159
|
|
|
*/ |
160
|
|
|
public function getProgress(): float |
161
|
|
|
{ |
162
|
|
|
return $this->progress; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param float $progress |
167
|
|
|
* |
168
|
|
|
* @return self |
169
|
|
|
*/ |
170
|
|
|
public function setProgress(float $progress): self |
171
|
|
|
{ |
172
|
|
|
$this->progress = $progress; |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
public function isActive(): bool |
181
|
|
|
{ |
182
|
|
|
return $this->active; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param bool $active |
187
|
|
|
* |
188
|
|
|
* @return self |
189
|
|
|
*/ |
190
|
|
|
public function setActive(bool $active): self |
191
|
|
|
{ |
192
|
|
|
$this->active = $active; |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param array $documents |
199
|
|
|
* |
200
|
|
|
* @return self |
201
|
|
|
*/ |
202
|
|
|
public function setDocuments(array $documents): self |
203
|
|
|
{ |
204
|
|
|
$this->documents->setModels($documents); |
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return Document[]|array |
211
|
|
|
*/ |
212
|
|
|
public function getDocuments(): array |
213
|
|
|
{ |
214
|
|
|
return array_values($this->documents->getModels()); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|