-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathType.php
More file actions
121 lines (104 loc) · 2.98 KB
/
Copy pathType.php
File metadata and controls
121 lines (104 loc) · 2.98 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
<?php
namespace BNETDocs\Libraries\Server;
use \BNETDocs\Libraries\Db\MariaDb;
use \OutOfBoundsException;
use \StdClass;
class Type implements \BNETDocs\Interfaces\DatabaseObject, \JsonSerializable
{
public const MAX_ID = 0xFFFFFFFFFFFFFFFF; // bigint(20) unsigned
public const MAX_LABEL = 0xFF; // varchar(255)
protected ?int $id;
protected string $label;
public function __construct(StdClass|int|null $value)
{
if ($value instanceof StdClass)
{
$this->allocateObject($value);
}
else
{
$this->setId($value);
if (!$this->allocate()) throw new \UnexpectedValueException();
}
}
public function allocate(): bool
{
$this->setLabel('');
$id = $this->getId();
if (is_null($id)) return true;
$q = MariaDb::instance()->prepare('SELECT `id`, `label` FROM `server_types` WHERE `id` = ? LIMIT 1;');
if (!$q || !$q->execute([$id]) || $q->rowCount() != 1) return false;
$this->allocateObject($q->fetchObject());
$q->closeCursor();
return true;
}
private function allocateObject(StdClass $value): void
{
$this->setId($value->id);
$this->setLabel($value->label);
}
public function commit(): bool
{
$q = MariaDb::instance()->prepare('UPDATE `server_types` SET `id` = :id, `label` = :label WHERE `id` = :id LIMIT 1;');
$p = [':id' => $this->getId(), ':label' => $this->getLabel()];
try { return $q && $q->execute($p) && $q->rowCount() === 1; }
finally { if ($q) $q->closeCursor(); }
}
/**
* Deallocates the properties of this object from the database.
*
* @return boolean Whether the operation was successful.
*/
public function deallocate(): bool
{
$id = $this->getId();
if (is_null($id)) return false;
$q = MariaDb::instance()->prepare('DELETE FROM `server_types` WHERE `id` = ? LIMIT 1;');
try { return $q && $q->execute([$id]); }
finally { $q->closeCursor(); }
}
public static function getAllServerTypes(): ?array
{
$q = MariaDb::instance()->prepare('SELECT `id`, `label` FROM `server_types` ORDER BY `id` ASC;');
if (!$q || !$q->execute()) return null;
$r = [];
while ($row = $q->fetchObject()) $r[] = new self($row);
$q->closeCursor();
return $r;
}
public function getId(): ?int
{
return $this->id;
}
public function getLabel(): string
{
return $this->label;
}
public function jsonSerialize(): mixed
{
return [
'id' => $this->getId(),
'label' => $this->getLabel(),
];
}
public function setId(?int $value): void
{
if ($value < 0 || $value > self::MAX_ID)
{
throw new OutOfBoundsException(sprintf(
'value must be null or an integer between 0-%d', self::MAX_ID
));
}
$this->id = $value;
}
public function setLabel(string $value): void
{
if (strlen($value) > self::MAX_LABEL)
{
throw new OutOfBoundsException(sprintf(
'value must be a string between 0-%d characters', self::MAX_LABEL
));
}
$this->label = $value;
}
}