Passed
Pull Request — master (#4)
by Morten Poul
05:31 queued 02:55
created

ApiResource   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

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

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 7 2
A getAttributes() 0 3 1
A offsetSet() 0 3 1
A offsetExists() 0 3 1
A except() 0 3 2
A getAttribute() 0 3 1
A offsetUnset() 0 3 1
A only() 0 3 2
A setAttribute() 0 5 1
A __construct() 0 4 1
A offsetGet() 0 3 1
A toArray() 0 3 1
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
     * Determine if the given attribute exists.
63
     *
64
     * @param  mixed  $offset
65
     * @return bool
66
     */
67
    public function offsetExists($offset)
68
    {
69
        return array_key_exists($offset, $this->attributes);
70
    }
71
72
    /**
73
     * Get the value for a given offset.
74
     *
75
     * @param  mixed  $offset
76
     * @return mixed
77
     */
78
    public function offsetGet($offset)
79
    {
80
        return $this->getAttribute($offset);
81
    }
82
83
    /**
84
     * Set the value for a given offset.
85
     *
86
     * @param  mixed  $offset
87
     * @param  mixed  $value
88
     * @return void
89
     */
90
    public function offsetSet($offset, $value)
91
    {
92
        return $this->setAttribute($offset, $value);
93
    }
94
95
    /**
96
     * Unset the value for a given offset.
97
     *
98
     * @param  mixed  $offset
99
     * @return void
100
     */
101
    public function offsetUnset($offset)
102
    {
103
        unset($this->attributes[$offset]);
104
    }
105
106
    /**
107
     * Get an attribute.
108
     *
109
     * @param  string $key
110
     * @return mixed
111
     */
112
    protected function getAttribute($key)
113
    {
114
        return $this->attributes[$key];
115
    }
116
117
    /**
118
     * Set an attribute.
119
     *
120
     * @param string $key
121
     * @param mixed $value
122
     */
123
    protected function setAttribute($key, $value)
124
    {
125
        $this->attributes[$key] = $value;
126
127
        return $this;
128
    }
129
130
    /**
131
     * Get attributes for the resource.
132
     *
133
     * @return array
134
     */
135
    public function getAttributes()
136
    {
137
        return $this->attributes;
138
    }
139
140
    public function toArray()
141
    {
142
        return $this->getAttributes();
143
    }
144
}
145