Test Failed
Pull Request — master (#6)
by Arina
06:20
created

Attribute::instance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace ArinaSystems\JsonResponse;
4
5
use Illuminate\Support\Arr;
6
use InvalidArgumentException;
7
8
class Attribute
9
{
10
    /**
11
     * @var \ArinaSystems\JsonResponse\Option
12
     */
13
    protected $options;
14
15
    /**
16
     * @var array
17
     */
18
    protected $attributes = [];
19
20
    /**
21
     * Create a new instance.
22
     *
23
     * @param \ArinaSystems\JsonResponse\Option $options
24
     */
25
    public function __construct(Option $options)
26
    {
27
        $this->parse($options);
28
    }
29
30
    /**
31
     * Get an attribute from an array using "dot" notation.
32
     *
33
     * @param  string  $key
34
     * @param  mixed   $default
35
     * @return mixed
36
     */
37
    public function get(string $key, $default = null)
38
    {
39
        return Arr::get($this->attributes, $key.'.value', $default);
40
    }
41
42
    /**
43
     * Set a attribute to a given value using "dot" notation.
44
     *
45
     * @param  string|array $keys
46
     * @param  null|mixed   $value
47
     * @return self
48
     */
49
    public function set($keys, $value = null)
50
    {
51
        if (is_array($keys)) {
52
            foreach ($keys as $key => $value) {
53
                $this->set($key, $value);
54
            }
55
56
            return $this;
57
        }
58
59
        if (! is_string($keys)) {
0 ignored issues
show
introduced by
The condition is_string($keys) is always true.
Loading history...
60
            throw new InvalidArgumentException('$key must be a string or array.');
61
        }
62
63
        $value = $this->build($keys, $value);
64
65
        Arr::set($this->attributes, $keys.'.value', $value);
66
67
        return $this;
68
    }
69
70
    /**
71
     * Retrieve all response attributes.
72
     *
73
     * @return array
74
     */
75
    public function all(string $value = 'value')
76
    {
77
        $attributes = $this->attributes;
78
79
        if (! is_null($value)) {
0 ignored issues
show
introduced by
The condition is_null($value) is always false.
Loading history...
80
            foreach ($attributes as $attribute => $options) {
81
                $attributes[$attribute] = Arr::get($options, $value);
82
            }
83
        }
84
85
        return $attributes;
86
    }
87
88
    /**
89
     * Build the value of attribute.
90
     *
91
     * @param  string     $attribute
92
     * @param  mixed      $value
93
     * @param  mixed|null $builder
94
     * @return mixed
95
     */
96
    public function build(string $attribute, $value, $builder = null)
97
    {
98
        if (is_null($builder)) {
99
            $builder = $this->builderOf($attribute);
100
        }
101
102
        if (is_string($builder) && class_exists($builder)) {
103
            $builder = new $builder($this->options, $this);
104
105
            return $builder->build($value);
106
        }
107
108
        return $value;
109
    }
110
111
    /**
112
     * Get the attribute's value builder.
113
     *
114
     * @param  string  $attribute
115
     * @return mixed
116
     */
117
    protected function builderOf(string $attribute)
118
    {
119
        return Arr::get($this->options->builders(), $attribute);
120
    }
121
122
    /**
123
     * Parsing the attributes from the given options.
124
     *
125
     * @param  \ArinaSystems\JsonResponse\Option $options
126
     * @return void
127
     */
128
    protected function parse(Option $options): void
129
    {
130
        $this->options = $options;
131
        $this->attributes = (array) $options->get('attributes');
132
        $this->set($this->all());
133
    }
134
135
    /**
136
     * Dynamically set attribute on the response attributes object.
137
     *
138
     * @param  string $key
139
     * @param  mixed  $value
140
     * @return void
141
     */
142
    public function __set(string $key, $value)
143
    {
144
        $this->set($key, $value);
145
    }
146
147
    /**
148
     * Dynamically retrieve attribute on the response attributes object.
149
     *
150
     * @param  string  $key
151
     * @return mixed
152
     */
153
    public function __get($key)
154
    {
155
        return $this->get($key);
156
    }
157
158
    /**
159
     * Get an instance of attribute object.
160
     *
161
     * @return self
162
     */
163
    public function instance()
164
    {
165
        return $this;
166
    }
167
}
168