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
2036f03f
Commit
2036f03f
authored
Feb 02, 2018
by
Katherine Walker
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #475
parents
40971158
691e260c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
177 additions
and
0 deletions
+177
-0
TypeMapArrayIterator.php
src/Model/TypeMapArrayIterator.php
+112
-0
TypeMapArrayIteratorTest.php
tests/Model/TypeMapArrayIteratorTest.php
+65
-0
No files found.
src/Model/TypeMapArrayIterator.php
View file @
2036f03f
...
...
@@ -18,6 +18,7 @@
namespace
MongoDB\Model
;
use
ArrayIterator
;
use
MongoDB\Exception\BadMethodCallException
;
/**
* Iterator for applying a type map to documents in inline command results.
...
...
@@ -46,6 +47,28 @@ class TypeMapArrayIterator extends ArrayIterator
$this
->
typeMap
=
$typeMap
;
}
/**
* Not supported.
*
* @see http://php.net/arrayiterator.append
* @throws BadMethodCallException
*/
public
function
append
(
$value
)
{
throw
BadMethodCallException
::
classIsImmutable
(
__CLASS__
);
}
/**
* Not supported.
*
* @see http://php.net/arrayiterator.asort
* @throws BadMethodCallException
*/
public
function
asort
()
{
throw
BadMethodCallException
::
classIsImmutable
(
__CLASS__
);
}
/**
* Return the current element with the type map applied to it.
*
...
...
@@ -56,4 +79,93 @@ class TypeMapArrayIterator extends ArrayIterator
{
return
\MongoDB\apply_type_map_to_document
(
parent
::
current
(),
$this
->
typeMap
);
}
/**
* Not supported.
*
* @see http://php.net/arrayiterator.ksort
* @throws BadMethodCallException
*/
public
function
ksort
()
{
throw
BadMethodCallException
::
classIsImmutable
(
__CLASS__
);
}
/**
* Not supported.
*
* @see http://php.net/arrayiterator.natcasesort
* @throws BadMethodCallException
*/
public
function
natcasesort
()
{
throw
BadMethodCallException
::
classIsImmutable
(
__CLASS__
);
}
/**
* Not supported.
*
* @see http://php.net/arrayiterator.natsort
* @throws BadMethodCallException
*/
public
function
natsort
()
{
throw
BadMethodCallException
::
classIsImmutable
(
__CLASS__
);
}
/**
* Return the value from the provided offset with the type map applied.
*
* @see http://php.net/arrayiterator.offsetget
* @param mixed $offset
* @return array|object
*/
public
function
offsetGet
(
$offset
)
{
return
\MongoDB\apply_type_map_to_document
(
parent
::
offsetGet
(
$offset
),
$this
->
typeMap
);
}
/**
* Not supported.
*
* @see http://php.net/arrayiterator.offsetset
* @throws BadMethodCallException
*/
public
function
offsetSet
(
$index
,
$newval
)
{
throw
BadMethodCallException
::
classIsImmutable
(
__CLASS__
);
}
/**
* Not supported.
*
* @see http://php.net/arrayiterator.offsetunset
* @throws BadMethodCallException
*/
public
function
offsetUnset
(
$index
)
{
throw
BadMethodCallException
::
classIsImmutable
(
__CLASS__
);
}
/**
* Not supported.
*
* @see http://php.net/arrayiterator.uasort
* @throws BadMethodCallException
*/
public
function
uasort
(
$cmp_function
)
{
throw
BadMethodCallException
::
classIsImmutable
(
__CLASS__
);
}
/**
* Not supported.
*
* @see http://php.net/arrayiterator.uksort
* @throws BadMethodCallException
*/
public
function
uksort
(
$cmp_function
)
{
throw
BadMethodCallException
::
classIsImmutable
(
__CLASS__
);
}
}
tests/Model/TypeMapArrayIteratorTest.php
View file @
2036f03f
...
...
@@ -31,4 +31,69 @@ class TypeMapArrayIteratorTest extends TestCase
$this
->
assertEquals
(
$expectedDocument
,
$iterator
->
current
());
}
public
function
testOffsetGetAppliesTypeMap
()
{
$document
=
[
'array'
=>
[
1
,
2
,
3
],
'object'
=>
[
'foo'
=>
'bar'
],
];
$typeMap
=
[
'root'
=>
'object'
,
'document'
=>
'object'
,
'array'
=>
'array'
,
];
$iterator
=
new
TypeMapArrayIterator
([
$document
],
$typeMap
);
$expectedDocument
=
(
object
)
[
'array'
=>
[
1
,
2
,
3
],
'object'
=>
(
object
)
[
'foo'
=>
'bar'
],
];
$iterator
->
rewind
();
$this
->
assertEquals
(
$expectedDocument
,
$iterator
->
offsetGet
(
0
));
}
/**
* @dataProvider provideMutateMethods
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessage MongoDB\Model\TypeMapArrayIterator is immutable
*/
public
function
testMutateMethodsCannotBeCalled
(
$method
,
$args
)
{
$document
=
[
'array'
=>
[
1
,
2
,
3
],
'object'
=>
[
'foo'
=>
'bar'
],
];
$typeMap
=
[
'root'
=>
'object'
,
'document'
=>
'object'
,
'array'
=>
'array'
,
];
$iterator
=
new
TypeMapArrayIterator
([
$document
],
$typeMap
);
$iterator
->
rewind
();
call_user_func_array
([
$iterator
,
$method
],
$args
);
}
public
function
provideMutateMethods
()
{
return
[
[
'append'
,
[[
'x'
=>
1
]]],
[
'asort'
,
[]],
[
'ksort'
,
[]],
[
'natcasesort'
,
[]],
[
'natsort'
,
[]],
[
'offsetSet'
,
[
0
,
[
'x'
=>
1
]]],
[
'offsetUnset'
,
[
0
]],
[
'uasort'
,
[
function
(
$a
,
$b
)
{
return
0
;
}]],
[
'uksort'
,
[
function
(
$a
,
$b
)
{
return
0
;
}]],
];
}
}
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