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

DataDocument::toArray()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 2
nop 0
1
<?php
2
3
namespace alsvanzelf\jsonapi;
4
5
use alsvanzelf\jsonapi\Document;
6
use alsvanzelf\jsonapi\exceptions\DuplicateException;
7
use alsvanzelf\jsonapi\helpers\Validator;
8
use alsvanzelf\jsonapi\objects\ResourceObject;
9
10
/**
11
 * @see ResourceDocument or CollectionDocument
12
 */
0 ignored issues
show
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...
Coding Style introduced by
Missing @category tag in class comment
Loading history...
13
abstract class DataDocument extends Document {
14
	/** @var ResourceObject[] */
15
	protected $includedResources = [];
16
	/** @var Validator */
17
	protected $validator;
18
	
19
	public function __construct() {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
20
		parent::__construct();
21
		
22
		$this->validator = new Validator();
23
	}
24
	
25
	/**
26
	 * human api
27
	 */
28
	
29
	/**
30
	 * spec api
31
	 */
32
	
33
	/**
34
	 * mainly used when an `included` query parameter is passed
35
	 * and resources are requested separate from what is standard for a response
36
	 * 
37
	 * @param ResourceObject ...$resourceObjects
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
38
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
39
	public function addIncludedResourceObject(ResourceObject ...$resourceObjects) {
40
		foreach ($resourceObjects as $resourceObject) {
41
			try {
42
				$this->validator->claimUsedResourceIdentifier($resourceObject);
43
			}
44
			catch (DuplicateException $e) {
45
				// silently skip duplicates
46
				continue;
47
			}
48
			
49
			$this->includedResources[] = $resourceObject;
50
		}
51
	}
52
	
53
	/**
54
	 * internal api
55
	 */
56
	
57
	/**
58
	 * DocumentInterface
59
	 */
60
	
61
	/**
62
	 * @inheritDoc
63
	 */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
64
	public function toArray() {
65
		$array = parent::toArray();
66
		
67
		$array['data'] = null;
68
		
69
		if ($this->includedResources !== []) {
70
			$array['included'] = [];
71
			foreach ($this->includedResources as $resource) {
72
				$array['included'][] = $resource->toArray();
73
			}
74
		}
75
		
76
		return $array;
77
	}
78
}
79