Passed
Push — master ( 7fa4c0...87d9f9 )
by Filippo
02:21
created

MetaClass::resetMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 = []; //!< Metadata array.
30
31
32
  /**
33
   * @brief Resets the metadata.
34
   */
35
  public function resetMetadata() {
36
    unset($this->meta);
37
    $this->meta = [];
38
  }
39
40
41
  /**
42
   * @brief Returns the metadata.
43
   * @retval mixed
44
   */
45
  public function getMetadata($name) {
46
    return @$this->meta[$name];
47
  }
48
49
50
  /**
51
   * @brief Checks the document for the given attribute.
52
   * @param[in] string $name The attribute name.
53
   * @retval bool
54
   */
55
  public function isMetadataPresent($name) {
56
    return (array_key_exists($name, $this->meta)) ? TRUE : FALSE;
57
  }
58
59
60
  /**
61
   * @brief Sets the metadata to the provided value.
62
   * @param[in] string $name The metadata name.
63
   * @param[in] mixed $value The metadata value.
64
   * @param[in] bool $override When `true` overrides the metadata value.
65
   * @param[in] bool $allowNull When `true` allows a `null` value.
66
   */
67
  public function setMetadata($name, $value, $override = TRUE, $allowNull = TRUE) {
68
    if (is_null($value) && !$allowNull)
69
      return;
70
71
    if ($this->isMetadataPresent($name) && !$override)
72
      return;
73
74
    $this->meta[$name] = $value;
75
  }
76
77
78
  /**
79
   * @brief Removes an metadata previously set.
80
   * @param[in] string $name The metadata name.
81
   */
82
  public function unsetMetadata($name) {
83
    if (array_key_exists($name, $this->meta))
84
      unset($this->meta[$name]);
85
  }
86
87
88
  /**
89
   * @brief Given a JSON object, this function assigns every single object's property to the `$meta` array, the array
90
   * that stores the document's metadata.
91
   * @param[in] string $json A JSON object.
92
   */
93
  public function assignJson($json) {
94
    $this->meta = array_merge($this->meta, ArrayHelper::fromJson($json, TRUE));
95
  }
96
97
98
  /**
99
   * @brief Assigns the given associative array to the `$meta` array, the array that stores the document's metadata.
100
   * @param[in] array $array An associative array.
101
   */
102
  public function assignArray(array $array) {
103
    if (ArrayHelper::isAssociative($array)) {
104
      $this->meta = array_merge($this->meta, $array);
105
    }
106
    else
107
      throw new \InvalidArgumentException("\$array must be an associative array.");
108
  }
109
110
111
  /**
112
   * @brief Given an instance of a standard class, this function assigns every single object's property to the `$meta`
113
   * array, the array that stores the document's metadata.
114
   */
115
  public function assignObject(\stdClass $object) {
116
    $this->meta = array_merge($this->meta, get_object_vars($object));
117
  }
118
119
120
  /**
121
   * @brief Returns the document representation as a JSON object.
122
   * @retval JSON object
123
   */
124
  public function asJson() {
125
    $json = json_encode($this->meta,
126
      JSON_UNESCAPED_UNICODE |
127
      JSON_PARTIAL_OUTPUT_ON_ERROR |
0 ignored issues
show
Bug introduced by
The constant ToolBag\Meta\JSON_PARTIAL_OUTPUT_ON_ERROR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
128
      JSON_PRESERVE_ZERO_FRACTION
129
    );
130
131
    if ($json === FALSE)
132
      throw new JSONErrorException(json_last_error_msg());
133
134
    return $json;
135
  }
136
137
138
  /**
139
   * @brief Returns the document representation as an associative array.
140
   * @retval array An associative array
141
   */
142
  public function asArray() {
143
    return $this->meta;
144
  }
145
146
}