|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This software may be modified and distributed under the terms |
|
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace FAPI\Sylius\Model\Cart; |
|
11
|
|
|
|
|
12
|
|
|
use FAPI\Sylius\Model\CreatableFromArray; |
|
13
|
|
|
use FAPI\Sylius\Model\Product\Variant; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author Kasim Taskin <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
final class CartItem implements CreatableFromArray |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var int |
|
22
|
|
|
*/ |
|
23
|
|
|
private $id; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var int |
|
27
|
|
|
*/ |
|
28
|
|
|
private $quantity; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var int |
|
32
|
|
|
*/ |
|
33
|
|
|
private $unitPrice; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var int |
|
37
|
|
|
*/ |
|
38
|
|
|
private $total; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var Variant |
|
42
|
|
|
*/ |
|
43
|
|
|
private $variant; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* CartItem constructor. |
|
47
|
|
|
* |
|
48
|
|
|
* @param Variant|null $variant |
|
49
|
|
|
*/ |
|
50
|
|
|
private function __construct( |
|
51
|
|
|
int $id, |
|
52
|
|
|
int $quantity, |
|
53
|
|
|
int $unitPrice, |
|
54
|
|
|
int $total, |
|
55
|
|
|
Variant $variant |
|
56
|
|
|
) { |
|
57
|
|
|
$this->id = $id; |
|
58
|
|
|
$this->quantity = $quantity; |
|
59
|
|
|
$this->unitPrice = $unitPrice; |
|
60
|
|
|
$this->total = $total; |
|
61
|
|
|
if (null !== $variant) { |
|
62
|
|
|
$this->variant = $variant; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return CartItem |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function createFromArray(array $data): self |
|
70
|
|
|
{ |
|
71
|
|
|
$id = -1; |
|
72
|
|
|
if (isset($data['id'])) { |
|
73
|
|
|
$id = $data['id']; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$quantity = -1; |
|
77
|
|
|
if (isset($data['quantity'])) { |
|
78
|
|
|
$quantity = $data['quantity']; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$unitPrice = -1; |
|
82
|
|
|
if (isset($data['unitPrice'])) { |
|
83
|
|
|
$unitPrice = $data['unitPrice']; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$total = ''; |
|
87
|
|
|
if (isset($data['total'])) { |
|
88
|
|
|
$total = $data['total']; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$variant = null; |
|
92
|
|
|
if (isset($data['variant'])) { |
|
93
|
|
|
$variant = Variant::createFromArray($data['variant']); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return new self($id, $quantity, $unitPrice, $total, $variant); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function getId(): int |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->id; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getQuantity(): int |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->quantity; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function getUnitPrice(): int |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->unitPrice; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function getTotal(): int |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->total; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function getVariant(): ?Variant |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->variant; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.