Completed
Push — master ( 7f1b4a...afb6c0 )
by Filippo
02:31
created

TDoc   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 36
lcom 1
cbo 2
dl 0
loc 161
rs 8.8
c 0
b 0
f 0

27 Methods

Rating   Name   Duplication   Size   Complexity  
fixDocId() 0 1 ?
getPath() 0 1 ?
A resetMetadata() 0 4 1
A getMetadata() 0 3 1
A isMetadataPresent() 0 3 2
B setMetadata() 0 9 5
A unsetMetadata() 0 4 2
A setClass() 0 3 1
A setType() 0 3 1
A getType() 0 3 1
A hasType() 0 3 1
A assignJson() 0 4 1
A assignArray() 0 8 2
A assignObject() 0 4 1
A asJson() 0 10 2
A asArray() 0 3 1
A delete() 0 3 1
A isDeleted() 0 3 1
A getRevisions() 0 3 2
A getId() 0 3 1
A issetId() 0 3 1
A setId() 0 3 1
A unsetId() 0 4 2
A getRev() 0 3 1
A issetRev() 0 3 1
A setRev() 0 3 1
A unsetRev() 0 4 2
1
<?php
2
3
/**
4
 * @file TDoc.php
5
 * @brief This file contains the TDoc trait.
6
 * @details
7
 * @author Filippo F. Fadda
8
 */
9
10
11
namespace EoC\Doc;
12
13
14
use EoC\Extension\TProperty;
15
use EoC\Helper;
16
17
18
/**
19
 * @brief Implements the IDoc interface and add many functions.
20
 * @see AbstractDoc.dox
21
 */
22
trait TDoc {
23
  use TProperty;
24
25
  protected $meta = [];
26
27
28
  abstract protected function fixDocId();
29
30
31
  abstract public function getPath();
32
33
34
  public function resetMetadata() {
35
    unset($this->meta);
36
    $this->meta = [];
37
  }
38
39
40
  public function getMetadata($name) {
41
    return @$this->meta[$name];
42
  }
43
44
45
  public function isMetadataPresent($name) {
46
    return (array_key_exists($name, $this->meta)) ? TRUE : FALSE;
47
  }
48
49
50
  public function setMetadata($name, $value, $override = TRUE, $allowNull = TRUE) {
51
    if (is_null($value) && !$allowNull)
52
      return;
53
54
    if ($this->isMetadataPresent($name) && !$override)
55
      return;
56
57
    $this->meta[$name] = $value;
58
  }
59
60
61
  public function unsetMetadata($name) {
62
    if (array_key_exists($name, $this->meta))
63
      unset($this->meta[$name]);
64
  }
65
66
67
  public function setClass($value) {
68
    $this->meta['class'] = $value;
69
  }
70
71
72
  public function setType($value) {
73
    $this->meta['type'] = $value;
74
  }
75
76
77
  public function getType() {
78
    return $this->meta['type'];
79
  }
80
81
82
  public function hasType() {
83
    return FALSE;
84
  }
85
86
87
  public function assignJson($json) {
88
    $this->meta = array_merge($this->meta, Helper\ArrayHelper::fromJson($json, TRUE));
89
    $this->fixDocId();
90
  }
91
92
93
  public function assignArray(array $array) {
94
    if (Helper\ArrayHelper::isAssociative($array)) {
95
      $this->meta = array_merge($this->meta, $array);
96
      $this->fixDocId();
97
    }
98
    else
99
      throw new \InvalidArgumentException("\$array must be an associative array.");
100
  }
101
102
103
  public function assignObject(\stdClass $object) {
104
    $this->meta = array_merge($this->meta, get_object_vars($object));
105
    $this->fixDocId();
106
  }
107
108
109
  public function asJson() {
110
    $json = json_encode($this->meta,
111
        JSON_UNESCAPED_UNICODE |
112
        JSON_PARTIAL_OUTPUT_ON_ERROR |
113
        JSON_PRESERVE_ZERO_FRACTION
114
    );
115
116
    if ($json === FALSE)
117
      throw new \RuntimeException(json_last_error_msg());
118
  }
119
120
121
  public function asArray() {
122
    return $this->meta;
123
  }
124
125
126
  public function delete() {
127
    $this->meta['_deleted'] = TRUE;
128
  }
129
130
131
  public function isDeleted() {
132
    return $this->meta['_deleted'];
133
  }
134
135
136
  public function getRevisions() {
137
    return (array_key_exists('_revisions', $this->meta)) ? $this->meta['_revisions'] : NULL;
138
  }
139
140
141
  public function getId() {
142
    return $this->meta['_id'];
143
  }
144
145
146
  public function issetId() {
147
    return isset($this->meta['_id']);
148
  }
149
150
151
  public function setId($value) {
152
    $this->meta['_id'] = (string)$value;
153
  }
154
155
156
  public function unsetId() {
157
    if ($this->isMetadataPresent('_id'))
158
      unset($this->meta['_id']);
159
  }
160
161
162
  public function getRev() {
163
    return $this->meta['_rev'];
164
  }
165
166
167
  public function issetRev() {
168
    return isset($this->meta['_rev']);
169
  }
170
171
172
  public function setRev($value) {
173
    $this->meta['_rev'] = (string)$value;
174
  }
175
176
177
  public function unsetRev() {
178
    if ($this->isMetadataPresent('_rev'))
179
      unset($this->meta['_rev']);
180
  }
181
182
}