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

JsonapiObject::addMeta()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 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\interfaces\ObjectInterface;
8
use alsvanzelf\jsonapi\objects\MetaObject;
9
10
class JsonapiObject implements ObjectInterface {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class JsonapiObject
Loading history...
11
	use AtMemberManager;
12
	
13
	/** @var string */
14
	protected $version;
15
	/** @var MetaObject */
16
	protected $meta;
17
	
18
	/**
19
	 * @param string $version one of the Document::JSONAPI_VERSION_* constants, optional, defaults to Document::JSONAPI_VERSION_LATEST
20
	 */
21
	public function __construct($version=Document::JSONAPI_VERSION_LATEST) {
22
		if ($version !== null) {
23
			$this->setVersion($version);
24
		}
25
	}
26
	
27
	/**
28
	 * human api
29
	 */
30
	
31
	/**
32
	 * @param string $key
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
33
	 * @param mixed  $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
34
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
35
	public function addMeta($key, $value) {
36
		if ($this->meta === null) {
37
			$this->setMetaObject(new MetaObject());
38
		}
39
		
40
		$this->meta->add($key, $value);
41
	}
42
	
43
	/**
44
	 * spec api
45
	 */
46
	
47
	/**
48
	 * @param string $version
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
49
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
50
	public function setVersion($version) {
51
		$this->version = $version;
52
	}
53
	
54
	/**
55
	 * @param MetaObject $metaObject
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
56
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
57
	public function setMetaObject(MetaObject $metaObject) {
58
		$this->meta = $metaObject;
59
	}
60
	
61
	/**
62
	 * ObjectInterface
63
	 */
64
	
65
	/**
66
	 * @inheritDoc
67
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
68 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...
69
		if ($this->version !== null) {
70
			return false;
71
		}
72
		if ($this->meta !== null && $this->meta->isEmpty() === false) {
73
			return false;
74
		}
75
		if ($this->hasAtMembers()) {
76
			return false;
77
		}
78
		
79
		return true;
80
	}
81
	
82
	/**
83
	 * @inheritDoc
84
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
85 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...
86
		$array = $this->getAtMembers();
87
		
88
		if ($this->version !== null) {
89
			$array['version'] = $this->version;
90
		}
91
		if ($this->meta !== null && $this->meta->isEmpty() === false) {
92
			$array['meta'] = $this->meta->toArray();
93
		}
94
		
95
		return $array;
96
	}
97
}
98