|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @file MetaClass.php |
|
5
|
|
|
* @brief This file contains the MetaClass abstract class. |
|
6
|
|
|
* @details |
|
7
|
|
|
* @author Filippo F. Fadda |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
//! Meta classes |
|
12
|
|
|
namespace ToolBag\Meta; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
use ToolBag\Extension\TProperty; |
|
16
|
|
|
use ToolBag\Helper\ArrayHelper; |
|
17
|
|
|
use ToolBag\Exception\JSONErrorException; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @brief This abstract class provides a set of methods to store and access metadata. |
|
22
|
|
|
* @details Metadata are accessible through properties defined by the subclass that extends the `MetaClass` itself. |
|
23
|
|
|
* In fact, the class is using the `TProperty` extension. |
|
24
|
|
|
* @nosubgrouping |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract class MetaClass { |
|
27
|
|
|
use TProperty; |
|
28
|
|
|
|
|
29
|
|
|
protected $meta = []; |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
public function __construct() { |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
public function getMetadata($name) { |
|
37
|
|
|
return @$this->meta[$name]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
public function isMetadataPresent($name) { |
|
42
|
|
|
return (array_key_exists($name, $this->meta)) ? TRUE : FALSE; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
public function setMetadata($name, $value, $override = TRUE, $allowNull = TRUE) { |
|
47
|
|
|
if (is_null($value) && !$allowNull) |
|
48
|
|
|
return; |
|
49
|
|
|
|
|
50
|
|
|
if ($this->isMetadataPresent($name) && !$override) |
|
51
|
|
|
return; |
|
52
|
|
|
|
|
53
|
|
|
$this->meta[$name] = $value; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
public function unsetMetadata($name) { |
|
58
|
|
|
if (array_key_exists($name, $this->meta)) |
|
59
|
|
|
unset($this->meta[$name]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
public function assignJson($json) { |
|
64
|
|
|
$this->meta = array_merge($this->meta, ArrayHelper::fromJson($json, TRUE)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
public function assignArray(array $array) { |
|
69
|
|
|
if (ArrayHelper::isAssociative($array)) { |
|
70
|
|
|
$this->meta = array_merge($this->meta, $array); |
|
71
|
|
|
} |
|
72
|
|
|
else |
|
73
|
|
|
throw new \InvalidArgumentException("\$array must be an associative array."); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
public function assignObject(\stdClass $object) { |
|
78
|
|
|
$this->meta = array_merge($this->meta, get_object_vars($object)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @brief Returns the document representation as a JSON object. |
|
84
|
|
|
* @retval JSON object |
|
85
|
|
|
*/ |
|
86
|
|
|
public function asJson() { |
|
87
|
|
|
$json = json_encode($this->meta, |
|
88
|
|
|
JSON_UNESCAPED_UNICODE | |
|
89
|
|
|
JSON_PARTIAL_OUTPUT_ON_ERROR | |
|
|
|
|
|
|
90
|
|
|
JSON_PRESERVE_ZERO_FRACTION |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
if ($json === FALSE) |
|
94
|
|
|
throw new JSONErrorException(json_last_error_msg()); |
|
95
|
|
|
|
|
96
|
|
|
return $json; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @brief Returns the document representation as an associative array. |
|
102
|
|
|
* @retval array An associative array |
|
103
|
|
|
*/ |
|
104
|
|
|
public function asArray() { |
|
105
|
|
|
return $this->meta; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
} |