public function getObjectList($selector, $className = '\\Sokil\\Mongo\\Structure')
{
$data = $this->get($selector);
if (!$data || !is_array($data)) {
return array();
}
// class name is string
if (is_string($className)) {
$list = array_map(function ($dataItem) use($className) {
$listItemStructure = new $className();
if (!$listItemStructure instanceof Structure) {
throw new Exception('Wrong structure class specified');
}
$listItemStructure->mergeUnmodified($dataItem);
return $listItemStructure;
}, $data);
return $list;
}
// class name id callable
if (is_callable($className)) {
return array_map(function ($dataItem) use($className) {
$classNameString = $className($dataItem);
$listItemStructure = new $classNameString();
if (!$listItemStructure instanceof Structure) {
throw new Exception('Wrong structure class specified');
}
return $listItemStructure->merge($dataItem);
}, $data);
}
throw new Exception('Wrong class name specified. Use string or closure');
}