-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathItem.php
183 lines (154 loc) · 3.95 KB
/
Item.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace AlexWestergaard\PhpGa4;
use AlexWestergaard\PhpGa4\Helper;
use AlexWestergaard\PhpGa4\Facade;
/**
* Structured items for events that touch products
*/
class Item extends Helper\IOHelper implements Facade\Type\ItemType
{
protected null|string $item_id;
protected null|string $item_name;
protected null|string $affiliation;
protected null|string $coupon;
protected null|string $currency;
protected null|string $item_brand;
protected null|string $item_list_id;
protected null|string $item_list_name;
protected null|string $item_variant;
protected null|string $location_id;
protected null|int|float $discount;
protected null|int|float $price;
protected null|int $quantity;
protected null|int $index;
protected array $item_category = [];
public function setItemId(string $id)
{
$this->item_id = $id;
return $this;
}
public function setItemName(string $name)
{
$this->item_name = $name;
return $this;
}
public function setAffiliation(string $affiliation)
{
$this->affiliation = $affiliation;
return $this;
}
public function setCoupon(string $code)
{
$this->coupon = $code;
return $this;
}
public function setCurrency(string $iso)
{
$this->currency = $iso;
return $this;
}
public function setDiscount(int|float $amount)
{
$this->discount = $amount;
return $this;
}
public function setIndex(int $i)
{
$this->index = $i;
return $this;
}
public function setItemBrand(string $brand)
{
$this->item_brand = $brand;
return $this;
}
public function addItemCategory(string $category)
{
$this->item_category[] = $category;
return $this;
}
public function setItemListId(string $id)
{
$this->item_list_id = $id;
return $this;
}
public function setItemListName(string $name)
{
$this->item_list_name = $name;
return $this;
}
public function setItemVariant(string $variant)
{
$this->item_variant = $variant;
return $this;
}
public function setLocationId(string $id)
{
$this->location_id = $id;
return $this;
}
public function setPrice(int|float $amount)
{
$this->price = 0 + $amount;
return $this;
}
public function setQuantity(int $amount)
{
$this->quantity = $amount;
return $this;
}
public function getParams(): array
{
return [
'item_id',
'item_name',
'affiliation',
'coupon',
'currency',
'discount',
'index',
'item_brand',
'item_category',
'item_list_id',
'item_list_name',
'item_variant',
'location_id',
'price',
'quantity',
];
}
public function getRequiredParams(): array
{
$return = [];
if (
(!isset($this->item_id) || empty($this->item_id) && strval($this->item_id) !== '0')
&& (!isset($this->item_name) || empty($this->item_name) && strval($this->item_name) !== '0')
) {
$return = [
'item_id',
'item_name',
];
}
return $return;
}
public function toArray(): array
{
$res = parent::toArray();
if (!isset($res['item_category'])) {
return $res;
}
$categories = $res['item_category'];
unset($res['item_category']);
if (is_array($categories)) {
foreach ($categories as $k => $val) {
$tag = 'item_category' . ($k > 0 ? $k + 1 : '');
$res[$tag] = $val;
}
}
return $res;
}
public static function new(): static
{
return new static();
}
}