public function schema() { $data = Libraries::locate('models'); $models = new Collection(compact('data')); if ($this->request->is('json')) { $models->each(function ($model) { $schema = is_callable(array($model, 'schema')) ? $model::schema() : array(); return array($model => $schema ? $schema->fields() : array()); }); } return compact('models'); }
/** * Applies a callback to all data in the collection. * * Overridden to load any data that has not yet been loaded. * * @param callback $filter The filter to apply. * @return object This collection instance. */ public function each($filter) { if (!$this->closed()) { while ($this->next()) { } } return parent::each($filter); }
/** * Tests that the `each()` filter applies the callback to each item in the current collection, * returning an instance of itself. * * @return void */ public function testCollectionEachFilter() { $collection = new Collection(array('data' => array(1, 2, 3, 4, 5))); $filter = function ($item) { return ++$item; }; $result = $collection->each($filter); $this->assertIdentical($collection, $result); $this->assertEqual(array(2, 3, 4, 5, 6), $collection->to('array')); }
/** * Applies a callback to all data in the collection. * * Overridden to load any data that has not yet been loaded. * * @param callback $filter The filter to apply. * @return object This collection instance. */ public function each($filter) { $this->offsetGet(null); return parent::each($filter); }
/** * Loads entities and records from file-based structure * * Trys to implement a similar method to load datasets not from database, but rather from * a file that holds all relevant model data and is laid into the filesystem of each library. * This allows for easy default-data to be loaded without using a database as backend. * * Put your files into `{:library}\data\{:class}\{:name}.neon` and let the content be found * with loading just the id or slug of that file. * * Attention: This feature is considered experimental and should be used with care. It might * not work as expected. Also, not all features are implemented. * * If nothing is found, it just returns null or an empty array to ensure a falsey value. * * @param string|array $model fully namespaced model class, e.g. `radium\models\Contents` * can also be an array, in which case `key` and `source` must be given * according to the internal structure of `Model::meta()`. * @param string $type The find type, which is looked up in `Model::$_finders`. By default it * accepts `all`, `first`, `list` and `count`. Later two are not implement, yet. * @param array $options Options for the query. By default, accepts: * - `conditions`: The conditional query elements, e.g. * `'conditions' => array('published' => true)` * - `fields`: The fields that should be retrieved. When set to `null`, defaults to * all fields. * - `order`: The order in which the data will be returned, e.g. `'order' => 'ASC'`. * - `limit`: The maximum number of records to return. * - `page`: For pagination of data. * @return mixed returns null or an empty array if nothing is found * If `$type` is `first` returns null or the correct entity with given data * If `$type` is `all` returns null or a DocumentSet object with loaded entities */ public static function find($model, $type, array $options = array()) { $defaults = array('conditions' => null, 'fields' => null); $options += $defaults; $paths = self::$_paths; Libraries::paths($paths); $meta = is_array($model) ? $model : $model::meta(); $locate = sprintf('neons.%s', $meta['source']); $data = Libraries::locate($locate, null, array('namespaces' => true)); $files = new Collection(compact('data')); unset($data); $files->each(function ($file) { return str_replace('\\', '/', $file) . 'neon'; }); extract($options); if (isset($conditions['slug'])) { $field = 'slug'; } if (isset($conditions[$meta['key']])) { $field = $meta['key']; } if (!isset($field)) { $field = 'all'; // var_dump($field); // return array(); } $value = $conditions[$field]; switch (true) { case is_string($value): $pattern = sprintf('/%s/', $value); break; case isset($value['like']): $pattern = $value['like']; break; } if (isset($pattern)) { $filter = function ($file) use($pattern) { return (bool) preg_match($pattern, $file); }; } if (isset($filter)) { $files = $files->find($filter); } if (isset($order)) { // TODO: add sort } if ($type == 'count') { return count($files); } if ($type == 'list') { // TODO: implement me } if ($type == 'first' && count($files)) { $data = self::file($files->first()); $data[$field] = $value; if ($model === 'radium\\models\\Configurations') { $data['value'] = Neon::encode($data['value']); } return $model::create($data); } // we found one (!) file with name 'all.neon', this is the entire set if ($type == 'all' && count($files) == 1 && substr($files->first(), -strlen('all.neon')) === 'all.neon') { $rows = self::file($files->first()); $data = array(); foreach ($rows as $row) { $data[] = $model::create($row); } if (is_array($model)) { return new Collection(compact('data')); } $model = $meta['class']; return new DocumentSet(compact('data', 'model')); } if ($type == 'all' && count($files)) { $data = array(); foreach ($files as $file) { $current = self::file($file); if (is_array($model)) { $filename = File::name($file); $data[$filename] = $current; continue; } if ($model === 'radium\\models\\Configurations') { $current['value'] = Neon::encode($current['value']); } $data[] = $model::create($current); } if (is_array($model)) { return new Collection(compact('data')); } $model = $meta['class']; return new DocumentSet(compact('data', 'model')); } return false; }