Ejemplo n.º 1
0
 public function __call($method, $arguments)
 {
     switch ($method) {
         case in_array($method, static::$retrieve):
             $params = array_merge(static::$options, $this->params);
             $this->params = array();
             switch ($method) {
                 case 'all':
                 case 'pick':
                     $limit = array_shift($arguments) ?: ($method === 'all' ? 0 : 1);
                     if ($limit > 1) {
                         $params['limit'] = $limit;
                     }
                     $result = $this->select($params['select'] ?: '*', $params['where'], $params);
                     return $limit != 1 ? $result->fetch_all() : $result->fetch();
                 case 'each':
                     @(list($lambda) = $arguments);
                     if ($lambda instanceof \Closure) {
                         $result = $this->select($params['select'] ?: '*', $params['where'], $params);
                         while ($row = $result->fetch()) {
                             $lambda($row);
                         }
                         return;
                     }
                 case 'count':
                     return (int) $this->select('COUNT(*)', $params['where'], $params)->result();
                 default:
                     throw new \Exception("Invalid parameters on '{$method}()'");
             }
         case in_array($method, static::$chained):
             if (sizeof($arguments) === 0) {
                 throw new \Exception("Missing arguments for '{$method}()'");
             } elseif (isset($this->params[$method])) {
                 array_unshift($arguments, $this->params[$method]);
             }
             @(list($first) = $arguments);
             $method = str_replace('get', 'select', $method);
             $this->params[$method] = $first;
             return $this;
         case 'index':
             @(list($name, $unique) = $arguments);
             return $this->add_index("{$this}_{$this->offset}_{$name}_idx", array($this->offset), !!$unique);
         case 'unindex':
             @(list($name) = $arguments);
             return $this->remove_index("{$this}_{$this->offset}_{$name}_idx", (string) $this);
         default:
             return parent::__call($method, $arguments);
     }
 }