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

CollectionDocument   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 6
dl 0
loc 124
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A fromResources() 0 9 2
A add() 0 8 2
A setPaginationLinks() 0 14 5
A addResource() 0 15 4
A toArray() 0 10 2
A getContainedResources() 0 3 1
1
<?php
2
3
namespace alsvanzelf\jsonapi;
4
5
use alsvanzelf\jsonapi\DataDocument;
6
use alsvanzelf\jsonapi\Document;
7
use alsvanzelf\jsonapi\exceptions\InputException;
8
use alsvanzelf\jsonapi\interfaces\PaginableInterface;
9
use alsvanzelf\jsonapi\interfaces\RecursiveResourceContainerInterface;
10
use alsvanzelf\jsonapi\interfaces\ResourceContainerInterface;
11
use alsvanzelf\jsonapi\interfaces\ResourceInterface;
12
use alsvanzelf\jsonapi\objects\ResourceObject;
13
use alsvanzelf\jsonapi\objects\ResourceIdentifierObject;
14
15
/**
16
 * this document is a set of Resources
17
 * this document should be used if there could be multiple, also if only one or even none is returned
18
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
19
class CollectionDocument extends DataDocument implements PaginableInterface, ResourceContainerInterface {
20
	/** @var ResourceInterface[] */
21
	protected $resources = [];
22
	/** @var array */
23
	protected static $defaults = [
24
		/**
25
		 * add resources inside relationships to /included when adding resources to the collection
26
		 */
27
		'includeContainedResources' => true,
28
	];
29
	
30
	/**
31
	 * human api
32
	 */
33
	
34
	/**
35
	 * generate a CollectionDocument from one or multiple resources
36
	 * 
37
	 * adds included resources if found inside the resource's relationships, use {@see ->addResource()} to change that behavior
38
	 * 
39
	 * @param  ResourceInterface ...$resources
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
40
	 * @return CollectionDocument
41
	 */
42
	public static function fromResources(ResourceInterface ...$resources) {
43
		$collectionDocument = new self();
44
		
45
		foreach ($resources as $resource) {
46
			$collectionDocument->addResource($resource);
47
		}
48
		
49
		return $collectionDocument;
50
	}
51
	
52
	/**
53
	 * @param string     $type
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
54
	 * @param string|int $id
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
55
	 * @param array      $attributes optional, if given a ResourceObject is added, otherwise a ResourceIdentifierObject is added
56
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
57
	public function add($type, $id, array $attributes=[]) {
58
		if ($attributes === []) {
59
			$this->addResource(new ResourceIdentifierObject($type, $id));
60
		}
61
		else {
62
			$this->addResource(ResourceObject::fromArray($attributes, $type, $id));
63
		}
64
	}
65
	
66
	/**
0 ignored issues
show
Coding Style introduced by
Parameter $firstHref should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $previousHref should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $lastHref should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $nextHref should have a doc-comment as per coding-style.
Loading history...
67
	 * @inheritDoc
68
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
69
	public function setPaginationLinks($previousHref=null, $nextHref=null, $firstHref=null, $lastHref=null) {
70
		if ($previousHref !== null) {
71
			$this->addLink('prev', $previousHref);
72
		}
73
		if ($nextHref !== null) {
74
			$this->addLink('next', $nextHref);
75
		}
76
		if ($firstHref !== null) {
77
			$this->addLink('first', $firstHref);
78
		}
79
		if ($lastHref !== null) {
80
			$this->addLink('last', $lastHref);
81
		}
82
	}
83
	
84
	/**
85
	 * spec api
86
	 */
87
	
88
	/**
89
	 * add a resource to the collection
90
	 * 
91
	 * adds included resources if found inside the resource's relationships, unless $options['includeContainedResources'] is set to false
92
	 * 
93
	 * @param ResourceInterface $resource
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
94
	 * @param array             $options  optional {@see CollectionDocument::$defaults}
95
	 * 
96
	 * @throws InputException if the resource is empty
97
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
98
	public function addResource(ResourceInterface $resource, array $options=[]) {
99
		if ($resource->getResource()->isEmpty()) {
100
			throw new InputException('does not make sense to add empty resources to a collection');
101
		}
102
		
103
		$options = array_merge(self::$defaults, $options);
104
		
105
		$this->validator->claimUsedResourceIdentifier($resource);
106
		
107
		$this->resources[] = $resource;
108
		
109
		if ($options['includeContainedResources'] && $resource instanceof RecursiveResourceContainerInterface) {
110
			$this->addIncludedResourceObject(...$resource->getNestedContainedResourceObjects());
111
		}
112
	}
113
	
114
	/**
115
	 * DocumentInterface
116
	 */
117
	
118
	/**
119
	 * @inheritDoc
120
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
121
	public function toArray() {
122
		$array = parent::toArray();
123
		
124
		$array['data'] = [];
125
		foreach ($this->resources as $resource) {
126
			$array['data'][] = $resource->getResource()->toArray();
127
		}
128
		
129
		return $array;
130
	}
131
	
132
	/**
133
	 * ResourceContainerInterface
134
	 */
135
	
136
	/**
137
	 * @inheritDoc
138
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
139
	public function getContainedResources() {
140
		return $this->resources;
141
	}
142
}
143