Test Failed
Pull Request — main (#64)
by Lode
08:15
created

LinksObject::toArray()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.4444
c 0
b 0
f 0
cc 8
nc 5
nop 0
1
<?php
2
3
namespace alsvanzelf\jsonapi\objects;
4
5
use alsvanzelf\jsonapi\exceptions\DuplicateException;
6
use alsvanzelf\jsonapi\helpers\AtMemberManager;
7
use alsvanzelf\jsonapi\helpers\Converter;
8
use alsvanzelf\jsonapi\helpers\Validator;
9
use alsvanzelf\jsonapi\interfaces\ObjectInterface;
10
use alsvanzelf\jsonapi\objects\LinkObject;
11
use alsvanzelf\jsonapi\objects\LinksArray;
12
13
class LinksObject implements ObjectInterface {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class LinksObject
Loading history...
14
	use AtMemberManager;
15
	
16
	/** @var array with string|LinkObject */
17
	protected $links = [];
18
	
19
	/**
20
	 * human api
21
	 */
22
	
23
	/**
24
	 * @param  array  $links key-value with values being href strings
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
25
	 * @return LinksObject
26
	 */
27
	public static function fromArray(array $links) {
28
		$linksObject = new self();
29
		
30
		foreach ($links as $key => $href) {
31
			$linksObject->add($key, $href);
32
		}
33
		
34
		return $linksObject;
35
	}
36
	
37
	/**
38
	 * @param  object $links
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
39
	 * @return LinksObject
40
	 */
41
	public static function fromObject($links) {
42
		$array = Converter::objectToArray($links);
43
		
44
		return self::fromArray($array);
45
	}
46
	
47
	/**
48
	 * @param string $key
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
49
	 * @param string $href
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
50
	 * @param array  $meta optional, if given a LinkObject is added, otherwise a link string is added
51
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
52
	public function add($key, $href, array $meta=[]) {
53
		if ($meta === []) {
54
			$this->addLinkString($key, $href);
55
		}
56
		else {
57
			$this->addLinkObject($key, new LinkObject($href, $meta));
58
		}
59
	}
60
	
61
	/**
62
	 * appends a link to an array of links under a specific key
63
	 * 
64
	 * @see LinksArray for use cases
65
	 * 
66
	 * @param string $key
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
67
	 * @param string $href
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
68
	 * @param array  $meta optional, if given a LinkObject is added, otherwise a link string is added
69
	 * 
70
	 * @throws DuplicateException if another link is already using that $key but is not an array
71
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
72 View Code Duplication
	public function append($key, $href, array $meta=[]) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
		Validator::checkMemberName($key);
74
		
75
		if (isset($this->links[$key]) === false) {
76
			$this->addLinksArray($key, new LinksArray());
77
		}
78
		elseif ($this->links[$key] instanceof LinksArray === false) {
79
			throw new DuplicateException('can not add to key "'.$key.'", it is not an array of links');
80
		}
81
		
82
		$this->links[$key]->add($href, $meta);
83
	}
84
	
85
	/**
86
	 * spec api
87
	 */
88
	
89
	/**
90
	 * @param string $key
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
91
	 * @param string $href
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
92
	 * 
93
	 * @throws DuplicateException if another link is already using that $key
94
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
95 View Code Duplication
	public function addLinkString($key, $href) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
		Validator::checkMemberName($key);
97
		
98
		if (isset($this->links[$key])) {
99
			throw new DuplicateException('link with key "'.$key.'" already set');
100
		}
101
		
102
		$this->links[$key] = $href;
103
	}
104
	
105
	/**
106
	 * @param string     $key
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
107
	 * @param LinkObject $linkObject
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
108
	 * 
109
	 * @throws DuplicateException if another link is already using that $key
110
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
111 View Code Duplication
	public function addLinkObject($key, LinkObject $linkObject) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
		Validator::checkMemberName($key);
113
		
114
		if (isset($this->links[$key])) {
115
			throw new DuplicateException('link with key "'.$key.'" already set');
116
		}
117
		
118
		$this->links[$key] = $linkObject;
119
	}
120
	
121
	/**
122
	 * @param string     $key
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
123
	 * @param LinksArray $linksArray
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
124
	 * 
125
	 * @throws DuplicateException if another link is already using that $key
126
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
127 View Code Duplication
	public function addLinksArray($key, LinksArray $linksArray) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
		Validator::checkMemberName($key);
129
		
130
		if (isset($this->links[$key])) {
131
			throw new DuplicateException('link with key "'.$key.'" already set');
132
		}
133
		
134
		$this->links[$key] = $linksArray;
135
	}
136
	
137
	/**
138
	 * @param  string     $key
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
139
	 * @param  LinkObject $linkObject
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
140
	 * 
141
	 * @throws DuplicateException if another link is already using that $key but is not an array
142
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
143 View Code Duplication
	public function appendLinkObject($key, LinkObject $linkObject) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
		Validator::checkMemberName($key);
145
		
146
		if (isset($this->links[$key]) === false) {
147
			$this->addLinksArray($key, new LinksArray());
148
		}
149
		elseif ($this->links[$key] instanceof LinksArray === false) {
150
			throw new DuplicateException('can not add to key "'.$key.'", it is not an array of links');
151
		}
152
		
153
		$this->links[$key]->addLinkObject($linkObject);
154
	}
155
	
156
	/**
157
	 * ObjectInterface
158
	 */
159
	
160
	/**
161
	 * @inheritDoc
162
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
163
	public function isEmpty() {
164
		return ($this->links === [] && $this->hasAtMembers() === false);
165
	}
166
	
167
	/**
168
	 * @inheritDoc
169
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
170
	public function toArray() {
171
		$array = $this->getAtMembers();
172
		
173
		foreach ($this->links as $key => $link) {
174
			if ($link instanceof LinkObject && $link->isEmpty() === false) {
175
				$array[$key] = $link->toArray();
176
			}
177
			elseif ($link instanceof LinksArray && $link->isEmpty() === false) {
178
				$array[$key] = $link->toArray();
179
			}
180
			elseif ($link instanceof LinkObject && $link->isEmpty()) {
181
				$array[$key] = null;
182
			}
183
			else { // string or null
184
				$array[$key] = $link;
185
			}
186
		}
187
		
188
		return $array;
189
	}
190
}
191