Skip to content

Commit 2036f03

Browse files
committed
Merge pull request #475
2 parents 4097115 + 691e260 commit 2036f03

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed

src/Model/TypeMapArrayIterator.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace MongoDB\Model;
1919

2020
use ArrayIterator;
21+
use MongoDB\Exception\BadMethodCallException;
2122

2223
/**
2324
* Iterator for applying a type map to documents in inline command results.
@@ -46,6 +47,28 @@ public function __construct(array $documents = [], array $typeMap)
4647
$this->typeMap = $typeMap;
4748
}
4849

50+
/**
51+
* Not supported.
52+
*
53+
* @see http://php.net/arrayiterator.append
54+
* @throws BadMethodCallException
55+
*/
56+
public function append($value)
57+
{
58+
throw BadMethodCallException::classIsImmutable(__CLASS__);
59+
}
60+
61+
/**
62+
* Not supported.
63+
*
64+
* @see http://php.net/arrayiterator.asort
65+
* @throws BadMethodCallException
66+
*/
67+
public function asort()
68+
{
69+
throw BadMethodCallException::classIsImmutable(__CLASS__);
70+
}
71+
4972
/**
5073
* Return the current element with the type map applied to it.
5174
*
@@ -56,4 +79,93 @@ public function current()
5679
{
5780
return \MongoDB\apply_type_map_to_document(parent::current(), $this->typeMap);
5881
}
82+
83+
/**
84+
* Not supported.
85+
*
86+
* @see http://php.net/arrayiterator.ksort
87+
* @throws BadMethodCallException
88+
*/
89+
public function ksort()
90+
{
91+
throw BadMethodCallException::classIsImmutable(__CLASS__);
92+
}
93+
94+
/**
95+
* Not supported.
96+
*
97+
* @see http://php.net/arrayiterator.natcasesort
98+
* @throws BadMethodCallException
99+
*/
100+
public function natcasesort()
101+
{
102+
throw BadMethodCallException::classIsImmutable(__CLASS__);
103+
}
104+
105+
/**
106+
* Not supported.
107+
*
108+
* @see http://php.net/arrayiterator.natsort
109+
* @throws BadMethodCallException
110+
*/
111+
public function natsort()
112+
{
113+
throw BadMethodCallException::classIsImmutable(__CLASS__);
114+
}
115+
116+
/**
117+
* Return the value from the provided offset with the type map applied.
118+
*
119+
* @see http://php.net/arrayiterator.offsetget
120+
* @param mixed $offset
121+
* @return array|object
122+
*/
123+
public function offsetGet($offset)
124+
{
125+
return \MongoDB\apply_type_map_to_document(parent::offsetGet($offset), $this->typeMap);
126+
}
127+
128+
/**
129+
* Not supported.
130+
*
131+
* @see http://php.net/arrayiterator.offsetset
132+
* @throws BadMethodCallException
133+
*/
134+
public function offsetSet($index, $newval)
135+
{
136+
throw BadMethodCallException::classIsImmutable(__CLASS__);
137+
}
138+
139+
/**
140+
* Not supported.
141+
*
142+
* @see http://php.net/arrayiterator.offsetunset
143+
* @throws BadMethodCallException
144+
*/
145+
public function offsetUnset($index)
146+
{
147+
throw BadMethodCallException::classIsImmutable(__CLASS__);
148+
}
149+
150+
/**
151+
* Not supported.
152+
*
153+
* @see http://php.net/arrayiterator.uasort
154+
* @throws BadMethodCallException
155+
*/
156+
public function uasort($cmp_function)
157+
{
158+
throw BadMethodCallException::classIsImmutable(__CLASS__);
159+
}
160+
161+
/**
162+
* Not supported.
163+
*
164+
* @see http://php.net/arrayiterator.uksort
165+
* @throws BadMethodCallException
166+
*/
167+
public function uksort($cmp_function)
168+
{
169+
throw BadMethodCallException::classIsImmutable(__CLASS__);
170+
}
59171
}

tests/Model/TypeMapArrayIteratorTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,69 @@ public function testCurrentAppliesTypeMap()
3131

3232
$this->assertEquals($expectedDocument, $iterator->current());
3333
}
34+
35+
public function testOffsetGetAppliesTypeMap()
36+
{
37+
$document = [
38+
'array' => [1, 2, 3],
39+
'object' => ['foo' => 'bar'],
40+
];
41+
42+
$typeMap = [
43+
'root' => 'object',
44+
'document' => 'object',
45+
'array' => 'array',
46+
];
47+
48+
$iterator = new TypeMapArrayIterator([$document], $typeMap);
49+
50+
$expectedDocument = (object) [
51+
'array' => [1, 2, 3],
52+
'object' => (object) ['foo' => 'bar'],
53+
];
54+
55+
$iterator->rewind();
56+
57+
$this->assertEquals($expectedDocument, $iterator->offsetGet(0));
58+
}
59+
60+
/**
61+
* @dataProvider provideMutateMethods
62+
* @expectedException MongoDB\Exception\BadMethodCallException
63+
* @expectedExceptionMessage MongoDB\Model\TypeMapArrayIterator is immutable
64+
*/
65+
public function testMutateMethodsCannotBeCalled($method, $args)
66+
{
67+
$document = [
68+
'array' => [1, 2, 3],
69+
'object' => ['foo' => 'bar'],
70+
];
71+
72+
$typeMap = [
73+
'root' => 'object',
74+
'document' => 'object',
75+
'array' => 'array',
76+
];
77+
78+
$iterator = new TypeMapArrayIterator([$document], $typeMap);
79+
80+
$iterator->rewind();
81+
82+
call_user_func_array([$iterator, $method], $args);
83+
}
84+
85+
public function provideMutateMethods()
86+
{
87+
return [
88+
['append', [['x' => 1]]],
89+
['asort', []],
90+
['ksort', []],
91+
['natcasesort', []],
92+
['natsort', []],
93+
['offsetSet', [0, ['x' => 1]]],
94+
['offsetUnset', [0]],
95+
['uasort', [function($a, $b) { return 0; }]],
96+
['uksort', [function($a, $b) { return 0; }]],
97+
];
98+
}
3499
}

0 commit comments

Comments
 (0)