Option   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 131
rs 10
wmc 14

10 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 13 4
A defaults() 0 9 2
A __get() 0 3 1
A instance() 0 3 1
A builders() 0 5 1
A __construct() 0 3 1
A __set() 0 3 1
A loadOptions() 0 3 1
A all() 0 3 1
A get() 0 3 1
1
<?php
2
3
namespace ArinaSystems\JsonResponse;
4
5
use Illuminate\Support\Arr;
6
7
/**
8
 * @property array $attributes
9
 * @property array $status
10
 * @property array $transformers
11
 * @property array $codes
12
 * @property int $encoding_options
13
 * @property bool $debug
14
 * @property bool $message_translations
15
 */
16
class Option
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $options = [];
22
23
    /**
24
     * Create a new instance.
25
     *
26
     * @param  array  $options
27
     */
28
    public function __construct(array $options)
29
    {
30
        $this->loadOptions($options);
31
    }
32
33
    /**
34
     * Get a option from an array using "dot" notation.
35
     *
36
     * @param  string  $key
37
     * @param  mixed|null  $default
38
     * @return mixed
39
     */
40
    public function get(string $key, $default = null)
41
    {
42
        return Arr::get($this->options, $key, $default);
43
    }
44
45
    /**
46
     * Set a option to a given value using "dot" notation.
47
     *
48
     * @param  string|array  $keys
49
     * @param  null|mixed  $value
50
     * @return self
51
     */
52
    public function set($keys, $value = null)
53
    {
54
        if (is_array($keys)) {
55
            foreach ($keys as $key => $value) {
56
                $this->set($key, $value);
57
            }
58
        }
59
60
        if (is_string($keys)) {
61
            Arr::set($this->options, $keys, $value);
62
        }
63
64
        return $this;
65
    }
66
67
    /**
68
     * Retrieve all response options.
69
     *
70
     * @return array
71
     */
72
    public function all(): array
73
    {
74
        return (array) $this->options;
75
    }
76
77
    /**
78
     * Load the given options array to response options object.
79
     *
80
     * @param  array  $options
81
     * @return void
82
     */
83
    protected function loadOptions(array $options): void
84
    {
85
        $this->options = $options;
86
    }
87
88
    /**
89
     * Dynamically set option on the response options object.
90
     *
91
     * @param  string  $key
92
     * @param  mixed  $value
93
     * @return void
94
     */
95
    public function __set(string $key, $value)
96
    {
97
        $this->set($key, $value);
98
    }
99
100
    /**
101
     * Dynamically retrieve option on the response options object.
102
     *
103
     * @param  string  $key
104
     * @return mixed
105
     */
106
    public function __get($key)
107
    {
108
        return $this->get($key);
109
    }
110
111
    /**
112
     * Get the current instance of json response options.
113
     *
114
     * @return mixed
115
     */
116
    public function instance(): self
117
    {
118
        return $this;
119
    }
120
121
    /**
122
     * Retrieve all attributes builders.
123
     *
124
     * @return array
125
     */
126
    public function builders(): array
127
    {
128
        return array_map(function ($attribute) {
129
            return Arr::get($attribute, 'builder');
130
        }, /* @scrutinizer ignore-type */ $this->get('attributes'));
131
    }
132
133
    /**
134
     * Retrieve all attributes defaults.
135
     *
136
     * @return array
137
     */
138
    public function defaults(string $attribute = null): array
139
    {
140
        if (! is_null($attribute)) {
141
            return config("json-response.attributes.{$attribute}.value");
142
        }
143
144
        return array_map(function ($attribute) {
145
            return Arr::get($attribute, 'value');
146
        }, config('json-response.attributes'));
147
    }
148
}
149