Completed
Branch TASK/11208/update-bundled-gate... (fce252)
by
unknown
121:38 queued 109:55
created

RegistryContainer::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\container;
4
5
use ArrayAccess;
6
use ArrayIterator;
7
use OutOfBoundsException;
8
9
defined('EVENT_ESPRESSO_VERSION') || exit;
10
11
12
13
/**
14
 * Class RegistryContainer
15
 * Object for storing data that behaves as an array
16
 *
17
 * @package EventEspresso\core\services\container
18
 * @author  Brent Christensen
19
 * @since   4.9.49
20
 */
21
class RegistryContainer implements ArrayAccess, CountableTraversableAggregate
22
{
23
24
    /**
25
     * @var array $container
26
     */
27
    private $container;
28
29
    /**
30
     * RegistryContainer constructor.
31
     * Container data can be seeded by passing parameters to constructor.
32
     * Each parameter will become its own element in the container
33
     */
34
    public function __construct()
35
    {
36
        $this->container = func_get_args();
37
        if (func_num_args() === 0) {
38
            $this->container = array();
39
        }
40
    }
41
42
43
    /**
44
     * @param mixed $offset
45
     * @param mixed $value
46
     */
47
    public function offsetSet($offset, $value)
48
    {
49
        $this->container[$offset] = $value;
50
    }
51
52
53
    /**
54
     * @param mixed $offset
55
     * @return bool
56
     */
57
    public function offsetExists($offset)
58
    {
59
        return isset($this->container[$offset]);
60
    }
61
62
63
    /**
64
     * @param mixed $offset
65
     */
66
    public function offsetUnset($offset)
67
    {
68
        unset($this->container[$offset]);
69
    }
70
71
72
    /**
73
     * @param mixed $offset
74
     * @return mixed|null
75
     */
76
    public function offsetGet($offset)
77
    {
78
        return isset($this->container[$offset]) ? $this->container[$offset] : null;
79
    }
80
81
82
    /**
83
     * @return int
84
     */
85
    public function count()
86
    {
87
        return count($this->container);
88
    }
89
90
91
    /**
92
     * @return ArrayIterator
93
     */
94
    public function getIterator()
95
    {
96
        return new ArrayIterator($this->container);
97
    }
98
99
100
    /**
101
     * @param $offset
102
     * @param $value
103
     */
104
    public function __set($offset, $value)
105
    {
106
        $this->container[$offset] = $value;
107
    }
108
109
110
    /**
111
     * @param $offset
112
     * @return mixed
113
     * @throws OutOfBoundsException
114
     */
115
    public function __get($offset)
116
    {
117
        if (array_key_exists($offset, $this->container)) {
118
            return $this->container[$offset];
119
        }
120
        $trace = debug_backtrace();
121
        throw new OutOfBoundsException(
122
            sprintf(
123
                esc_html__('Invalid offset: %1$s %2$sCalled from %3$s on line %4$d', 'event_espresso'),
124
                $offset,
125
                '<br  />',
126
                $trace[0]['file'],
127
                $trace[0]['line']
128
            )
129
        );
130
    }
131
132
133
    /**
134
     * @param $offset
135
     * @return bool
136
     */
137
    public function __isset($offset)
138
    {
139
        return isset($this->container[$offset]);
140
    }
141
142
143
    /**
144
     * @param $offset
145
     */
146
    public function __unset($offset)
147
    {
148
        unset($this->container[$offset]);
149
    }
150
151
}
152
// Location: RegistryContainer.php
153