1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Waredesk\Models; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Waredesk\Collections\Products\Categories; |
7
|
|
|
use Waredesk\Collections\Products\Variants; |
8
|
|
|
use JsonSerializable; |
9
|
|
|
use Waredesk\Entity; |
10
|
|
|
use Waredesk\Image; |
11
|
|
|
use Waredesk\ReplaceableEntity; |
12
|
|
|
|
13
|
|
|
class Product implements Entity, ReplaceableEntity, JsonSerializable |
14
|
|
|
{ |
15
|
|
|
private $id; |
16
|
|
|
private $categories; |
17
|
|
|
private $images; |
18
|
|
|
private $variants; |
19
|
|
|
private $name; |
20
|
|
|
private $description; |
21
|
|
|
private $notes; |
22
|
|
|
private $creation; |
23
|
|
|
private $modification; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Image |
27
|
|
|
*/ |
28
|
|
|
private $pendingImage; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
private $deleteImage = false; |
34
|
|
|
|
35
|
5 |
|
public function __construct(array $data = null) |
36
|
|
|
{ |
37
|
5 |
|
$this->categories = new Categories(); |
38
|
5 |
|
$this->variants = new Variants(); |
39
|
5 |
|
$this->reset($data); |
40
|
5 |
|
} |
41
|
|
|
|
42
|
|
|
public function __clone() |
43
|
|
|
{ |
44
|
|
|
$this->categories = clone $this->categories; |
45
|
|
|
$this->variants = clone $this->variants; |
46
|
|
|
} |
47
|
|
|
|
48
|
4 |
|
public function getId(): ? string |
49
|
|
|
{ |
50
|
4 |
|
return $this->id; |
51
|
|
|
} |
52
|
|
|
|
53
|
4 |
|
public function getCategories(): ? Categories |
54
|
|
|
{ |
55
|
4 |
|
return $this->categories; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public function getImages(): ? array |
59
|
|
|
{ |
60
|
1 |
|
return $this->images; |
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
public function getVariants(): ? Variants |
64
|
|
|
{ |
65
|
4 |
|
return $this->variants; |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
public function getName(): ? string |
69
|
|
|
{ |
70
|
4 |
|
return $this->name; |
71
|
|
|
} |
72
|
|
|
|
73
|
4 |
|
public function getDescription(): ? string |
74
|
|
|
{ |
75
|
4 |
|
return $this->description; |
76
|
|
|
} |
77
|
|
|
|
78
|
4 |
|
public function getNotes(): ? string |
79
|
|
|
{ |
80
|
4 |
|
return $this->notes; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getCreation(): ?DateTime |
84
|
|
|
{ |
85
|
|
|
return $this->creation; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getModification(): ? DateTime |
89
|
|
|
{ |
90
|
|
|
return $this->modification; |
91
|
|
|
} |
92
|
|
|
|
93
|
5 |
|
public function reset(array $data = null) |
94
|
|
|
{ |
95
|
5 |
|
if ($data) { |
96
|
3 |
|
foreach ($data as $key => $value) { |
97
|
|
|
switch ($key) { |
98
|
3 |
|
case 'id': |
|
|
|
|
99
|
3 |
|
$this->id = $value; |
100
|
3 |
|
break; |
101
|
3 |
|
case 'categories': |
102
|
|
|
$this->categories = $value; |
103
|
|
|
break; |
104
|
3 |
|
case 'images': |
105
|
3 |
|
$this->deleteImage = false; |
106
|
3 |
|
$this->pendingImage = null; |
107
|
3 |
|
$this->images = $value; |
108
|
3 |
|
break; |
109
|
3 |
|
case 'variants': |
110
|
3 |
|
$this->variants = $value; |
111
|
3 |
|
break; |
112
|
3 |
|
case 'name': |
113
|
3 |
|
$this->name = $value; |
114
|
3 |
|
break; |
115
|
3 |
|
case 'description': |
116
|
3 |
|
$this->description = $value; |
117
|
3 |
|
break; |
118
|
3 |
|
case 'notes': |
119
|
3 |
|
$this->notes = $value; |
120
|
3 |
|
break; |
121
|
3 |
|
case 'creation': |
122
|
3 |
|
$this->creation = $value; |
123
|
3 |
|
break; |
124
|
3 |
|
case 'modification': |
125
|
3 |
|
$this->modification = $value; |
126
|
3 |
|
break; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
5 |
|
} |
131
|
|
|
|
132
|
|
|
public function deleteImage() |
133
|
|
|
{ |
134
|
|
|
$this->deleteImage = true; |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
public function setImage(Image $image = null) |
138
|
|
|
{ |
139
|
1 |
|
$this->pendingImage = $image; |
140
|
1 |
|
} |
141
|
|
|
|
142
|
4 |
|
public function setName(string $name = null) |
143
|
|
|
{ |
144
|
4 |
|
$this->name = $name; |
145
|
4 |
|
} |
146
|
|
|
|
147
|
4 |
|
public function setDescription(string $description = null) |
148
|
|
|
{ |
149
|
4 |
|
$this->description = $description; |
150
|
4 |
|
} |
151
|
|
|
|
152
|
|
|
public function setNotes(string $notes = null) |
153
|
|
|
{ |
154
|
|
|
$this->notes = $notes; |
155
|
|
|
} |
156
|
|
|
|
157
|
4 |
|
public function jsonSerialize(): array |
158
|
|
|
{ |
159
|
|
|
$returnValue = [ |
160
|
4 |
|
'categories' => $this->getCategories()->jsonSerialize(), |
161
|
4 |
|
'variants' => $this->getVariants()->jsonSerialize(), |
162
|
4 |
|
'name' => $this->getName(), |
163
|
4 |
|
'description' => $this->getDescription(), |
164
|
4 |
|
'notes' => $this->getNotes(), |
165
|
|
|
]; |
166
|
4 |
|
if ($this->pendingImage) { |
167
|
1 |
|
$returnValue['image'] = $this->pendingImage->toBase64(); |
168
|
3 |
|
} elseif ($this->deleteImage) { |
169
|
|
|
$returnValue['image'] = null; |
170
|
|
|
} |
171
|
4 |
|
if ($this->getId()) { |
172
|
|
|
$returnValue = array_merge(['id' => $this->getId()], $returnValue); |
173
|
|
|
} |
174
|
4 |
|
return $returnValue; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.