/** * Build a Collection or Generic depending * on what you pass into the function. * * @param array $array * * @return Generic|Collection */ public static function build(array $array) { // If we pass a HashMap, return a Generic. if (has_string_keys($array)) { return new Generic($array); } // Otherwise, turn each member into a Generic where appropriate. $array = array_map(function ($item) { return new Generic($item); }, $array); // Return a Collection of Generics. return new Collection($array); }
/** * Generic constructor. * * This also turns elements of the original attribute array into * Generic's themselves if they are string key'd arrays. * Otherwise, they are returned as a new collection of Generic's. * * @param array $attributes */ public function __construct(array $attributes = []) { array_walk($attributes, function (&$item, $key) { if (is_array($item)) { if (has_string_keys($item)) { $item = new self($item); } else { $item = new Collection($item); } } }); $this->attributes = $attributes; }