private function extract($iterator, $className = null)
{
if (is_numeric($iterator->key())) {
$container = [];
} else {
if (empty($className)) {
$container = $this;
} else {
$container = new $className();
}
}
while ($iterator->valid()) {
$key = $iterator->key();
$value = $iterator->current();
if (DbRef::isRef($value)) {
$value = new DbRef($this->collection, $value);
} elseif (is_array($value)) {
$value = $this->extract($iterator->getChildren(), 'stdClass');
}
if (is_numeric($key)) {
$container[$key] = $value;
} else {
$container->{$key} = $value;
}
$iterator->next();
}
return $container;
}