|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2017 MongoDB, Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace MongoDB\Model; |
| 19 | + |
| 20 | +use Countable; |
| 21 | +use Generator; |
| 22 | +use Iterator; |
| 23 | +use Traversable; |
| 24 | + |
| 25 | +/** |
| 26 | + * Iterator for wrapping a Traversable and caching its results. |
| 27 | + * |
| 28 | + * By caching results, this iterators allows a Traversable to be counted and |
| 29 | + * rewound multiple times, even if the wrapped object does not natively support |
| 30 | + * those operations (e.g. MongoDB\Driver\Cursor). |
| 31 | + * |
| 32 | + * @internal |
| 33 | + */ |
| 34 | +class CachingIterator implements Countable, Iterator |
| 35 | +{ |
| 36 | + private $items; |
| 37 | + private $iterator; |
| 38 | + private $iteratorExhausted = false; |
| 39 | + |
| 40 | + /** |
| 41 | + * @param Traversable $traversable |
| 42 | + */ |
| 43 | + public function __construct(Traversable $traversable) |
| 44 | + { |
| 45 | + $this->iterator = $this->wrapTraversable($traversable); |
| 46 | + $this->storeCurrentItem(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @see http://php.net/countable.count |
| 51 | + * @return integer |
| 52 | + */ |
| 53 | + public function count() |
| 54 | + { |
| 55 | + $this->exhaustIterator(); |
| 56 | + |
| 57 | + return count($this->items); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @see http://php.net/iterator.current |
| 62 | + * @return mixed |
| 63 | + */ |
| 64 | + public function current() |
| 65 | + { |
| 66 | + return current($this->items); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @see http://php.net/iterator.mixed |
| 71 | + * @return mixed |
| 72 | + */ |
| 73 | + public function key() |
| 74 | + { |
| 75 | + return key($this->items); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @see http://php.net/iterator.next |
| 80 | + * @return void |
| 81 | + */ |
| 82 | + public function next() |
| 83 | + { |
| 84 | + if ( ! $this->iteratorExhausted) { |
| 85 | + $this->iterator->next(); |
| 86 | + $this->storeCurrentItem(); |
| 87 | + } |
| 88 | + |
| 89 | + next($this->items); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @see http://php.net/iterator.rewind |
| 94 | + * @return void |
| 95 | + */ |
| 96 | + public function rewind() |
| 97 | + { |
| 98 | + $this->exhaustIterator(); |
| 99 | + reset($this->items); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * |
| 104 | + * @see http://php.net/iterator.valid |
| 105 | + * @return boolean |
| 106 | + */ |
| 107 | + public function valid() |
| 108 | + { |
| 109 | + return $this->key() !== null; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Ensures that the inner iterator is fully consumed and cached. |
| 114 | + */ |
| 115 | + private function exhaustIterator() |
| 116 | + { |
| 117 | + while ( ! $this->iteratorExhausted) { |
| 118 | + $this->next(); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Stores the current item in the cache. |
| 124 | + */ |
| 125 | + private function storeCurrentItem() |
| 126 | + { |
| 127 | + $key = $this->iterator->key(); |
| 128 | + |
| 129 | + if ($key === null) { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + $this->items[$key] = $this->iterator->current(); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Wraps the Traversable with a Generator. |
| 138 | + * |
| 139 | + * @param Traversable $traversable |
| 140 | + * @return Generator |
| 141 | + */ |
| 142 | + private function wrapTraversable(Traversable $traversable) |
| 143 | + { |
| 144 | + foreach ($traversable as $key => $value) { |
| 145 | + yield $key => $value; |
| 146 | + } |
| 147 | + |
| 148 | + $this->iteratorExhausted = true; |
| 149 | + } |
| 150 | +} |
0 commit comments