예제 #1
0
 /**
  * fetches items from lists
  *
  * {{{
  *    Lists::get('inbox'); // return all elements of inbox
  *    Lists::get('inbox', 4); // return 4th element of inbox
  *    Lists::get('inbox', array(2, 5)); // return 2-5th element of inbox
  *    Lists::get('inbox', array(10)); // return all remaining items, starting from 10
  *    Lists::get('inbox', array(10, -1)); // same as above
  * }}}
  *
  * @see li3_redis\storage\Redis::listGet()
  * @see li3_redis\storage\Redis::getKey()
  * @param string $key redis key in which to store the hash
  * @param integer|array $index index to fetch or a range
  * @param string|array $buckets string or array with additional buckets, see Lists::add()
  * @param array $options array with additional options, see Redis::getKey()
  *     - `start`: on empty index, this start will be used
  *     - `end`: on empty index, this end will be used
  *     - `merge`: set to false, if you want your results to be returned on a per-bucket base.
  *     - `collect`: set to false, to return an array, instead of an collection object
  * @return object|array a lithium collection object, containing all values, or array of values
  * @filter
  */
 public static function get($key, $index = array(), $buckets = null, array $options = array())
 {
     $defaults = array('collect' => true, 'merge' => true, 'namespace' => static::$namespace);
     $options += $defaults;
     $params = compact('key', 'index', 'buckets', 'options');
     return static::_filter(__METHOD__, $params, function ($self, $params) {
         extract($params);
         if (is_null($buckets)) {
             $data = Redis::listGet($key, $index, $options);
             return $options['collect'] ? new Collection(compact('data')) : $data;
         }
         $data = array();
         $buckets = !is_array($buckets) ? array($buckets) : $buckets;
         foreach ($buckets as $prefix => $val) {
             $options['prefix'] = !is_numeric($prefix) ? Redis::addPrefix($val, $prefix) : $val;
             $row = Redis::listGet($key, $index, $options);
             if (!$options['merge']) {
                 $data[$options['prefix']] = $row;
             } else {
                 $data = array_merge($data, (array) $row);
             }
         }
         return $options['collect'] ? new Collection(compact('data')) : $data;
     });
 }