This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PCollection.php
234 lines (201 loc) · 4.25 KB
/
PCollection.php
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
namespace Poundation;
require_once 'PObject.php';
use Poundation\PObject;
/**
* @abstract PCollection is an abstract class to manage collections.
* @author danielkbx
*/
abstract class PCollection extends PObject implements \Iterator, \Countable, \ArrayAccess {
const CollectionIndexNotFound = - 1;
protected $map;
protected $iteratorPosition;
/**
*/
function __construct() {
$this->map = array ();
$this->iteratorPosition = 0;
}
/**
* Returns the number of elements.
*
* @return integer
*/
function count() {
return count ( $this->map );
}
/**
* Checks if the given element is contained in the collection.
*
* @param $object The
* element to check
* @return boolean
*/
function contains($object) {
return (in_array ( $object, $this->map ));
}
/**
* Returns the index of the given object.
* @param unknown_type $object
* @return integer
*/
function indexOfObject($object) {
foreach ( $this->map as $key => $value ) {
if ($value == $object) {
return $key;
break;
}
}
return self::CollectionIndexNotFound;
}
/**
* Returns the object with the given index.
* @param integer $index
* @return Object
*/
function objectForIndex($index) {
if (isset ( $this->map [$index] )) {
return $this->map [$index];
} else {
return NULL;
}
}
/**
* Returns the first object of the collection.
* @return object
*/
function firstObject() {
return $this->objectForIndex(0);
}
/**
* Returns the last object of the collection.
* @return object
*/
function lastObject() {
if ($this->count() > 0) {
return $this->objectForIndex($this->count() - 1);
} else {
return NULL;
}
}
/**
* Returns a string with all elements glued together with the given string.
* @param string $glue
* @return \Poundation\PString
*/
function string($glue) {
return new PString(implode($glue, $this->map));
}
/**
* Filters the collection using the filter descriptor.
* @param PFilterDescriptor $descriptor
*
* @return bool
*/
function filterUsingFilterDescriptor(PFilterDescriptor $descriptor) {
$indexesToRemove = array();
foreach($this->map as $key => $value) {
if (!$descriptor->doesElementMatch($value)) {
$indexesToRemove[] = $key;
}
}
foreach($indexesToRemove as $indexToRemove) {
unset($this->map[$indexToRemove]);
}
return true;
}
/**
* Filters the collection by the given property name and value (using a filter descriptor internally).
* @param $property
* @param $value
*
* @return bool
*/
function filterByProperty($property, $value) {
if (is_string($property)) {
return $this->filterUsingFilterDescriptor(new PFilterDescriptor($property, $value));
} else {
return false;
}
}
// Iteration methods
/*
* (non-PHPdoc)
* @see Iterator::current()
*/
public function current() {
return current($this->map);
//return $this->map [$this->iteratorPosition];
}
/*
* (non-PHPdoc)
* @see Iterator::key()
*/
public function key() {
return key($this->map);
//return $this->iteratorPosition;
}
/*
* (non-PHPdoc)
* @see Iterator::next()
*/
public function next() {
return next($this->map);
//++ $this->iteratorPosition;
}
/*
* (non-PHPdoc)
* @see Iterator::rewind()
*/
public function rewind() {
return reset($this->map);
//$this->iteratorPosition = 0;
}
/*
* (non-PHPdoc)
* @see Iterator::valid()
*/
public function valid() {
return ($this->current() !== false);
//return (isset ( $this->map [$this->iteratorPosition] ));
}
// Array Access methods
/*
* (non-PHPdoc)
* @see ArrayAccess::offsetExists()
*/
public function offsetExists($offset) {
return (isset($this->map[$offset]));
}
/*
* (non-PHPdoc)
* @see ArrayAccess::offsetGet()
*/
public function offsetGet($offset) {
if ($this->offsetExists($offset)) {
return $this->map[$offset];
} else {
return NULL;
}
}
/*
* (non-PHPdoc)
* @see ArrayAccess::offsetSet()
*/
public function offsetSet($offset, $value) {}
/*
* (non-PHPdoc)
* @see ArrayAccess::offsetUnset()
*/
public function offsetUnset($offset) {
unset($this->map[$offset]);
}
/**
* Returns a PHP-native array.
* @return array
*/
public function nativeArray() {
return $this->map;
}
}
?>