Sokil\Mongo\Structure::getObjectList PHP Method

getObjectList() public method

Get list of structure objects from list of values in mongo document
public getObjectList ( string $selector, string | callable $className = '\Sokil\Mongo\Structure' ) : object
$selector string
$className string | callable Structure class name or closure, which accept data and return string class name of Structure
return object representation of document with class, passed as argument
    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');
    }