1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Opeyemiabiodun\PotatoORM\Models; |
4
|
|
|
|
5
|
|
|
use RuntimeException; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Opeyemiabiodun\PotatoORM\Connections\Connection; |
8
|
|
|
use Opeyemiabiodun\PotatoORM\Connections\ConnectionFactory; |
9
|
|
|
use Opeyemiabiodun\PotatoORM\Exceptions\AssignmentException; |
10
|
|
|
use Opeyemiabiodun\PotatoORM\Exceptions\PropertyNotFoundException; |
11
|
|
|
|
12
|
|
|
trait Model |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The model's attributes. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $_attributes = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The model's database connection. |
23
|
|
|
* |
24
|
|
|
* @var Opeyemiabiodun\PotatoORM\Connections\Connection |
25
|
|
|
*/ |
26
|
|
|
protected static $_connection; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The primary key of the model's database table. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected static $_primaryKey; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The model's database table. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected static $_table; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The model's constructor method. |
44
|
|
|
* |
45
|
|
|
* @param Connection|null $connection An Opeyemiabiodun\PotatoORM\Connections\Connection instance or null |
46
|
|
|
* @param string $table The name of the model's table in the database |
47
|
|
|
*/ |
48
|
8 |
|
public function __construct($array = [], Connection $connection = null, $table = null) |
49
|
|
|
{ |
50
|
8 |
|
if (null === $connection) { |
51
|
8 |
|
$connection = ConnectionFactory::load(); |
52
|
8 |
|
} |
53
|
|
|
|
54
|
8 |
|
if (null === $table) { |
55
|
7 |
|
$table = strtolower(substr(get_class($this), strripos(get_class($this), '\\') + 1)).'_table'; |
56
|
7 |
|
} |
57
|
|
|
|
58
|
8 |
|
$this->setConnection($connection); |
59
|
8 |
|
$this->setTable($table); |
60
|
|
|
|
61
|
7 |
|
if (! empty($array)) { |
62
|
1 |
|
$this->setProperties($array); |
|
|
|
|
63
|
1 |
|
} |
64
|
7 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The getter method for the model's properties. |
68
|
|
|
* |
69
|
|
|
* @param string $property The particular property |
70
|
|
|
* |
71
|
|
|
* @return int|float|string|bool The value of the property |
72
|
|
|
*/ |
73
|
2 |
|
public function __get($property) |
74
|
|
|
{ |
75
|
2 |
View Code Duplication |
if (array_key_exists($property, $this->_attributes)) { |
|
|
|
|
76
|
1 |
|
return $this->_attributes[$property]; |
77
|
|
|
} else { |
78
|
1 |
|
throw new PropertyNotFoundException('The '.get_class($this)." instance has no {$property} property."); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* The setter method for the model's properties. |
84
|
|
|
* |
85
|
|
|
* @param string $property The particular property |
86
|
|
|
* @param int|float|string|bool $value The value of the property |
87
|
|
|
*/ |
88
|
4 |
|
public function __set($property, $value) |
89
|
|
|
{ |
90
|
4 |
|
if (! is_scalar($value)) { |
91
|
1 |
|
throw new AssignmentException("Only scalar values can be assigned to the {$property} property."); |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
View Code Duplication |
if (array_key_exists($property, $this->_attributes)) { |
|
|
|
|
95
|
2 |
|
$this->_attributes[$property] = $value; |
96
|
2 |
|
} else { |
97
|
1 |
|
throw new PropertyNotFoundException('The '.get_class($this)." instance has no {$property} property."); |
98
|
|
|
} |
99
|
2 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Deletes a specified instance of the model in the database. |
103
|
|
|
* |
104
|
|
|
* @param int $number Specifies which model instance to delete; the 1st, 2nd, 3rd, ..... |
105
|
|
|
* |
106
|
|
|
* @return bool Returns boolean true if the instance was successfully deleted or else it returns false. |
107
|
|
|
*/ |
108
|
3 |
|
public static function destroy($number) |
109
|
|
|
{ |
110
|
3 |
|
if (! is_int($number)) { |
111
|
1 |
|
throw new InvalidArgumentException("The parameter {$number} is not an integer. An integer is required instead."); |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
if ($number <= 0) { |
115
|
1 |
|
throw new InvalidArgumentException("The parameter {$number} is not a positive integer. A positive integer is required instead."); |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
return self::$_connection->deleteRecord(self::$_table, $number - 1); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Finds a specified instance of the model in the database. |
123
|
|
|
* |
124
|
|
|
* @param int $number Specifies which model instance to find; the 1st, 2nd, 3rd, ..... |
125
|
|
|
* |
126
|
|
|
* @return array Returns the particular instance of the model. |
127
|
|
|
*/ |
128
|
3 |
|
public static function find($number) |
129
|
|
|
{ |
130
|
3 |
|
if (! is_int($number)) { |
131
|
1 |
|
throw new InvalidArgumentException("The parameter {$number} is not an integer. An integer is required instead."); |
132
|
|
|
} |
133
|
|
|
|
134
|
2 |
|
if ($number <= 0) { |
135
|
1 |
|
throw new InvalidArgumentException("The parameter {$number} is not a positive integer. A positive integer is required instead."); |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
$result = new self(); |
139
|
|
|
|
140
|
1 |
|
$record = self::$_connection->findRecord(self::$_table, $number - 1); |
141
|
|
|
|
142
|
1 |
|
$result->setProperties($record[0]); |
143
|
|
|
|
144
|
1 |
|
return $result; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns all instances of the model in the database. |
149
|
|
|
* |
150
|
|
|
* @return array All instances of the model in the database. |
151
|
|
|
*/ |
152
|
1 |
|
public static function getAll() |
153
|
|
|
{ |
154
|
1 |
|
$records = self::$_connection->getAllRecords(self::$_table); |
155
|
|
|
|
156
|
1 |
|
$count = count($records); |
157
|
1 |
|
$result = []; |
158
|
|
|
|
159
|
1 |
|
for ($i = 0; $i < $count; $i++) { |
160
|
1 |
|
array_push($result, new self($records[$i])); |
161
|
1 |
|
} |
162
|
|
|
|
163
|
1 |
|
return $result; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Checks the attributes of the model to ensure they are not all null. |
168
|
|
|
* |
169
|
|
|
* @return bool true if at least one of the models's attributes is not null else false. |
170
|
|
|
*/ |
171
|
3 |
|
private function hasAttributes() |
172
|
|
|
{ |
173
|
3 |
|
$hasAttributes = false; |
174
|
|
|
|
175
|
3 |
|
foreach ($this->_attributes as $key => $value) { |
176
|
3 |
|
if (! is_null($value)) { |
177
|
2 |
|
$hasAttributes = true; |
178
|
2 |
|
} |
179
|
3 |
|
} |
180
|
|
|
|
181
|
3 |
|
return $hasAttributes; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Saves or updates an instance of the model in the database. |
186
|
|
|
* |
187
|
|
|
* @return bool Returns true if the operation was successfully else returns false. |
188
|
|
|
*/ |
189
|
3 |
|
public function save() |
190
|
|
|
{ |
191
|
3 |
|
if (! $this->hasAttributes()) { |
192
|
1 |
|
throw new RuntimeException(get_class($this).' model has nothing to persist to the database.'); |
193
|
|
|
} |
194
|
|
|
|
195
|
2 |
|
$pk = (empty($this->_attributes[self::$_primaryKey])) ? 'NULL' : $this->_attributes[self::$_primaryKey]; |
196
|
|
|
|
197
|
|
|
//$record = self::$_connection->findRecord(self::$_table, (string) $pk); |
|
|
|
|
198
|
|
|
|
199
|
2 |
|
if ('NULL' === $pk) { |
200
|
1 |
|
return self::$_connection->createRecord(self::$_table, $this->_attributes); |
201
|
|
|
} else { |
202
|
1 |
|
return self::$_connection->updateRecord(self::$_table, (string) $pk, $this->_attributes); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Sets the model's connection. |
208
|
|
|
* |
209
|
|
|
* @param Opeyemiabiodun\PotatoORM\Connections\Connection $connection An instance of Opeyemiabiodun\PotatoORM\Connections\Connection. |
210
|
|
|
*/ |
211
|
8 |
|
protected function setConnection(Connection $connection) |
212
|
|
|
{ |
213
|
8 |
|
self::$_connection = $connection; |
|
|
|
|
214
|
8 |
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Sets the model's properties. |
218
|
|
|
* |
219
|
|
|
* @param string $array An array containing the model's poperties. |
220
|
|
|
*/ |
221
|
2 |
|
public function setProperties($array) |
222
|
|
|
{ |
223
|
2 |
|
foreach ($this->_attributes as $key => $value) { |
224
|
2 |
|
if (! empty($array[$key])) { |
225
|
2 |
|
$this->_attributes[$key] = $array[$key]; |
226
|
2 |
|
} |
227
|
2 |
|
} |
228
|
2 |
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Sets the model's table. |
232
|
|
|
* |
233
|
|
|
* @param string $table An existing table in the database. |
234
|
|
|
*/ |
235
|
8 |
|
protected function setTable($table) |
236
|
|
|
{ |
237
|
8 |
|
if (gettype($table) !== 'string') { |
238
|
1 |
|
throw new InvalidArgumentException("The parameter {$table} is not a string. A string is required instead."); |
239
|
|
|
} |
240
|
|
|
|
241
|
7 |
|
self::$_table = $table; |
242
|
|
|
|
243
|
7 |
|
$columns = self::$_connection->getColumns($table); |
244
|
|
|
|
245
|
7 |
|
for ($i = 0; $i < count($columns); $i++) { |
|
|
|
|
246
|
7 |
|
$this->_attributes[$columns[$i]['column_name']] = null; |
247
|
7 |
|
} |
248
|
|
|
|
249
|
7 |
|
self::$_primaryKey = self::$_connection->getPrimaryKey($table); |
250
|
7 |
|
} |
251
|
|
|
} |
252
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: