Created
January 25, 2013 16:48
-
-
Save jmikola/4635953 to your computer and use it in GitHub Desktop.
Testing iterator implementations that return non-scalars for keys. See: https://jira.mongodb.org/browse/PHP-570
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class BadIdeaIterator implements Iterator | |
{ | |
protected $position; | |
protected $keys; | |
protected $values; | |
public function __construct(array $keys, array $values) | |
{ | |
$this->position = 0; | |
$this->keys = array_values($keys); | |
$this->values = array_values($values); | |
if (count($this->keys) !== count($this->values)) { | |
throw new InvalidArgumentException('Number of keys and values differ'); | |
} | |
} | |
public function current() | |
{ | |
return $this->values[$this->position]; | |
} | |
public function key() | |
{ | |
return $this->keys[$this->position]; | |
} | |
public function next() | |
{ | |
++$this->position; | |
} | |
public function rewind() | |
{ | |
$this->position = 0; | |
} | |
public function valid() | |
{ | |
return array_key_exists($this->position, $this->keys); | |
} | |
} | |
$keys = [null, new MongoId(), ['x' => 1, 'y' => 2]]; | |
$values = ['a', 'b', 'c']; | |
$a = new BadIdeaIterator($keys, $values); | |
var_dump(iterator_to_array($a, false)); | |
var_dump(iterator_to_array($a, true)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment