ArrayAttribute::offsetGet()   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 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
nc 1
cc 1
eloc 2
nop 1
1
<?php
2
3
namespace Tacone\Bees\Attribute;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
8
use Illuminate\Database\Query\Builder as QueryBuilder;
9
use Traversable;
10
11
class ArrayAttribute extends AbstractAttribute implements \Countable, \IteratorAggregate, \ArrayAccess
12
{
13
    protected $default = [];
14
15
    /**
16
     * Required by DelegatedArrayTrait, must return the
17
     * storage array.
18
     */
19
    public function getDelegatedStorage()
20
    {
21
        return !empty($this->storage[$this->path])
22
            ? $this->storage[$this->path]
23
            : [];
24
    }
25
26
    protected function get()
27
    {
28
        return $this->getDelegatedStorage();
29
    }
30
31
    protected function set($arguments)
32
    {
33
        $array = $arguments[0];
34
        $this->exchangeArray($array);
35
    }
36
37
    // --- cast
38
39
    protected function castToArray($array)
40
    {
41
        switch (true) {
42
            case is_array($array):
43
                return $array;
44
            case $array instanceof Arrayable:
45
            case $array instanceof Model:
0 ignored issues
show
Bug introduced by
The class Illuminate\Database\Eloquent\Model 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...
46
                return $array->toArray();
47
48
            case $array instanceof EloquentBuilder:
0 ignored issues
show
Bug introduced by
The class Illuminate\Database\Eloquent\Builder 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...
49
            case $array instanceof QueryBuilder:
0 ignored issues
show
Bug introduced by
The class Illuminate\Database\Query\Builder 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...
50
                return $array->get()->toArray();
51
52
            case $array instanceof \ArrayIterator:
53
            case $array instanceof \ArrayObject:
54
                return $array->getArrayCopy();
55
56
            case is_null($array):
57
                return [];
58
        }
59
60
        throw new \RuntimeException(sprintf(
61
            'ArrayAttribute does not supports type: %s%s',
62
            gettype($array),
63
            is_object($array) ? ' - '.get_class($array) : ''
64
        ));
65
    }
66
67
    // --- Array methods
68
69
    public function count()
70
    {
71
        return $this->getDelegatedStorage()->count();
72
    }
73
74
    public function offsetExists($name)
75
    {
76
        return isset($this->getDelegatedStorage()[$name]);
77
    }
78
79
    public function offsetSet($name, $value)
80
    {
81
        $array = $this->getDelegatedStorage();
82
        $array[$name] = $value;
83
        $this->exchangeArray($array);
84
    }
85
86
    public function offsetUnset($name)
87
    {
88
        return $this->getDelegatedStorage()->offsetUnset($name);
89
    }
90
91
    public function offsetGet($name)
92
    {
93
        return $this->getDelegatedStorage()[$name];
94
    }
95
96
    /**
97
     * (PHP 5 &gt;= 5.0.0)<br/>
98
     * Retrieve an external iterator.
99
     *
100
     * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
101
     *
102
     * @return Traversable An instance of an object implementing <b>Iterator</b> or
103
     *                     <b>Traversable</b>
104
     */
105
    public function getIterator()
106
    {
107
        return $this->getDelegatedStorage();
108
    }
109
110
    public function toArray()
111
    {
112
        return $this->getDelegatedStorage();
113
    }
114
115
    public function exchangeArray($array)
116
    {
117
        $array = $this->castToArray($array);
118
        $this->storage[$this->path] = $array;
119
    }
120
}
121