Resource::hasRelationship()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
namespace Javis\JsonApi\Schema;
3
4
class Resource
5
{
6
    /**
7
     * @var string
8
     */
9
    protected $type;
10
11
    /**
12
     * @var string
13
     */
14
    protected $id;
15
16
    /**
17
     * @var array
18
     */
19
    protected $meta;
20
21
    /**
22
     * @var \Javis\JsonApi\Schema\Links
23
     */
24
    protected $links;
25
26
    /**
27
     * @var array
28
     */
29
    protected $attributes;
30
31
    /**
32
     * @var \Javis\JsonApi\Schema\Relationship[]
33
     */
34
    protected $relationships;
35
36
    /**
37
     * @param array $array
38
     * @param \Javis\JsonApi\Schema\Resources $resources
39
     */
40
    public function __construct($array, Resources $resources)
41
    {
42
        $this->type = empty($array["type"]) ? "" : $array["type"];
43
        $this->id = empty($array["id"]) ? "" : $array["id"];
44
        $this->meta = $this->isArrayKey($array, "meta") ? $array["meta"] : [];
45
        $this->links = Links::createFromArray($this->isArrayKey($array, "links") ? $array["links"] : []);
46
        $this->attributes = $this->isArrayKey($array, "attributes") ? $array["attributes"] : [];
47
48
        $this->relationships = [];
49
        if ($this->isArrayKey($array, "relationships")) {
50
            foreach ($array["relationships"] as $name => $relationship) {
51
                $this->relationships[$name] = new Relationship($name, $relationship, $resources);
52
            }
53
        }
54
    }
55
56
    /**
57
     * @return array
58
     */
59
    public function toArray()
60
    {
61
        $result = [
62
            "type" => $this->type,
63
            "id" => $this->id
64
        ];
65
66
        if (empty($this->meta) === false) {
67
            $result["meta"] = $this->meta;
68
        }
69
70
        if ($this->links) {
71
            $result["links"] = $this->links->toArray();
72
        }
73
74
        if (empty($this->attributes) === false) {
75
            $result["attributes"] = $this->attributes;
76
        }
77
78
        if (empty($this->relationships) === false) {
79
            $result["relationships"] = [];
80
            foreach ($this->relationships as $name => $relationship) {
81
                $result["relationships"][$name] = $relationship->toArray();
82
            }
83
        }
84
85
        return $result;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function type()
92
    {
93
        return $this->type;
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function id()
100
    {
101
        return $this->id;
102
    }
103
104
    /**
105
     * @return bool
106
     */
107
    public function hasMeta()
108
    {
109
        return empty($this->meta) === false;
110
    }
111
112
    /**
113
     * @return array
114
     */
115
    public function meta()
116
    {
117
        return $this->meta;
118
    }
119
120
    /**
121
     * @return bool
122
     */
123
    public function hasLinks()
124
    {
125
        return $this->links->hasLinks();
126
    }
127
128
    /**
129
     * @return \Javis\JsonApi\Schema\Links
130
     */
131
    public function links()
132
    {
133
        return $this->links;
134
    }
135
136
    /**
137
     * @return array
138
     */
139
    public function attributes()
140
    {
141
        return $this->attributes;
142
    }
143
144
    /**
145
     * @param string $name
146
     * @return bool
147
     */
148
    public function hasAttribute($name)
149
    {
150
        return array_key_exists($name, $this->attributes);
151
    }
152
153
    /**
154
     * @param string $name
155
     * @return mixed|null
156
     */
157
    public function attribute($name)
158
    {
159
        return $this->hasAttribute($name) ? $this->attributes[$name] : null;
160
    }
161
162
    /**
163
     * @return Relationship[]
164
     */
165
    public function relationships()
166
    {
167
        return $this->relationships;
168
    }
169
170
    /**
171
     * @param string $name
172
     * @return bool
173
     */
174
    public function hasRelationship($name)
175
    {
176
        return array_key_exists($name, $this->relationships);
177
    }
178
179
    /**
180
     * @param string $name
181
     * @return \Javis\JsonApi\Schema\Relationship|null
182
     */
183
    public function relationship($name)
184
    {
185
        return $this->hasRelationship($name) ? $this->relationships[$name] : null;
186
    }
187
188
    /**
189
     * @param array $array
190
     * @param string $key
191
     * @return bool
192
     */
193
    protected function isArrayKey($array, $key)
194
    {
195
        return isset($array[$key]) && is_array($array[$key]);
196
    }
197
}
198