Passed
Push — master ( ae0681...d5d767 )
by Radu
01:44
created

AbstractResourceObject::getId()   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 0
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 getId()
20
    {
21
        return $this->id;
22
    }
23
24
    public function setType($type)
25
    {
26
        $this->type = $type;
27
    }
28
29
    public function setId($id)
30
    {
31
        $this->id = $id;
32
    }
33
34
    public function setAttribute($key, $value)
35
    {
36
        $this->attributes[$key] = $value;
37
    }
38
39
    public function setLink($key, $value)
40
    {
41
        $this->links[$key] = $value;
42
    }
43
44
    public function setMeta($key, $value)
45
    {
46
        $this->meta[$key] = $value;
47
    }
48
49
    public function toArray()
50
    {
51
        $array = [
52
            'type' => $this->type,
53
            'id' => $this->id,
54
        ];
55
        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...
56
            $array['attributes'] = $this->attributes;
57
        }
58
        if (!empty($this->links)) {
59
            $array['links'] = $this->links;
60
        }
61
        if (!empty($this->meta)) {
62
            $array['meta'] = $this->meta;
63
        }
64
        return $array;
65
    }
66
67
    public function toJson()
68
    {
69
        $array = $this->toArray();
70
        return json_encode($array);
71
    }
72
}
73