1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Waredesk\Models; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Waredesk\Collections\Codes\Elements; |
7
|
|
|
use JsonSerializable; |
8
|
|
|
use Waredesk\Entity; |
9
|
|
|
use Waredesk\ReplaceableEntity; |
10
|
|
|
use Waredesk\Models\Product\Variant\Code as VariantCode; |
11
|
|
|
|
12
|
|
|
class Code implements Entity, ReplaceableEntity, JsonSerializable |
13
|
|
|
{ |
14
|
|
|
private $id; |
15
|
|
|
private $name; |
16
|
|
|
private $elements; |
17
|
|
|
private $creation; |
18
|
|
|
private $modification; |
19
|
|
|
|
20
|
4 |
|
public function __construct(array $data = null) |
21
|
|
|
{ |
22
|
4 |
|
$this->elements = new Elements(); |
23
|
4 |
|
$this->reset($data); |
24
|
4 |
|
} |
25
|
|
|
|
26
|
1 |
|
public function __clone() |
27
|
|
|
{ |
28
|
1 |
|
$this->elements = clone $this->elements; |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
public function toVariantCode(): VariantCode |
32
|
|
|
{ |
33
|
|
|
$code = new VariantCode(); |
34
|
|
|
$code->reset([ |
35
|
|
|
'code' => $this->getId(), |
36
|
|
|
'name' => $this->getName(), |
37
|
|
|
'creation' => $this->getCreation(), |
38
|
|
|
'modification' => $this->getModification(), |
39
|
|
|
]); |
40
|
|
|
foreach ($this->getElements() as $element) { |
41
|
|
|
$nextElement = new VariantCode\Element(); |
42
|
|
|
$nextElement->reset([ |
43
|
|
|
'element' => $element->getId(), |
44
|
|
|
'type' => $element->getType(), |
45
|
|
|
'value' => $element->getValue(), |
46
|
|
|
'auto_increment' => $element->getAutoIncrement(), |
47
|
|
|
'pad_direction' => $element->getPadDirection(), |
48
|
|
|
'pad_char' => $element->getPadChar(), |
49
|
|
|
'pad_length' => $element->getPadLength(), |
50
|
|
|
]); |
51
|
|
|
$code->getElements()->add($nextElement); |
52
|
|
|
} |
53
|
|
|
return $code; |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
public function getId(): ? string |
57
|
|
|
{ |
58
|
2 |
|
return $this->id; |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
public function getName(): ? string |
62
|
|
|
{ |
63
|
2 |
|
return $this->name; |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
public function getElements(): ? Elements |
67
|
|
|
{ |
68
|
4 |
|
return $this->elements; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getCreation(): ?DateTime |
72
|
|
|
{ |
73
|
|
|
return $this->creation; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getModification(): ? DateTime |
77
|
|
|
{ |
78
|
|
|
return $this->modification; |
79
|
|
|
} |
80
|
|
|
|
81
|
4 |
|
public function reset(array $data = null) |
82
|
|
|
{ |
83
|
4 |
|
if ($data) { |
84
|
3 |
|
foreach ($data as $key => $value) { |
85
|
|
|
switch ($key) { |
86
|
3 |
|
case 'id': |
|
|
|
|
87
|
3 |
|
$this->id = $value; |
88
|
3 |
|
break; |
89
|
3 |
|
case 'name': |
90
|
3 |
|
$this->name = $value; |
91
|
3 |
|
break; |
92
|
3 |
|
case 'variants': |
93
|
|
|
$this->elements = $value; |
94
|
|
|
break; |
95
|
3 |
|
case 'creation': |
96
|
3 |
|
$this->creation = $value; |
97
|
3 |
|
break; |
98
|
3 |
|
case 'modification': |
99
|
3 |
|
$this->modification = $value; |
100
|
3 |
|
break; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
4 |
|
} |
105
|
|
|
|
106
|
1 |
|
public function setName(string $name = null) |
107
|
|
|
{ |
108
|
1 |
|
$this->name = $name; |
109
|
1 |
|
} |
110
|
|
|
|
111
|
2 |
|
public function jsonSerialize(): array |
112
|
|
|
{ |
113
|
|
|
$returnValue = [ |
114
|
2 |
|
'name' => $this->getName(), |
115
|
2 |
|
'elements' => $this->getElements()->jsonSerialize(), |
116
|
|
|
]; |
117
|
2 |
|
if ($this->getId()) { |
118
|
1 |
|
$returnValue = array_merge(['id' => $this->getId()], $returnValue); |
119
|
|
|
} |
120
|
2 |
|
return $returnValue; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
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.