Passed
Pull Request — main (#66)
by Lode
02:46
created

LinkObject::toArray()   F

Complexity

Conditions 12
Paths 384

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 12

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 12
eloc 22
c 3
b 0
f 1
nc 384
nop 0
dl 0
loc 37
ccs 22
cts 22
cp 1
crap 12
rs 3.8333

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace alsvanzelf\jsonapi\objects;
4
5
use alsvanzelf\jsonapi\helpers\AtMemberManager;
6
use alsvanzelf\jsonapi\helpers\ExtensionMemberManager;
7
use alsvanzelf\jsonapi\interfaces\ObjectInterface;
8
use alsvanzelf\jsonapi\objects\MetaObject;
9
10
class LinkObject implements ObjectInterface {
11
	use AtMemberManager, ExtensionMemberManager;
12
	
13
	/** @var string */
14
	protected $href;
15
	/** @var string */
16
	protected $rel;
17
	/** @var LinkObject */
18
	protected $describedby;
19
	/** @var string */
20
	protected $title;
21
	/** @var string */
22
	protected $type;
23
	/** @var string[] */
24
	protected $hreflang = [];
25
	/** @var MetaObject */
26
	protected $meta;
27
	
28
	/**
29
	 * @param string $href
30
	 * @param array  $meta optional
31
	 */
32 41
	public function __construct($href=null, array $meta=[]) {
33 41
		if ($href !== null) {
34 28
			$this->setHref($href);
35
		}
36 41
		if ($meta !== []) {
37 6
			$this->setMetaObject(MetaObject::fromArray($meta));
38
		}
39 41
	}
40
	
41
	/**
42
	 * human api
43
	 */
44
	
45
	/**
46
	 * @param string $href
47
	 */
48 1
	public function setDescribedBy($href) {
49 1
		$this->setDescribedByLinkObject(new LinkObject($href));
50 1
	}
51
	
52
	/**
53
	 * @param string $language
54
	 */
55 2
	public function addLanguage($language) {
56 2
		if ($this->hreflang === []) {
57 2
			$this->setHreflang($language);
58
		}
59
		else {
60 1
			$this->setHreflang(...array_merge($this->hreflang, [$language]));
61
		}
62 2
	}
63
	
64
	/**
65
	 * @param string $key
66
	 * @param mixed  $value
67
	 */
68 1
	public function addMeta($key, $value) {
69 1
		if ($this->meta === null) {
70 1
			$this->setMetaObject(new MetaObject());
71
		}
72
		
73 1
		$this->meta->add($key, $value);
74 1
	}
75
	
76
	/**
77
	 * spec api
78
	 */
79
	
80
	/**
81
	 * @param string $href
82
	 */
83 28
	public function setHref($href) {
84 28
		$this->href = $href;
85 28
	}
86
	
87
	/**
88
	 * @todo validate according to https://tools.ietf.org/html/rfc8288#section-2.1
89
	 * 
90
	 * @param string $relationType
91
	 */
92 1
	public function setRelationType($relationType) {
93 1
		$this->rel = $relationType;
94 1
	}
95
	
96
	/**
97
	 * @param LinkObject $describedBy
98
	 */
99 2
	public function setDescribedByLinkObject(LinkObject $describedBy) {
100 2
		$this->describedby = $describedBy;
101 2
	}
102
	
103
	/**
104
	 * @param string $friendlyTitle
105
	 */
106 1
	public function setHumanTitle($humanTitle) {
107 1
		$this->title = $humanTitle;
108 1
	}
109
	
110
	/**
111
	 * @param string $mediaType
112
	 */
113 4
	public function setMediaType($mediaType) {
114 4
		$this->type = $mediaType;
115 4
	}
116
	
117
	/**
118
	 * @todo validate according to https://tools.ietf.org/html/rfc5646
119
	 * 
120
	 * @param string ...$hreflang
121
	 */
122 3
	public function setHreflang(...$hreflang) {
123 3
		$this->hreflang = $hreflang;
124 3
	}
125
	
126
	/**
127
	 * @param MetaObject $metaObject
128
	 */
129 10
	public function setMetaObject(MetaObject $metaObject) {
130 10
		$this->meta = $metaObject;
131 10
	}
132
	
133
	/**
134
	 * ObjectInterface
135
	 */
136
	
137
	/**
138
	 * @inheritDoc
139
	 */
140 36
	public function isEmpty() {
141 36
		if ($this->href !== null) {
142 25
			return false;
143
		}
144 16
		if ($this->rel !== null) {
145 1
			return false;
146
		}
147 16
		if ($this->title !== null) {
148 1
			return false;
149
		}
150 16
		if ($this->type !== null) {
151 1
			return false;
152
		}
153 16
		if ($this->hreflang !== []) {
154 2
			return false;
155
		}
156 16
		if ($this->describedby !== null && $this->describedby->isEmpty() === false) {
157 2
			return false;
158
		}
159 16
		if ($this->meta !== null && $this->meta->isEmpty() === false) {
160 1
			return false;
161
		}
162 16
		if ($this->hasAtMembers()) {
163 1
			return false;
164
		}
165 16
		if ($this->hasExtensionMembers()) {
166 1
			return false;
167
		}
168
		
169 16
		return true;
170
	}
171
	
172
	/**
173
	 * @inheritDoc
174
	 */
175 32
	public function toArray() {
176 32
		$array = [];
177
		
178 32
		if ($this->hasAtMembers()) {
179 2
			$array = array_merge($array, $this->getAtMembers());
180
		}
181 32
		if ($this->hasExtensionMembers()) {
182 1
			$array = array_merge($array, $this->getExtensionMembers());
183
		}
184
		
185 32
		$array['href'] = $this->href;
186
		
187 32
		if ($this->rel !== null) {
188 1
			$array['rel'] = $this->rel;
189
		}
190 32
		if ($this->title !== null) {
191 1
			$array['title'] = $this->title;
192
		}
193 32
		if ($this->type !== null) {
194 4
			$array['type'] = $this->type;
195
		}
196 32
		if ($this->hreflang !== []) {
197 3
			if (count($this->hreflang) === 1) {
198 2
				$array['hreflang'] = $this->hreflang[0];
199
			}
200
			else {
201 2
				$array['hreflang'] = $this->hreflang;
202
			}
203
		}
204 32
		if ($this->describedby !== null && $this->describedby->isEmpty() === false) {
205 2
			$array['describedby'] = $this->describedby->toArray();
206
		}
207 32
		if ($this->meta !== null && $this->meta->isEmpty() === false) {
208 10
			$array['meta'] = $this->meta->toArray();
209
		}
210
		
211 32
		return $array;
212
	}
213
}
214