|
1
|
|
|
<?php |
|
2
|
|
|
/** Object */ |
|
3
|
|
|
|
|
4
|
|
|
namespace Battis\SharedLogs; |
|
5
|
|
|
|
|
6
|
|
|
use Battis\SharedLogs\Exceptions\ObjectException; |
|
7
|
|
|
use JsonSerializable; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* A generic object bound to a database table |
|
11
|
|
|
|
|
12
|
|
|
* Implements `JsonSerializable` for easy output as a JSON response via the API. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Seth Battis <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class AbstractObject implements JsonSerializable |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Construct an object from the corresponding row of a bound database table |
|
20
|
|
|
* |
|
21
|
|
|
* @param array $databaseRecord Associative array of fields |
|
22
|
|
|
* |
|
23
|
|
|
* @uses Object::validateParams() |
|
24
|
|
|
* |
|
25
|
|
|
* @throws ObjectException If an empty database record is provided |
|
26
|
|
|
*/ |
|
27
|
20 |
|
public function __construct($databaseRecord) |
|
28
|
|
|
{ |
|
29
|
20 |
|
foreach ($this->validateParams($databaseRecord) as $key => $value) { |
|
30
|
19 |
|
$this->$key = $value; |
|
31
|
|
|
} |
|
32
|
19 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Validate (and filter) a row of database information before applying it to the object |
|
36
|
|
|
* |
|
37
|
|
|
* @param array $databaseRow Associative array |
|
38
|
|
|
* @return array Filtered and validated |
|
39
|
|
|
* @throws ObjectException If no parameters are provided |
|
40
|
|
|
*/ |
|
41
|
20 |
|
protected function validateParams($databaseRow) |
|
42
|
|
|
{ |
|
43
|
|
|
/* TODO Convert MySQL timestamp to SOAP timestamp for portability */ |
|
44
|
20 |
|
if (is_array($databaseRow)) { |
|
45
|
19 |
|
return $databaseRow; |
|
46
|
|
|
} else { |
|
47
|
3 |
|
throw new ObjectException( |
|
48
|
3 |
|
'No parameters provided to from which construct object', |
|
49
|
3 |
|
ObjectException::MISSING_DATABASE_RECORD |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Prepare the object for JSON serialization |
|
56
|
|
|
* |
|
57
|
|
|
* @see JsonSerializable::jsonSerialize() |
|
58
|
|
|
* |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
8 |
|
public function jsonSerialize() |
|
62
|
|
|
{ |
|
63
|
8 |
|
return get_object_vars($this); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|