ApiResource::offsetUnset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Signifly\Shopify\REST\Resources;
4
5
use ArrayAccess;
6
use Exception;
7
use Illuminate\Contracts\Support\Arrayable;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Traits\Macroable;
10
use Signifly\Shopify\Shopify;
11
12
class ApiResource implements ArrayAccess, Arrayable
13
{
14
    use Macroable;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Support\Traits\Macroable requires the property $name which is not provided by Signifly\Shopify\REST\Resources\ApiResource.
Loading history...
15
16
    protected array $attributes = [];
17
18
    protected Shopify $shopify;
19
20
    public function __construct(array $attributes, Shopify $shopify)
21
    {
22
        $this->attributes = $attributes;
23
        $this->shopify = $shopify;
24
    }
25
26
    /**
27
     * Get all of the attributes except for a specified array of keys.
28
     *
29
     * @param  array|string $keys
30
     * @return array
31
     */
32
    public function except($keys): array
33
    {
34
        return Arr::except($this->getAttributes(), is_array($keys) ? $keys : func_get_args());
35
    }
36
37
    /**
38
     * Get a subset of the attributes.
39
     *
40
     * @param  array|string $keys
41
     * @return array
42
     */
43
    public function only($keys): array
44
    {
45
        return Arr::only($this->getAttributes(), is_array($keys) ? $keys : func_get_args());
46
    }
47
48
    /**
49
     * @param  string $key
50
     * @return mixed
51
     */
52
    public function __get($key)
53
    {
54
        if (array_key_exists($key, $this->attributes)) {
55
            return $this->getAttribute($key);
56
        }
57
58
        throw new Exception('Property '.$key.' does not exist on '.get_called_class());
59
    }
60
61
    /**
62
     * @param  string $key
63
     * @return bool
64
     */
65
    public function __isset($key): bool
66
    {
67
        return array_key_exists($key, $this->attributes);
68
    }
69
70
    /**
71
     * Determine if the given attribute exists.
72
     *
73
     * @param  mixed  $offset
74
     * @return bool
75
     */
76
    #[\ReturnTypeWillChange]
77
    public function offsetExists($offset)
78
    {
79
        return array_key_exists($offset, $this->attributes);
80
    }
81
82
    /**
83
     * Get the value for a given offset.
84
     *
85
     * @param  mixed  $offset
86
     * @return mixed
87
     */
88
    #[\ReturnTypeWillChange]
89
    public function offsetGet($offset)
90
    {
91
        return $this->getAttribute($offset);
92
    }
93
94
    /**
95
     * Set the value for a given offset.
96
     *
97
     * @param  mixed  $offset
98
     * @param  mixed  $value
99
     * @return void
100
     */
101
    #[\ReturnTypeWillChange]
102
    public function offsetSet($offset, $value)
103
    {
104
        return $this->setAttribute($offset, $value);
105
    }
106
107
    /**
108
     * Unset the value for a given offset.
109
     *
110
     * @param  mixed  $offset
111
     * @return void
112
     */
113
    #[\ReturnTypeWillChange]
114
    public function offsetUnset($offset)
115
    {
116
        unset($this->attributes[$offset]);
117
    }
118
119
    /**
120
     * Get an attribute.
121
     *
122
     * @param  string $key
123
     * @return mixed
124
     */
125
    protected function getAttribute($key)
126
    {
127
        return $this->attributes[$key];
128
    }
129
130
    /**
131
     * Set an attribute.
132
     *
133
     * @param string $key
134
     * @param mixed $value
135
     */
136
    protected function setAttribute($key, $value)
137
    {
138
        $this->attributes[$key] = $value;
139
140
        return $this;
141
    }
142
143
    /**
144
     * Get attributes for the resource.
145
     *
146
     * @return array
147
     */
148
    public function getAttributes()
149
    {
150
        return $this->attributes;
151
    }
152
153
    public function toArray()
154
    {
155
        return $this->getAttributes();
156
    }
157
}
158