[5341] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * Name: When_Iterator |
---|
| 4 | * Author: Thomas Planer <tplaner@gmail.com> |
---|
| 5 | * Location: http://github.com/tplaner/When |
---|
| 6 | * Created: November 2010 |
---|
| 7 | * 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. |
---|
| 8 | * |
---|
| 9 | * Thanks to Andrew Collington for suggesting the implementation of an Iterator and supplying the base code for it. |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | require_once('When.php'); |
---|
| 13 | |
---|
| 14 | class When_Iterator extends When implements Iterator |
---|
| 15 | { |
---|
| 16 | // store the current position in the array |
---|
| 17 | protected $position = 0; |
---|
| 18 | |
---|
| 19 | // store an individual result if caching is disabled |
---|
| 20 | protected $result; |
---|
| 21 | |
---|
| 22 | // store all of the results |
---|
| 23 | protected $results = array(); |
---|
| 24 | |
---|
| 25 | protected $cache = false; |
---|
| 26 | |
---|
| 27 | // caching the results will cause the script to |
---|
| 28 | // use more memory but less cpu (should also perform quicker) |
---|
| 29 | // |
---|
| 30 | // results should always be the same regardless of cache |
---|
| 31 | public function __construct($cache = false) |
---|
| 32 | { |
---|
| 33 | parent::__construct(); |
---|
| 34 | |
---|
| 35 | $this->position = 0; |
---|
| 36 | $this->results = array(); |
---|
| 37 | $this->cache = $cache; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | public function rewind() |
---|
| 41 | { |
---|
| 42 | if($this->cache) |
---|
| 43 | { |
---|
| 44 | $this->position = 0; |
---|
| 45 | } |
---|
| 46 | else |
---|
| 47 | { |
---|
| 48 | // reset the counter and try_date in the parent class |
---|
| 49 | $this->counter = 0; |
---|
| 50 | $this->try_date = clone $this->start_date; |
---|
| 51 | } |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | public function current() |
---|
| 55 | { |
---|
| 56 | if($this->cache === true) |
---|
| 57 | { |
---|
| 58 | return $this->results[$this->position]; |
---|
| 59 | } |
---|
| 60 | else |
---|
| 61 | { |
---|
| 62 | return $this->result; |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | // only used if caching is enabled |
---|
| 67 | public function key() |
---|
| 68 | { |
---|
| 69 | return $this->position; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | // only used of caching is enabled |
---|
| 73 | public function next() |
---|
| 74 | { |
---|
| 75 | ++$this->position; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | public function valid() |
---|
| 79 | { |
---|
| 80 | if($this->cache === true) |
---|
| 81 | { |
---|
| 82 | // check to see if the current position has already been stored |
---|
| 83 | if(!empty($this->results[$this->position])) |
---|
| 84 | { |
---|
| 85 | return isset($this->results[$this->position]); |
---|
| 86 | } |
---|
| 87 | // if it hasn't been found, check to see if there are more dates |
---|
| 88 | elseif($next_date = parent::next()) |
---|
| 89 | { |
---|
| 90 | $this->results[] = $next_date; |
---|
| 91 | return isset($next_date); |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | else |
---|
| 95 | { |
---|
| 96 | // check to see if there is another result and set that as the result |
---|
| 97 | if($next_date = parent::next()) |
---|
| 98 | { |
---|
| 99 | $this->result = $next_date; |
---|
| 100 | return isset($this->result); |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | // end the foreach loop when all options are exhausted |
---|
| 105 | return false; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | public function enable_cache($cache) |
---|
| 109 | { |
---|
| 110 | $this->cache = $cache; |
---|
| 111 | } |
---|
| 112 | } |
---|