Passed
Push — master ( ed2df0...55ced3 )
by Radu
03:44
created

AbstractResourceObject::toJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
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 $meta;
10
11
    public function __construct()
12
    {
13
        $this->attributes = [];
14
        $this->meta = [];
15
    }
16
17
    public function setType($type)
18
    {
19
        $this->type = $type;
20
    }
21
22
    public function setId($id)
23
    {
24
        $this->id = $id;
25
    }
26
27
    public function setAttribute($key, $value)
28
    {
29
        $this->attributes[$key] = $value;
30
    }
31
32
    public function setMeta($key, $value)
33
    {
34
        $this->meta[$key] = $value;
35
    }
36
37
    public function toArray()
38
    {
39
        $array = [
40
            'type' => $this->type,
41
            'id' => $this->id,
42
        ];
43
        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...
44
            $array['attributes'] = $this->attributes;
45
        }
46
        if ($this->meta) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->meta 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...
47
            $array['meta'] = $this->meta;
48
        }
49
        return $array;
50
    }
51
52
    public function toJson()
53
    {
54
        $array = $this->toArray();
55
        return json_encode($array);
56
    }
57
}
58