dedalozzo /
meta
| 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 Meta; |
||
| 13 | |||
| 14 | |||
| 15 | use Meta\Extension\TProperty; |
||
| 16 | |||
| 17 | use ToolBag\Helper\ArrayHelper; |
||
| 18 | use ToolBag\Exception\JSONErrorException; |
||
| 19 | |||
| 20 | use InvalidArgumentException; |
||
| 21 | |||
| 22 | |||
| 23 | /** |
||
| 24 | * @brief This abstract class provides a set of methods to store and access metadata. |
||
| 25 | * @details Metadata are accessible through properties defined by the subclass that extends the `MetaClass` itself. |
||
| 26 | * In fact, the class is using the `TProperty` extension. |
||
| 27 | * @nosubgrouping |
||
| 28 | */ |
||
| 29 | abstract class MetaClass { |
||
| 30 | use TProperty; |
||
| 31 | |||
| 32 | protected $meta = []; //!< Metadata array. |
||
| 33 | |||
| 34 | |||
| 35 | /** |
||
| 36 | * @brief Resets the metadata. |
||
| 37 | */ |
||
| 38 | public function resetMetadata() { |
||
| 39 | unset($this->meta); |
||
| 40 | $this->meta = []; |
||
| 41 | } |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * @brief Returns the metadata. |
||
| 46 | * @return mixed |
||
| 47 | */ |
||
| 48 | public function getMetadata($name) { |
||
| 49 | return @$this->meta[$name]; |
||
| 50 | } |
||
| 51 | |||
| 52 | |||
| 53 | /** |
||
| 54 | * @brief Checks the document for the given attribute. |
||
| 55 | * @param string $name The attribute name. |
||
| 56 | * @return bool |
||
| 57 | */ |
||
| 58 | public function isMetadataPresent($name) { |
||
| 59 | return (array_key_exists($name, $this->meta)) ? TRUE : FALSE; |
||
| 60 | } |
||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * @brief Sets the metadata to the provided value. |
||
| 65 | * @param string $name The metadata name. |
||
| 66 | * @param mixed $value The metadata value. |
||
| 67 | * @param bool $override When `true` overrides the metadata value. |
||
| 68 | * @param bool $allowNull When `true` allows a `null` value. |
||
| 69 | */ |
||
| 70 | public function setMetadata($name, $value, $override = TRUE, $allowNull = TRUE) { |
||
| 71 | if (is_null($value) && !$allowNull) |
||
| 72 | return; |
||
| 73 | |||
| 74 | if ($this->isMetadataPresent($name) && !$override) |
||
| 75 | return; |
||
| 76 | |||
| 77 | $this->meta[$name] = $value; |
||
| 78 | } |
||
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * @brief Removes an metadata previously set. |
||
| 83 | * @param string $name The metadata name. |
||
| 84 | */ |
||
| 85 | public function unsetMetadata($name) { |
||
| 86 | if (array_key_exists($name, $this->meta)) |
||
| 87 | unset($this->meta[$name]); |
||
| 88 | } |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * @brief Given a JSON object, this function assigns every single object's property to the `$meta` array, the array |
||
| 93 | * that stores the document's metadata. |
||
| 94 | * @param string $json A JSON object. |
||
| 95 | */ |
||
| 96 | public function assignJson($json) { |
||
| 97 | $this->meta = array_merge($this->meta, ArrayHelper::fromJson($json, TRUE)); |
||
| 98 | } |
||
| 99 | |||
| 100 | |||
| 101 | /** |
||
| 102 | * @brief Assigns the given associative array to the `$meta` array, the array that stores the document's metadata. |
||
| 103 | * @param array $array An associative array. |
||
| 104 | * @throws InvalidArgumentException |
||
| 105 | */ |
||
| 106 | public function assignArray(array $array) { |
||
| 107 | if (ArrayHelper::isAssociative($array)) { |
||
| 108 | $this->meta = array_merge($this->meta, $array); |
||
| 109 | } |
||
| 110 | else |
||
| 111 | throw new InvalidArgumentException("\$array must be an associative array."); |
||
| 112 | } |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * @brief Given an instance of a standard class, this function assigns every single object's property to the `$meta` |
||
| 117 | * array, the array that stores the document's metadata. |
||
| 118 | */ |
||
| 119 | public function assignObject(\stdClass $object) { |
||
| 120 | $this->meta = array_merge($this->meta, get_object_vars($object)); |
||
| 121 | } |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * @brief Returns the document representation as a JSON object. |
||
| 126 | * @return string A JSON object. |
||
| 127 | * @throws JSONErrorException |
||
| 128 | */ |
||
| 129 | public function asJson() { |
||
| 130 | $json = json_encode($this->meta, |
||
| 131 | JSON_UNESCAPED_UNICODE | |
||
| 132 | JSON_PARTIAL_OUTPUT_ON_ERROR | |
||
| 133 | JSON_PRESERVE_ZERO_FRACTION |
||
| 134 | ); |
||
| 135 | |||
| 136 | if ($json === FALSE) |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 137 | throw new JSONErrorException(json_last_error_msg()); |
||
| 138 | |||
| 139 | return $json; |
||
| 140 | } |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * @brief Returns the document representation as an associative array. |
||
| 145 | * @return array An associative array. |
||
| 146 | */ |
||
| 147 | public function asArray() { |
||
| 148 | return $this->meta; |
||
| 149 | } |
||
| 150 | |||
| 151 | } |