Passed
Push — master ( 788d88...4b8f5b )
by Filippo
04:10 queued 02:17
created

MetaClass::setMetadata()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 5
nc 3
nop 4
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 |
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...
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
}