Relationship::toArray()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.5923

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 18
ccs 8
cts 12
cp 0.6667
rs 9.2
c 1
b 1
f 0
cc 4
eloc 9
nc 8
nop 0
crap 4.5923
1
<?php
2
3
/*
4
 * This file is part of JSON-API.
5
 *
6
 * (c) Toby Zerner <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tobscure\JsonApi;
13
14
class Relationship
15
{
16
    use LinksTrait;
17
    use MetaTrait;
18
19
    /**
20
     * The data object.
21
     *
22
     * @var \Tobscure\JsonApi\ElementInterface|null
23
     */
24
    protected $data;
25
26
    /**
27
     * Create a new relationship.
28
     *
29
     * @param \Tobscure\JsonApi\ElementInterface|null $data
30
     */
31 21
    public function __construct(ElementInterface $data = null)
32
    {
33 21
        $this->data = $data;
34 21
    }
35
36
    /**
37
     * Get the data object.
38
     *
39
     * @return \Tobscure\JsonApi\ElementInterface|null
40
     */
41 12
    public function getData()
42
    {
43 12
        return $this->data;
44
    }
45
46
    /**
47
     * Set the data object.
48
     *
49
     * @param \Tobscure\JsonApi\ElementInterface|null $data
50
     *
51
     * @return $this
52
     */
53
    public function setData($data)
54
    {
55
        $this->data = $data;
56
57
        return $this;
58
    }
59
60
    /**
61
     * Map everything to an array.
62
     *
63
     * @return array
64
     */
65 12
    public function toArray()
66
    {
67 12
        $array = [];
68
69 12
        if (! empty($this->data)) {
70 12
            $array['data'] = $this->data->toIdentifier();
71 12
        }
72
73 12
        if (! empty($this->meta)) {
74
            $array['meta'] = $this->meta;
75
        }
76
77 12
        if (! empty($this->links)) {
78
            $array['links'] = $this->links;
79
        }
80
81 12
        return $array;
82
    }
83
}
84