-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathProduct.php
More file actions
184 lines (160 loc) · 4.14 KB
/
Copy pathProduct.php
File metadata and controls
184 lines (160 loc) · 4.14 KB
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
184
<?php
namespace BNETDocs\Libraries;
use \BNETDocs\Libraries\Db\MariaDb;
use \StdClass;
class Product implements \BNETDocs\Interfaces\DatabaseObject, \JsonSerializable
{
protected int $bnet_product_id;
protected string $bnet_product_raw;
protected int $bnls_product_id;
protected string $label;
protected int $sort;
protected int $version_byte;
public function __construct(StdClass|int|null $value)
{
if ($value instanceof StdClass)
{
$this->allocateObject($value);
}
else
{
$this->setBnetProductId($value);
if (!$this->allocate()) throw new \BNETDocs\Exceptions\ProductNotFoundException($this);
}
}
public function allocate(): bool
{
$this->setBnetProductRaw('');
$this->setBnlsProductId(0);
$this->setLabel('');
$this->setSort(0);
$this->setVersionByte(0);
$id = $this->getBnetProductId();
if (is_null($id)) return true;
$q = MariaDb::instance()->prepare('
SELECT
`bnet_product_id`,
`bnet_product_raw`,
`bnls_product_id`,
`label`,
`sort`,
`version_byte`
FROM `products`
WHERE `bnet_product_id` = ?
LIMIT 1;
');
if (!$q || !$q->execute([$id]) || $q->rowCount() != 1) return false;
$this->allocateObject($q->fetchObject());
$q->closeCursor();
return true;
}
protected function allocateObject(StdClass $value): void
{
$this->setBnetProductId($value->bnet_product_id);
$this->setBnetProductRaw($value->bnet_product_raw);
$this->setBnlsProductId($value->bnls_product_id);
$this->setLabel($value->label);
$this->setSort($value->sort);
$this->setVersionByte($value->version_byte);
}
public function commit(): bool
{
return false;
}
/**
* Deallocates the properties of this object from the database.
*
* @return boolean Whether the operation was successful.
*/
public function deallocate(): bool
{
$id = $this->getBnetProductId();
if (is_null($id)) return false;
$q = MariaDb::instance()->prepare('DELETE FROM `products` WHERE `bnet_product_id` = ? LIMIT 1;');
try { return $q && $q->execute([$id]); }
finally { if ($q) $q->closeCursor(); }
}
public static function getAllProducts(): ?array
{
$q = MariaDb::instance()->prepare('
SELECT
`bnet_product_id`,
`bnet_product_raw`,
`bnls_product_id`,
`label`,
`sort`,
`version_byte`
FROM `products` ORDER BY `sort` ASC;
');
if (!$q || !$q->execute()) return null;
$r = [];
while ($row = $q->fetchObject()) $r[] = new self($row);
$q->closeCursor();
return $r;
}
public static function getProductsFromIds(array $values): array
{
$r = [];
foreach ($values as $value) $r[] = new self($value->bnet_product_id);
return $r;
}
public function getBnetProductId(): ?int
{
return $this->bnet_product_id;
}
public function getBnetProductRaw(): string
{
return $this->bnet_product_raw;
}
public function getBnlsProductId(): int
{
return $this->bnls_product_id;
}
public function getLabel(): string
{
return $this->label;
}
public function getSort(): int
{
return $this->sort;
}
public function getVersionByte(): int
{
return $this->version_byte;
}
public function jsonSerialize(): mixed
{
return [
'bnet_product_id' => $this->getBnetProductId(),
'bnet_product_raw' => $this->getBnetProductRaw(),
'bnls_product_id' => $this->getBnlsProductId(),
'label' => $this->getLabel(),
'sort' => $this->getSort(),
'version_byte' => $this->getVersionByte(),
];
}
public function setBnetProductId(int $value): void
{
$this->bnet_product_id = $value;
}
public function setBnetProductRaw(string $value): void
{
$this->bnet_product_raw = $value;
}
public function setBnlsProductId(int $value): void
{
$this->bnls_product_id = $value;
}
public function setLabel(string $value): void
{
$this->label = $value;
}
public function setSort(int $value): void
{
$this->sort = $value;
}
public function setVersionByte(int $value): void
{
$this->version_byte = $value;
}
}