HasAttributesTrait::jsonSerialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Sco\Attributes;
4
5
use Illuminate\Support\Arr;
6
7
trait HasAttributesTrait
8
{
9
    protected $attributes = [];
10
11
    /**
12
     * Set a given log value.
13
     *
14
     * @param array|string $key
15
     * @param mixed        $value
16
     *
17
     * @return $this
18
     */
19
    public function setAttribute($key, $value = null)
20
    {
21
        $keys = is_array($key) ? $key : [$key => $value];
22
23
        foreach ($keys as $key => $value) {
24
            Arr::set($this->attributes, $key, $value);
25
        }
26
        return $this;
27
    }
28
29
    /**
30
     * Get the specified log value.
31
     *
32
     * @param null|string $key
33
     * @param mixed       $default
34
     *
35
     * @return mixed
36
     */
37
    public function getAttribute($key = null, $default = null)
38
    {
39
        return Arr::get($this->attributes, $key, $default);
40
    }
41
42
    /**
43
     * Get all of the current attributes on the class.
44
     *
45
     * @return array
46
     */
47
    public function getAttributes()
48
    {
49
        return $this->attributes;
50
    }
51
52
    /**
53
     * Determine if the given key exists in the attributes.
54
     *
55
     * @param  string|int $key
56
     *
57
     * @return bool
58
     */
59
    public function existsAttribute($key)
60
    {
61
        return Arr::exists($this->attributes, $key);
62
    }
63
64
    /**
65
     * Check if an key or keys exist in the attributes using "dot" notation.
66
     *
67
     * @param  string|array $keys
68
     *
69
     * @return bool
70
     */
71
    public function hasAttribute($keys)
72
    {
73
        return Arr::has($this->attributes, $keys);
74
    }
75
76
    /**
77
     * Convert the Object instance to an array.
78
     *
79
     * @return array
80
     */
81
    public function toArray()
82
    {
83
        return array_map(function ($value) {
84
            return $value instanceof Arrayable ? $value->toArray() : $value;
0 ignored issues
show
Bug introduced by
The class Sco\Attributes\Arrayable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
85
        }, $this->getAttributes());
86
    }
87
88
    /**
89
     * Convert the object into something JSON serializable.
90
     *
91
     * @return array
92
     */
93
    public function jsonSerialize()
94
    {
95
        return $this->toArray();
96
    }
97
98
    /**
99
     * Convert the Object instance to JSON.
100
     *
101
     * @param  int  $options
102
     * @return string
103
     */
104
    public function toJson($options = 0)
105
    {
106
        return json_encode($this->jsonSerialize(), $options);
107
    }
108
}
109