Passed
Push — master ( 71a3af...64594b )
by Michael
02:24
created

IncludedResourcesContainer::addIncludedResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Document\Behaviour;
5
6
use Mikemirten\Component\JsonApi\Document\ResourceObject;
7
8
/**
9
 * Collection of included resources container behaviour
10
 *
11
 * @see http://jsonapi.org/format/#document-compound-documents
12
 *
13
 * @package Mikemirten\Component\JsonApi\Document\Behaviour
14
 */
15
trait IncludedResourcesContainer
16
{
17
    /**
18
     * Included resources
19
     *
20
     * @var ResourceObject[]
21
     */
22
    protected $includedResources = [];
23
24
    /**
25
     * Add included resource
26
     *
27
     * @param  ResourceObject $resource
28
     * @return mixed
29
     */
30 10
    public function addIncludedResource(ResourceObject $resource)
31
    {
32 10
        $this->includedResources[] = $resource;
33 10
    }
34
35
    /**
36
     * Get all included resources
37
     *
38
     * @return ResourceObject[]
39
     */
40 5
    public function getIncludedResources(): array
41
    {
42 5
        return $this->includedResources;
43
    }
44
45
    /**
46
     * Contains any included resources ?
47
     *
48
     * @return bool
49
     */
50 5
    public function hasIncludedResources(): bool
51
    {
52 5
        return count($this->includedResources) > 0;
53
    }
54
55
    /**
56
     * Cast included resources to an array
57
     *
58
     * @return array
59
     */
60 29
    protected function includedResourcesToArray(): array
61
    {
62 29
        $data = [];
63
64 29
        foreach ($this->includedResources as $resource)
65
        {
66 5
            $data[] = $resource->toArray();
67
        }
68
69 29
        return $data;
70
    }
71
}