Commit 2036f03f authored by Katherine Walker's avatar Katherine Walker

Merge pull request #475

parents 40971158 691e260c
......@@ -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__);
}
}
......@@ -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; }]],
];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment