-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfilter-iterator.php
91 lines (80 loc) · 2.77 KB
/
filter-iterator.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
<?php
/**
* This class demonstrates a "real world" example of filtering users out from a
* mock array. Pretend the data came from your model and you only need to match a subset
* of the returned results. This class is an example of filtering any array
* by either key or value with a few minor adjustments.
*
* @Class FilterExample
*/
class ObjectFilter extends FilterIterator
{
protected $_filterKey;
protected $_filterVal;
/**
* Calls the parent FilterIterator constructor.
*
* @param Iterator $it An iterator object
* @param mixed $filterKey
* @param mixed $filterVal
*
*/
public function __construct(Iterator $it, $filterKey, $filterVal)
{
parent::__construct($it);
$this->_filterKey = $filterKey;
$this->_filterVal = $filterVal;
}
/**
* The accept method is required, as FilterExample
* extends FilterIterator with abstract method accept().
*
* @access public
* @accept Only allow values that are not ___
* @return string
*/
public function accept()
{
$object = $this->getInnerIterator()->current();
// base case
if (!isset($object[$this->_filterKey])) return true;
// if the key and value match the filter
if (strcasecmp($object[$this->_filterKey], $this->_filterVal) == 0) {
return false;
}
return true;
}
}
// load up an append iterator
$it = new AppendIterator();
// create some example users as ArrayIterators
$user1 = new ArrayIterator(array('id' => 1, 'name' => 'George'));
$user2 = new ArrayIterator(array('id' => 2, 'name' => 'John'));
$user3 = new ArrayIterator(array('id' => 3, 'name' => 'Eric'));
$user4 = new ArrayIterator(array('id' => 4, 'name' => 'Jason'));
$user5 = new ArrayIterator(array('id' => 5, 'name' => 'Emanuel'));
// filter and append the ArrayIterators
$it->append(new ObjectFilter($user1, 'name', 'Eric'));
$it->append(new ObjectFilter($user2, 'name', 'Eric'));
$it->append(new ObjectFilter($user3, 'name', 'Eric'));
$it->append(new ObjectFilter($user4, 'name', 'Eric'));
$it->append(new ObjectFilter($user5, 'name', 'Eric'));
// show the example filtered output
foreach($it as $key => $val) {
echo $key . ' = ' . $val . PHP_EOL;
}
/**
* This is the same example, but utilising ArrayObject
* instead of AppendIterator and ArrayIterator.
*/
$users = array(
array('id' => 1, 'name' => 'George'),
array('id' => 2, 'name' => 'John'),
array('id' => 3, 'name' => 'Eric'),
array('id' => 4, 'name' => 'Jason'),
array('id' => 5, 'name' => 'Emanuel')
);
// convert users to ArrayObject
$users = new ArrayObject($users);
// filter out all user's with the name "John"
$it = new ObjectFilter($users->getIterator(), 'name', 'john');