JsonapiObject   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 46
c 2
b 0
f 2
dl 0
loc 131
ccs 48
cts 48
cp 1
rs 10
wmc 26

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setMetaObject() 0 2 1
A addProfile() 0 2 1
A setVersion() 0 2 1
B toArray() 0 29 10
A addExtension() 0 2 1
B isEmpty() 0 21 8
A addMeta() 0 6 2
A __construct() 0 3 2
1
<?php
2
3
namespace alsvanzelf\jsonapi\objects;
4
5
use alsvanzelf\jsonapi\Document;
6
use alsvanzelf\jsonapi\helpers\AtMemberManager;
7
use alsvanzelf\jsonapi\helpers\ExtensionMemberManager;
8
use alsvanzelf\jsonapi\interfaces\ExtensionInterface;
9
use alsvanzelf\jsonapi\interfaces\ObjectInterface;
10
use alsvanzelf\jsonapi\interfaces\ProfileInterface;
11
use alsvanzelf\jsonapi\objects\MetaObject;
12
13
class JsonapiObject implements ObjectInterface {
14
	use AtMemberManager, ExtensionMemberManager;
15
	
16
	/** @var string */
17
	protected $version;
18
	/** @var ExtensionInterface[] */
19
	protected $extensions = [];
20
	/** @var ProfileInterface */
21
	protected $profiles = [];
22
	/** @var MetaObject */
23
	protected $meta;
24
	
25
	/**
26
	 * @param string $version one of the Document::JSONAPI_VERSION_* constants, optional, defaults to Document::JSONAPI_VERSION_LATEST
27
	 */
28 127
	public function __construct($version=Document::JSONAPI_VERSION_LATEST) {
29 127
		if ($version !== null) {
30 122
			$this->setVersion($version);
31
		}
32
	}
33
	
34
	/**
35
	 * human api
36
	 */
37
	
38
	/**
39
	 * @param string $key
40
	 * @param mixed  $value
41
	 */
42 4
	public function addMeta($key, $value) {
43 4
		if ($this->meta === null) {
44 4
			$this->setMetaObject(new MetaObject());
45
		}
46
		
47 4
		$this->meta->add($key, $value);
48
	}
49
	
50
	/**
51
	 * spec api
52
	 */
53
	
54
	/**
55
	 * @param string $version
56
	 */
57 122
	public function setVersion($version) {
58 122
		$this->version = $version;
59
	}
60
	
61
	/**
62
	 * @param ExtensionInterface $extension
63
	 */
64 8
	public function addExtension(ExtensionInterface $extension) {
65 8
		$this->extensions[] = $extension;
66
	}
67
	
68
	/**
69
	 * @param ProfileInterface $profile
70
	 */
71 12
	public function addProfile(ProfileInterface $profile) {
72 12
		$this->profiles[] = $profile;
73
	}
74
	
75
	/**
76
	 * @param MetaObject $metaObject
77
	 */
78 6
	public function setMetaObject(MetaObject $metaObject) {
79 6
		$this->meta = $metaObject;
80
	}
81
	
82
	/**
83
	 * ObjectInterface
84
	 */
85
	
86
	/**
87
	 * @inheritDoc
88
	 */
89 91
	public function isEmpty() {
90 91
		if ($this->version !== null) {
91 86
			return false;
92
		}
93 5
		if ($this->extensions !== []) {
94 1
			return false;
95
		}
96 5
		if ($this->profiles !== []) {
97 1
			return false;
98
		}
99 5
		if ($this->meta !== null && $this->meta->isEmpty() === false) {
100 1
			return false;
101
		}
102 5
		if ($this->hasAtMembers()) {
103 1
			return false;
104
		}
105 5
		if ($this->hasExtensionMembers()) {
106 1
			return false;
107
		}
108
		
109 5
		return true;
110
	}
111
	
112
	/**
113
	 * @inheritDoc
114
	 */
115 87
	public function toArray() {
116 87
		$array = [];
117
		
118 87
		if ($this->hasAtMembers()) {
119 2
			$array = array_merge($array, $this->getAtMembers());
120
		}
121 87
		if ($this->hasExtensionMembers()) {
122 1
			$array = array_merge($array, $this->getExtensionMembers());
123
		}
124 87
		if ($this->version !== null) {
125 86
			$array['version'] = $this->version;
126
		}
127 87
		if ($this->extensions !== []) {
128 5
			$array['ext'] = [];
129 5
			foreach ($this->extensions as $extension) {
130 5
				$array['ext'][] = $extension->getOfficialLink();
131
			}
132
		}
133 87
		if ($this->profiles !== []) {
134 11
			$array['profile'] = [];
135 11
			foreach ($this->profiles as $profile) {
136 11
				$array['profile'][] = $profile->getOfficialLink();
137
			}
138
		}
139 87
		if ($this->meta !== null && $this->meta->isEmpty() === false) {
140 6
			$array['meta'] = $this->meta->toArray();
141
		}
142
		
143 87
		return $array;
144
	}
145
}
146