Passed
Push — master ( ed897d...ae0681 )
by Radu
02:02
created

AbstractResourceObject::setLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace WebServCo\Api\JsonApi;
3
4
abstract class AbstractResourceObject implements \WebServCo\Api\JsonApi\Interfaces\ResourceObjectInterface
5
{
6
    protected $type;
7
    protected $id;
8
    protected $attributes;
9
    protected $links;
10
    protected $meta;
11
12
    public function __construct()
13
    {
14
        $this->attributes = [];
15
        $this->links = [];
16
        $this->meta = [];
17
    }
18
19
    public function setType($type)
20
    {
21
        $this->type = $type;
22
    }
23
24
    public function setId($id)
25
    {
26
        $this->id = $id;
27
    }
28
29
    public function setAttribute($key, $value)
30
    {
31
        $this->attributes[$key] = $value;
32
    }
33
34
    public function setLink($key, $value)
35
    {
36
        $this->links[$key] = $value;
37
    }
38
39
    public function setMeta($key, $value)
40
    {
41
        $this->meta[$key] = $value;
42
    }
43
44
    public function toArray()
45
    {
46
        $array = [
47
            'type' => $this->type,
48
            'id' => $this->id,
49
        ];
50
        if ($this->attributes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->attributes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
51
            $array['attributes'] = $this->attributes;
52
        }
53
        if (!empty($this->links)) {
54
            $array['links'] = $this->links;
55
        }
56
        if (!empty($this->meta)) {
57
            $array['meta'] = $this->meta;
58
        }
59
        return $array;
60
    }
61
62
    public function toJson()
63
    {
64
        $array = $this->toArray();
65
        return json_encode($array);
66
    }
67
}
68