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

ResourceIdentifierObject::setMetaObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace alsvanzelf\jsonapi\objects;
4
5
use alsvanzelf\jsonapi\exceptions\Exception;
6
use alsvanzelf\jsonapi\helpers\AtMemberManager;
7
use alsvanzelf\jsonapi\helpers\Validator;
8
use alsvanzelf\jsonapi\interfaces\ObjectInterface;
9
use alsvanzelf\jsonapi\interfaces\ResourceInterface;
10
use alsvanzelf\jsonapi\objects\MetaObject;
11
12
class ResourceIdentifierObject implements ObjectInterface, ResourceInterface {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ResourceIdentifierObject
Loading history...
13
	use AtMemberManager;
14
	
15
	/** @var string */
16
	protected $type;
17
	/** @var string */
18
	protected $id;
19
	/** @var MetaObject */
20
	protected $meta;
21
	/** @var Validator */
22
	protected $validator;
23
	
24
	/**
25
	 * @note $type and $id are optional to pass during construction
26
	 *       however they are required for a valid ResourceIdentifierObject
27
	 *       so use ->setType() and ->setId() if not passing them during construction
28
	 * 
29
	 * @param string     $type optional
30
	 * @param string|int $id   optional
31
	 */
32
	public function __construct($type=null, $id=null) {
33
		$this->validator = new Validator();
34
		
35
		if ($type !== null) {
36
			$this->setType($type);
37
		}
38
		if ($id !== null) {
39
			$this->setId($id);
40
		}
41
		
42
		// always mark as used, as these keys are reserved
43
		$this->validator->claimUsedFields($fieldNames=['type'], Validator::OBJECT_CONTAINER_TYPE);
44
		$this->validator->claimUsedFields($fieldNames=['id'], Validator::OBJECT_CONTAINER_ID);
45
	}
46
	
47
	/**
48
	 * human api
49
	 */
50
	
51
	/**
52
	 * @param string $key
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
53
	 * @param mixed  $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
54
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
55
	public function addMeta($key, $value) {
56
		if ($this->meta === null) {
57
			$this->setMetaObject(new MetaObject());
58
		}
59
		
60
		$this->meta->add($key, $value);
61
	}
62
	
63
	/**
64
	 * spec api
65
	 */
66
	
67
	/**
68
	 * @param string $type
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
69
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
70
	public function setType($type) {
71
		$this->type = $type;
72
	}
73
	
74
	/**
75
	 * @param string|int $id will be casted to a string
76
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
77
	public function setId($id) {
78
		$this->id = (string) $id;
79
	}
80
	
81
	/**
82
	 * @param MetaObject $metaObject
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
83
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
84
	public function setMetaObject(MetaObject $metaObject) {
85
		$this->meta = $metaObject;
86
	}
87
	
88
	/**
89
	 * internal api
90
	 */
91
	
92
	/**
93
	 * @internal
94
	 * 
95
	 * @param  ResourceObject $resourceObject
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
96
	 * @return ResourceIdentifierObject
97
	 */
98
	public static function fromResourceObject(ResourceObject $resourceObject) {
99
		$resourceIdentifierObject = new self($resourceObject->type, $resourceObject->id);
0 ignored issues
show
Bug introduced by
The property id cannot be accessed from this context as it is declared protected in class alsvanzelf\jsonapi\objec...esourceIdentifierObject.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
Bug introduced by
The property type cannot be accessed from this context as it is declared protected in class alsvanzelf\jsonapi\objec...esourceIdentifierObject.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
100
		
101
		if ($resourceObject->meta !== null) {
0 ignored issues
show
Bug introduced by
The property meta cannot be accessed from this context as it is declared protected in class alsvanzelf\jsonapi\objec...esourceIdentifierObject.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
102
			$resourceIdentifierObject->setMetaObject($resourceObject->meta);
0 ignored issues
show
Bug introduced by
The property meta cannot be accessed from this context as it is declared protected in class alsvanzelf\jsonapi\objec...esourceIdentifierObject.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
103
		}
104
		
105
		return $resourceIdentifierObject;
106
	}
107
	
108
	/**
109
	 * @internal
110
	 * 
111
	 * @param  ResourceInterface $resource
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
112
	 * @return boolean
113
	 * 
114
	 * @throws Exception if one or both are missing identification
115
	 */
116
	public function equals(ResourceInterface $resource) {
117
		if ($this->hasIdentification() === false || $resource->getResource()->hasIdentification() === false) {
118
			throw new Exception('can not compare resources if identification is missing');
119
		}
120
		
121
		return ($this->getIdentificationKey() === $resource->getResource()->getIdentificationKey());
122
	}
123
	
124
	/**
125
	 * @internal
126
	 * 
127
	 * @return boolean
128
	 */
129
	public function hasIdentification() {
130
		return ($this->type !== null && $this->id !== null);
131
	}
132
	
133
	/**
134
	 * get a key to uniquely define this resource
135
	 * 
136
	 * @internal
137
	 * 
138
	 * @return string
139
	 * 
140
	 * @throws Exception if type or id is not set yet
141
	 */
142
	public function getIdentificationKey() {
143
		if ($this->hasIdentification() === false) {
144
			throw new Exception('resource has no identification yet');
145
		}
146
		
147
		return $this->type.'|'.$this->id;
148
	}
149
	
150
	/**
151
	 * ObjectInterface
152
	 */
153
	
154
	/**
155
	 * @inheritDoc
156
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
157 View Code Duplication
	public function isEmpty() {
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...
158
		if ($this->type !== null || $this->id !== null) {
159
			return false;
160
		}
161
		if ($this->meta !== null && $this->meta->isEmpty() === false) {
162
			return false;
163
		}
164
		if ($this->hasAtMembers()) {
165
			return false;
166
		}
167
		
168
		return true;
169
	}
170
	
171
	/**
172
	 * @inheritDoc
173
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
174 View Code Duplication
	public function toArray() {
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...
175
		$array = $this->getAtMembers();
176
		
177
		$array['type'] = $this->type;
178
		
179
		if ($this->id !== null) {
180
			$array['id'] = $this->id;
181
		}
182
		
183
		if ($this->meta !== null && $this->meta->isEmpty() === false) {
184
			$array['meta'] = $this->meta->toArray();
185
		}
186
		
187
		return $array;
188
	}
189
	
190
	/**
191
	 * ResourceInterface
192
	 */
193
	
194
	/**
0 ignored issues
show
Coding Style introduced by
Parameter $identifierOnly should have a doc-comment as per coding-style.
Loading history...
195
	 * @inheritDoc
196
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
197
	public function getResource($identifierOnly=false) {
198
		return $this;
199
	}
200
}
201