Arguments   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 17
c 1
b 0
f 0
dl 0
loc 69
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A offsetGet() 0 7 2
A getArrayCopy() 0 11 3
A toArray() 0 3 1
A isEmpty() 0 3 1
1
<?php
2
3
namespace Bakery\Support;
4
5
use ArrayObject;
6
7
class Arguments extends ArrayObject
8
{
9
    /**
10
     * Arguments constructor.
11
     *
12
     * @param array $args
13
     */
14
    public function __construct(array $args)
15
    {
16
        $data = [];
17
18
        foreach ($args as $key => $value) {
19
            if (is_array($value)) {
20
                $value = new self($value);
21
            }
22
23
            $data[$key] = $value;
24
        }
25
26
        parent::__construct($data, ArrayObject::ARRAY_AS_PROPS);
27
    }
28
29
    /**
30
     * @param $offset
31
     * @return mixed|null
32
     */
33
    public function offsetGet($offset)
34
    {
35
        if (! $this->offsetExists($offset)) {
36
            return null;
37
        }
38
39
        return parent::offsetGet($offset);
40
    }
41
42
    /**
43
     * Get the instance as an array.
44
     *
45
     * @return array
46
     */
47
    public function toArray(): array
48
    {
49
        return $this->getArrayCopy();
50
    }
51
52
    /**
53
     * @return array
54
     */
55
    public function getArrayCopy(): array
56
    {
57
        $array = parent::getArrayCopy();
58
59
        foreach ($array as $key => $value) {
60
            if ($value instanceof self) {
61
                $array[$key] = $value->getArrayCopy();
62
            }
63
        }
64
65
        return $array;
66
    }
67
68
    /**
69
     * Return if the arguments are empty.
70
     *
71
     * @return bool
72
     */
73
    public function isEmpty()
74
    {
75
        return empty($this->toArray());
76
    }
77
}
78