-
Notifications
You must be signed in to change notification settings - Fork 15
/
BaseModel.php
155 lines (141 loc) · 3.38 KB
/
BaseModel.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
<?php
namespace Aternos\Model;
/**
* Class BaseModel
*
* Contains all non-driver related functions such as id and
* changed fields, use this to build models with custom driver
* logic, but without writing everything from scratch
*
* @package Aternos\Model
*/
abstract class BaseModel implements ModelInterface
{
/**
* Name of the field used as unique identifier
*
* @var string
*/
protected static string $idField = "id";
/**
* Length of the random generated unique identifier
*
* @var int
*/
protected static int $idLength = 16;
/**
* Should be added as public property to all inheriting models
*
* It's protected to be easily replaced by a property with
* a different name if that's required
*
* @var mixed
*/
protected mixed $id;
/**
* Additional fields that aren't part of the model, but occur in the result
*
* e.g. results of calculations, aliased fields etc.
*
* @var array
*/
protected array $additionalFields = [];
/**
* Get the field name of the unique identifier
*
* @return string
*/
public static function getIdField(): string
{
return static::$idField;
}
/**
* @param array $rawData
* @return static|null
*/
public static function getModelFromData(array $rawData): ?static
{
$model = new static();
return $model->applyData($rawData);
}
/**
* Model constructor.
*
* @param mixed $id
*/
public function __construct(mixed $id = null)
{
if ($id) {
$this->setId($id);
}
}
/**
* Get the unique identifier of the model
*
* @return mixed
*/
public function getId(): mixed
{
if (!isset($this->{static::$idField})) {
return null;
}
return $this->{static::$idField};
}
/**
* Set the unique identifier
*
* @param mixed $id
* @return $this
*/
public function setId(mixed $id): static
{
$this->{static::$idField} = $id;
return $this;
}
/**
* Generate an unique identifier for the model
*/
protected function generateId()
{
$characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$charactersLength = strlen($characters);
do {
$id = '';
for ($i = 0; $i < static::$idLength; $i++) {
$id .= $characters[rand(0, $charactersLength - 1)];
}
} while (static::get($id));
$this->setId($id);
}
/**
* Apply data to the model
*
* @param array $rawData
* @return $this
*/
public function applyData(array $rawData): static
{
foreach ($rawData as $key => $value) {
if (property_exists($this, $key)) {
$this->{$key} = $value;
} else {
$this->additionalFields[$key] = $value;
}
}
return $this;
}
/**
* Get a field value, even if it's not part of the defined model
*
* @param string $key
* @return mixed
*/
public function getField(string $key): mixed
{
if (property_exists($this, $key)) {
return $this->{$key};
} else {
return $this->additionalFields[$key] ?? null;
}
}
}