-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProduct.php
156 lines (134 loc) · 3.59 KB
/
Product.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
<?php
namespace MHD\Tradebyte\Data\Csv;
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* @link https://infocenter.tradebyte.com/wp-content/uploads/docs/manual-basic-en/3005_Part4b_Master%20Data%20Import%20via%20CSV-EN.pdf
*/
class Product implements NormalizableInterface
{
/**
* @var string
*/
public $id;
/**
* @var string
*/
public $brand;
/**
* @var string
*/
public $name;
/**
* @var string
*/
public $description;
/**
* @var string
*/
public $keywords;
/**
* @var bool[]
*/
private $availability = [];
/**
* @var array
*/
private $bulletPoints = [];
/**
* @var array
*/
private $components = [];
/**
* @var array
*/
private $tags = [];
public function setAvailability(bool $isAvailable, ?string $channel = null): self
{
if ($channel === null) {
$this->availability = $isAvailable;
} else {
if (!is_array($this->availability)) {
$this->availability = [];
}
$this->availability[$channel] = $isAvailable;
}
return $this;
}
public function addBulletPoint(string $bulletPoint): self
{
$this->bulletPoints[] = $bulletPoint;
return $this;
}
public function addComponent(string $key, string $component): self
{
$this->components[$key] = $component;
return $this;
}
public function addTag(string $key, string $tag): self
{
if (!isset($this->tags[$key])) {
$this->tags[$key] = [];
}
$this->tags[$key][] = $tag;
return $this;
}
public function normalize(NormalizerInterface $normalizer, string $format = null, array $context = [])
{
return $this->toArray();
}
public function toArray(): array
{
return array_merge(
[
'p_nr' => $this->id,
'p_brand' => $this->brand,
'p_name' => $this->name,
'p_text' => $this->description,
'p_keywords' => $this->keywords,
],
$this->getFlattenedAvailability(),
$this->getFlattenedBulletPoints(),
$this->getFlattenedComponents(),
$this->getFlattenedTags()
);
}
public function getFlattenedAvailability(): array
{
$flattened = [];
if (is_array($this->availability)) {
foreach ($this->availability as $channel => $active) {
$flattened["p_active[{$channel}]"] = $active ? 1 : 0;
}
} else {
$flattened['p_active'] = $this->availability ? 1 : 0;
}
return $flattened;
}
public function getFlattenedBulletPoints(): array
{
$flattened = [];
foreach ($this->bulletPoints as $index => $bulletPoint) {
$flattened["p_bullet{{$index}}"] = $bulletPoint;
}
return $flattened;
}
public function getFlattenedComponents(): array
{
$flattened = [];
foreach ($this->components as $key => $component) {
$flattened["p_comp[{$key}]"] = $component;
}
return $flattened;
}
public function getFlattenedTags(): array
{
$flattened = [];
foreach ($this->tags as $key => $tags) {
foreach ($tags as $index => $tag) {
$flattened["p_tag[{$key}]{{$index}}"] = $tag;
}
}
return $flattened;
}
}