Completed
Push — master ( f49e02...f705ca )
by Johnny
02:06
created

Reflectable::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: johnny
5
 * Date: 11-04-16
6
 * Time: 23:06
7
 */
8
9
namespace Redbox\DNS;
10
11
12
class Reflectable implements \ArrayAccess, \Countable
13
{
14
    /**
15
     * Data
16
     *
17
     * @var array
18
     * @access private
19
     */
20
    private $data = [];
21
22
    /**
23
     * Get a data by key
24
     *
25
     * @param string The key data to retrieve
26
     * @access public
27
     */
28
    public function &__get($key)
29
    {
30
        return $this->data[$key];
31
    }
32
33
34
    /**
35
     * Assigns a value to the specified data
36
     *
37
     * @param string The data key to assign the value to
38
     * @param mixed  The value to set
39
     * @access public
40
     */
41
    public function __set($key, $value)
42
    {
43
        $this->data[$key] = $value;
44
    }
45
46
    /**
47
     * Whether or not an data exists by key
48
     *
49
     * @param string An data key to check for
50
     * @access public
51
     * @return boolean
52
     * @abstracting ArrayAccess
53
     */
54
    public function __isset($key)
55
    {
56
        return isset($this->data[$key]);
57
    }
58
59
    /**
60
     * Unsets an data by key
61
     *
62
     * @param string The key to unset
63
     * @access public
64
     */
65
    public function __unset($key)
66
    {
67
        unset($this->data[$key]);
68
    }
69
70
    /**
71
     * Assigns a value to the specified offset
72
     *
73
     * @param string The offset to assign the value to
74
     * @param mixed  The value to set
75
     * @access public
76
     * @abstracting ArrayAccess
77
     */
78
    public function offsetSet($offset, $value)
79
    {
80
        if (is_null($offset)) {
81
            $this->data[] = $value;
82
        } else {
83
            $this->data[$offset] = $value;
84
        }
85
    }
86
87
    /**
88
     * Whether or not an offset exists
89
     *
90
     * @param string An offset to check for
91
     * @access public
92
     * @return boolean
93
     * @abstracting ArrayAccess
94
     */
95
    public function offsetExists($offset)
96
    {
97
        return isset($this->data[$offset]);
98
    }
99
100
    /**
101
     * Unsets an offset
102
     *
103
     * @param string The offset to unset
104
     * @access public
105
     * @abstracting ArrayAccess
106
     */
107
    public function offsetUnset($offset)
108
    {
109
        if ($this->offsetExists($offset)) {
110
            unset($this->data[$offset]);
111
        }
112
    }
113
114
    /**
115
     * Returns the value at specified offset
116
     *
117
     * @param string The offset to retrieve
118
     * @access public
119
     * @return mixed
120
     * @abstracting ArrayAccess
121
     */
122
    public function offsetGet($offset)
123
    {
124
        return $this->offsetExists($offset) ? $this->data[$offset] : null;
125
    }
126
127
    public function clear()
128
    {
129
        $this->rewind();
130
131
        foreach($this->data as $key => $value) {
132
            unset($this->data[$key]);
133
        }
134
    }
135
136
    public function rewind() {
137
        reset($this->data);
138
    }
139
140
    public function count() {
141
        return count(array_keys($this->data));
142
    }
143
}