This repository was archived by the owner on Aug 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterableCountable.php
More file actions
171 lines (151 loc) · 3.05 KB
/
IterableCountable.php
File metadata and controls
171 lines (151 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* A countable iterator with additional magic functions.
*
* This iterator uses the method names 'set', 'get', 'has', 'remove' for CRUD operations.
* The method 'set' combines the insert and update commands.
* Magic function wrappers are defined for those four methods. (See examples)
*
* Example 1:
* <code>
* $ic = new IterableCountable();
* $ic->set( 'key1', 'value1' );
* $ic->set( 'key2', 'value2' );
* echo count( $ic );
* echo (int) $ic->has( 'key2' );
* $ic->remove( 'key2' );
* </code>
*
* Example 2 – iteration:
* <code>
* foreach( $ic as $key => $value ) var_dump( $key, $value );
* </code>
*
* Example 3 – using magic functions:
* <code>
* $ic = new IterableCountable();
* $ic->key1 = 'value1';
* echo $ic->key1;
* echo (int) isset( $ic->key1 );
* unset( $ic->key1 );
* </code>
*/
class IterableCountable implements \Iterator, \Countable
{
protected $data = array();
/**
* @param string $key
* @param mixed $value
* @return boolean|IterableCountable
*/
public function set( $key, $value )
{
$this->data[$key] = $value;
return $this;
}
/**
* @param string $key
* @return mixed|null
*/
public function get( $key )
{
return isset( $this->data[$key] ) ? $this->data[$key] : null;
}
/**
* @param string $key
* @return boolean
*/
public function has( $key )
{
return isset( $this->data[$key] );
}
/**
* @param string $key
* @return IterableCountable
*/
public function remove( $key )
{
unset( $this->data[$key] );
return $this;
}
/**
* @return array
*/
public function getArray()
{
return $this->data;
}
/* methods from interface Iterator */
/**
* @return mixed
*/
public function current()
{
return current( $this->data );
}
/**
* @return scalar
*/
public function key()
{
return key( $this->data );
}
/**
* @return void
*/
public function next()
{
next( $this->data );
}
/**
* @return void
*/
public function rewind()
{
reset( $this->data );
}
/**
* @return boolean
*/
public function valid()
{
return key( $this->data ) !== null;
}
/* methods from interface Countable */
/**
* @return int
*/
public function count()
{
return count( $this->data );
}
/* magic wrapper */
/**
* wrapper for self::set()
*/
public function __set( $key, $value )
{
return $this->set( $key, $value );
}
/**
* wrapper for self::get()
*/
public function __get( $key )
{
return $this->get( $key );
}
/**
* wrapper for self::has()
*/
public function __isset( $key )
{
return $this->has( $key );
}
/**
* wrapper for self::remove()
*/
public function __unset( $key )
{
return $this->remove( $key );
}
}