forked from tplaner/When
-
Notifications
You must be signed in to change notification settings - Fork 0
/
When_Iterator.php
112 lines (98 loc) · 2.37 KB
/
When_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* Name: When_Iterator
* Author: Thomas Planer <[email protected]>
* Location: http://github.com/tplaner/When
* Created: November 2010
* Description: Implements PHP's Object Iteration Interface (http://us.php.net/Iterator & http://php.net/manual/en/class.iterator.php) so you can use the object within a foreach loop.
*
* Thanks to Andrew Collington for suggesting the implementation of an Iterator and supplying the base code for it.
*/
require_once('When.php');
class When_Iterator extends When implements Iterator
{
// store the current position in the array
protected $position = 0;
// store an individual result if caching is disabled
protected $result;
// store all of the results
protected $results = array();
protected $cache = false;
// caching the results will cause the script to
// use more memory but less cpu (should also perform quicker)
//
// results should always be the same regardless of cache
public function __construct($cache = false)
{
parent::__construct();
$this->position = 0;
$this->results = array();
$this->cache = $cache;
}
public function rewind()
{
if($this->cache)
{
$this->position = 0;
}
else
{
// reset the counter and try_date in the parent class
$this->counter = 0;
$this->try_date = clone $this->start_date;
}
}
public function current()
{
if($this->cache === true)
{
return $this->results[$this->position];
}
else
{
return $this->result;
}
}
// only used if caching is enabled
public function key()
{
return $this->position;
}
// only used of caching is enabled
public function next()
{
++$this->position;
}
public function valid()
{
if($this->cache === true)
{
// check to see if the current position has already been stored
if(!empty($this->results[$this->position]))
{
return isset($this->results[$this->position]);
}
// if it hasn't been found, check to see if there are more dates
elseif($next_date = parent::next())
{
$this->results[] = $next_date;
return isset($next_date);
}
}
else
{
// check to see if there is another result and set that as the result
if($next_date = parent::next())
{
$this->result = $next_date;
return isset($this->result);
}
}
// end the foreach loop when all options are exhausted
return false;
}
public function enable_cache($cache)
{
$this->cache = $cache;
}
}