Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
0d5b7c48
Commit
0d5b7c48
authored
Oct 26, 2017
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-81: Move CachingIterator to MongoDB\Model
parent
9f677306
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
160 additions
and
7 deletions
+160
-7
CachingIterator.php
src/Model/CachingIterator.php
+150
-0
CachingIteratorTest.php
tests/Model/CachingIteratorTest.php
+10
-7
No files found.
src/CachingIterator.php
→
src/
Model/
CachingIterator.php
View file @
0d5b7c48
<?php
namespace
MongoDB
;
class
CachingIterator
implements
\Iterator
,
\Countable
{
/**
* @var \Traversable
*/
private
$iterator
;
/**
* @var array
/*
* Copyright 2017 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
private
$items
=
[];
/**
* @var bool
namespace
MongoDB\Model
;
use
Countable
;
use
Generator
;
use
Iterator
;
use
Traversable
;
/**
* Iterator for wrapping a Traversable and caching its results.
*
* By caching results, this iterators allows a Traversable to be counted and
* rewound multiple times, even if the wrapped object does not natively support
* those operations (e.g. MongoDB\Driver\Cursor).
*
* @internal
*/
class
CachingIterator
implements
Countable
,
Iterator
{
private
$items
;
private
$iterator
;
private
$iteratorExhausted
=
false
;
/**
* @param
\Traversable $iterator
* @param
Traversable $traversable
*/
public
function
__construct
(
\Traversable
$iterator
)
public
function
__construct
(
Traversable
$traversable
)
{
$this
->
iterator
=
$this
->
wrapTraversable
(
$
iterator
);
$this
->
iterator
=
$this
->
wrapTraversable
(
$
traversable
);
$this
->
storeCurrentItem
();
}
/**
* @return int
* @see http://php.net/countable.count
* @return integer
*/
public
function
count
()
{
$this
->
exhaustIterator
();
return
count
(
$this
->
items
);
}
/**
* @see http://php.net/iterator.current
* @return mixed
*/
public
function
current
()
...
...
@@ -46,6 +67,7 @@ class CachingIterator implements \Iterator, \Countable
}
/**
* @see http://php.net/iterator.mixed
* @return mixed
*/
public
function
key
()
...
...
@@ -54,11 +76,12 @@ class CachingIterator implements \Iterator, \Countable
}
/**
* @see http://php.net/iterator.next
* @return void
*/
public
function
next
()
{
if
(
!
$this
->
iteratorExhausted
)
{
if
(
!
$this
->
iteratorExhausted
)
{
$this
->
iterator
->
next
();
$this
->
storeCurrentItem
();
}
...
...
@@ -67,6 +90,7 @@ class CachingIterator implements \Iterator, \Countable
}
/**
* @see http://php.net/iterator.rewind
* @return void
*/
public
function
rewind
()
...
...
@@ -76,7 +100,9 @@ class CachingIterator implements \Iterator, \Countable
}
/**
* @return bool
*
* @see http://php.net/iterator.valid
* @return boolean
*/
public
function
valid
()
{
...
...
@@ -84,21 +110,23 @@ class CachingIterator implements \Iterator, \Countable
}
/**
* Ensures th
e original iterator is fully consumed and all items cached
* Ensures th
at the inner iterator is fully consumed and cached.
*/
private
function
exhaustIterator
()
{
while
(
!
$this
->
iteratorExhausted
)
{
while
(
!
$this
->
iteratorExhausted
)
{
$this
->
next
();
}
}
/**
* Stores the current item
* Stores the current item
in the cache.
*/
private
function
storeCurrentItem
()
{
if
(
null
===
$key
=
$this
->
iterator
->
key
())
{
$key
=
$this
->
iterator
->
key
();
if
(
$key
===
null
)
{
return
;
}
...
...
@@ -106,14 +134,17 @@ class CachingIterator implements \Iterator, \Countable
}
/**
* @param \Traversable $traversable
* @return \Generator
* Wraps the Traversable with a Generator.
*
* @param Traversable $traversable
* @return Generator
*/
private
function
wrapTraversable
(
\
Traversable
$traversable
)
private
function
wrapTraversable
(
Traversable
$traversable
)
{
foreach
(
$traversable
as
$key
=>
$value
)
{
yield
$key
=>
$value
;
}
$this
->
iteratorExhausted
=
true
;
}
}
tests/CachingIteratorTest.php
→
tests/
Model/
CachingIteratorTest.php
View file @
0d5b7c48
<?php
namespace
MongoDB\Tests
;
namespace
MongoDB\Tests
\Model
;
use
MongoDB\CachingIterator
;
use
MongoDB\
Model\
CachingIterator
;
class
CachingIteratorTest
extends
\PHPUnit_Framework_TestCase
{
/**
* Sanity check for all following tests
* Sanity check for all following tests.
*
* @expectedException \Exception
* @expectedExceptionMessage Cannot traverse an already closed generator
*/
public
function
testTravers
e
GeneratorConsumesIt
()
public
function
testTravers
ing
GeneratorConsumesIt
()
{
$iterator
=
$this
->
getTraversable
([
1
,
2
,
3
]);
$this
->
assertSame
([
1
,
2
,
3
],
iterator_to_array
(
$iterator
));
$this
->
assertSame
([
1
,
2
,
3
],
iterator_to_array
(
$iterator
));
}
public
function
testIterat
eOverItems
()
public
function
testIterat
ion
()
{
$iterator
=
new
CachingIterator
(
$this
->
getTraversable
([
1
,
2
,
3
]));
$expectedKey
=
0
;
$expectedItem
=
1
;
foreach
(
$iterator
as
$key
=>
$item
)
{
$this
->
assertSame
(
$expectedKey
++
,
$key
);
$this
->
assertSame
(
$expectedItem
++
,
$item
);
}
$this
->
assertFalse
(
$iterator
->
valid
());
}
public
function
test
IteratePartiallyThenRewind
()
public
function
test
RewindAfterPartialIteration
()
{
$iterator
=
new
CachingIterator
(
$this
->
getTraversable
([
1
,
2
,
3
]));
...
...
@@ -47,7 +50,7 @@ class CachingIteratorTest extends \PHPUnit_Framework_TestCase
$this
->
assertCount
(
3
,
$iterator
);
}
public
function
testCountAfterPartial
lyIterating
()
public
function
testCountAfterPartial
Iteration
()
{
$iterator
=
new
CachingIterator
(
$this
->
getTraversable
([
1
,
2
,
3
]));
$iterator
->
next
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment