Exemple #1
0
 public function __construct($db)
 {
     $this->orm = $db;
     $this->db = $db->db();
     $this->table = $db->table();
     $dir = Config::get('dir.blizz.store', session_save_path());
     if (!is_dir($dir)) {
         File::mkdir($dir);
     }
     $dir .= DS . Inflector::urlize(Inflector::uncamelize($this->db));
     if (!is_dir($dir)) {
         File::mkdir($dir);
     }
     $this->dir = $dir . DS . Inflector::urlize(Inflector::uncamelize($this->table));
     if (!is_dir($this->dir)) {
         File::mkdir($this->dir);
     }
     $file = $this->dir . DS . 'data.db';
     $new = false;
     if (!is_file($file)) {
         File::create($file);
         $new = true;
         File::put($this->dir . DS . 'age.blizz', '');
     }
     $link = new SQLite3($file);
     Now::set("blizz.link.{$this->db}.{$this->table}", $link);
     if ($new) {
         $this->init();
     }
 }
Exemple #2
0
 public function __call($event, $args)
 {
     if (substr($event, 0, 3) == 'get' && strlen($event) > 3) {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($event, 3)));
         $key = Inflector::lower($uncamelizeMethod);
         return $this->get($key);
     } elseif (substr($event, 0, 3) == 'set' && strlen($event) > 3) {
         $value = Arrays::first($args);
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($event, 3)));
         $key = Inflector::lower($uncamelizeMethod);
         return $this->set($key, $value);
     }
     if (true === $this->__has($event)) {
         array_push($args, $this);
         return $this->__fire($event, $args);
     } else {
         if (method_exists($this, $event)) {
             throw new Exception("The method {$event} is a native class' method. Please choose an other name.");
         }
         $value = Arrays::first($args);
         if ($value instanceof Closure) {
             $eventable = $this->__event($event, $value);
         } else {
             $set = function () use($value) {
                 return $value;
             };
             $eventable = $this->__event($event, $set);
         }
         return $this;
     }
 }
Exemple #3
0
 public function __call($func, $argv)
 {
     if (substr($func, 0, 3) == 'get' && strlen($func) > 3) {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3)));
         $var = Inflector::lower($uncamelizeMethod);
         if (isset($this->{$var})) {
             $this->clean();
             return $this->{$var};
         } else {
             return null;
         }
     } elseif (substr($func, 0, 3) == 'set' && strlen($func) > 3) {
         $value = $argv[0];
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3)));
         $var = Inflector::lower($uncamelizeMethod);
         $this->{$var} = $value;
         return $this->save();
     } elseif (substr($func, 0, 6) == 'forget' && strlen($func) > 6) {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 6)));
         $var = Inflector::lower($uncamelizeMethod);
         $this->erase($var);
         return $this->save();
     }
     $id = sha1($func);
     if (isset($this->{$id})) {
         if ($this->{$id} instanceof \Closure) {
             return call_user_func_array($this->{$id}, $argv);
         }
     }
     if (!is_callable($func) || substr($func, 0, 3) !== 'set' || substr($func, 0, 3) !== 'get') {
         throw new \BadMethodCallException(__CLASS__ . ' => ' . $func);
     }
 }
Exemple #4
0
 public function __call($event, $args)
 {
     if (substr($event, 0, 3) == 'get') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($event, 3)));
         $key = Inflector::lower($uncamelizeMethod);
         return self::get($key);
     } elseif (substr($event, 0, 3) == 'set') {
         $value = Arrays::first($args);
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($event, 3)));
         $key = Inflector::lower($uncamelizeMethod);
         return self::set($key, $value);
     }
     if (true === self::__has($event)) {
         return self::__fire($event, $args);
     } else {
         $value = Arrays::first($args);
         if ($value instanceof Closure) {
             $eventable = self::__event($event, $value);
         } else {
             $set = function () use($value) {
                 return $value;
             };
             $eventable = self::__event($event, $set);
         }
     }
 }
Exemple #5
0
function run($lib, $args = null)
{
    $lib = Inflector::lower(Inflector::uncamelize($lib));
    $script = str_replace('_', DS, $lib) . '.php';
    if (fnmatch('*_*', $lib)) {
        $class = 'Thin\\' . str_replace('_', '\\', $lib);
        $tab = explode('\\', $class);
        $first = $tab[1];
        $class = str_replace('Thin\\' . $first, 'Thin\\' . ucfirst($first) . 'Libs', $class);
        if (count($tab) > 2) {
            for ($i = 2; $i < count($tab); $i++) {
                $seg = trim($tab[$i]);
                $class = str_replace('\\' . $seg, '\\' . ucfirst($seg), $class);
            }
        }
    } else {
        $class = 'Thin\\' . ucfirst($lib) . 'Libs';
    }
    $file = __DIR__ . DS . 'libs' . DS . $script;
    if (is_file($file)) {
        require_once $file;
        if (empty($args)) {
            return new $class();
        } else {
            if (!is_array($args)) {
                if (is_string($args)) {
                    if (fnmatch('*,*', $args)) {
                        $args = explode(',', str_replace(', ', ',', $args));
                    } else {
                        $args = [$args];
                    }
                } else {
                    $args = [$args];
                }
            }
            $methods = get_class_methods($class);
            if (in_array('instance', $methods)) {
                return call_user_func_array([$class, 'instance'], $args);
            } else {
                return construct($class, $args);
            }
        }
    }
    if (class_exists('Thin\\' . $lib)) {
        $c = 'Thin\\' . $lib;
        return new $c();
    }
    if (class_exists($lib)) {
        return new $lib();
    }
    throw new Exception("The library {$class} does not exist.");
}
Exemple #6
0
 public static function __callStatic($method, $args)
 {
     $db = Inflector::uncamelize($method);
     if (fnmatch('*_*', $db)) {
         list($database, $table) = explode('_', $db, 2);
     } else {
         $database = SITE_NAME;
         $table = $db;
     }
     if (empty($args)) {
         return Db::instance($database, $table);
     } elseif (count($args) == 1) {
         $id = current($args);
         if (is_numeric($id)) {
             return Db::instance($database, $table)->find((int) $id);
         }
     }
 }
Exemple #7
0
 public static function __callStatic($method, $args)
 {
     $db = Inflector::uncamelize($method);
     if (fnmatch('*_*', $db)) {
         list($database, $table) = explode('_', $db, 2);
     } else {
         $database = SITE_NAME;
         $table = $db;
     }
     if (empty($args)) {
         return new Redis($database, $table);
     } elseif (count($args) == 1) {
         $id = Arrays::first($args);
         if (is_numeric($id)) {
             return with(new Redis($database, $table))->find($id);
         }
     }
 }
Exemple #8
0
 public function __call($func, $args)
 {
     if (substr($func, 0, strlen('get')) == 'get') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('get'))));
         $field = Inflector::lower($uncamelizeMethod);
         $default = count($args) == 1 ? Arrays::first($args) : null;
         $res = isAke($this->_data, $field, false);
         if (false !== $res) {
             return $res;
         } else {
             $resFk = isAke($this->_data, $field . '_id', false);
             if (false !== $resFk) {
                 $db = Db::instance($this->_db->db, $field);
                 $object = count($args) == 1 ? $args[0] : false;
                 if (!is_bool($object)) {
                     $object = false;
                 }
                 return $db->find($resFk, $object);
             } else {
                 if ($field[strlen($field) - 1] == 's' && isset($this->_data['id']) && $field[0] != '_') {
                     $db = Db::instance($this->_db->db, substr($field, 0, -1));
                     $object = count($args) == 1 ? $args[0] : false;
                     if (!is_bool($object)) {
                         $object = false;
                     }
                     $hasPivot = $this->hasPivot($db);
                     if (true === $hasPivot) {
                         $model = $db->model();
                         $pivots = $this->pivots($model)->exec();
                         $ids = [];
                         if (!empty($pivots)) {
                             foreach ($pivots as $pivot) {
                                 $id = isAke($pivot, substr($field, 0, -1) . '_id', false);
                                 if (false !== $id) {
                                     array_push($ids, $id);
                                 }
                             }
                             if (!empty($ids)) {
                                 return $db->where(['id', 'IN', implode(',', $ids)])->exec($object);
                             } else {
                                 return [];
                             }
                         }
                     } else {
                         $idField = $this->_db->table . '_id';
                         return $db->where([$idField, '=', $this->_data['id']])->exec($object);
                     }
                 } else {
                     return $default;
                 }
             }
         }
     } elseif (substr($func, 0, strlen('has')) == 'has') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('has'))));
         $field = Inflector::lower($uncamelizeMethod);
         $res = isAke($this->_data, $field, false);
         if (false !== $res) {
             return true;
         } else {
             $resFk = isAke($this->_data, $field . '_id', false);
             if (false !== $resFk) {
                 return true;
             } else {
                 if ($field[strlen($field) - 1] == 's' && isset($this->_data['id']) && $field[0] != '_') {
                     $db = Db::instance($this->_db->db, substr($field, 0, -1));
                     $hasPivot = $this->hasPivot($db);
                     if (true === $hasPivot) {
                         $model = $db->model();
                         $pivots = $this->pivots($model)->exec();
                         $ids = [];
                         if (!empty($pivots)) {
                             foreach ($pivots as $pivot) {
                                 $id = isAke($pivot, substr($field, 0, -1) . '_id', false);
                                 if (false !== $id) {
                                     array_push($ids, $id);
                                 }
                             }
                             return !empty($ids) ? true : false;
                         }
                     } else {
                         $idField = $this->_db->table . '_id';
                         $count = $db->where([$idField, '=', $this->_data['id']])->count();
                         return $count > 0 ? true : false;
                     }
                 }
             }
         }
         return false;
     } elseif (substr($func, 0, strlen('set')) == 'set') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('set'))));
         $field = Inflector::lower($uncamelizeMethod);
         if (!empty($args)) {
             $val = Arrays::first($args);
         } else {
             $val = null;
         }
         if (is_object($val)) {
             $val = (int) $val->id;
         }
         if (fnmatch('*_id', $field)) {
             if (is_numeric($val)) {
                 $val = (int) $val;
             }
         }
         $this->_data[$field] = $val;
         $autosave = isAke($this->_data, 'autosave', false);
         return !$autosave ? $this : $this->save();
     } else {
         $cb = isAke($this->_events, $func, false);
         if (false !== $cb) {
             if ($cb instanceof Closure) {
                 return call_user_func_array($cb, $args);
             }
         } else {
             if ($func[strlen($func) - 1] == 's' && isset($this->_data['id']) && $func[0] != '_') {
                 $db = Db::instance($this->_db->db, substr($func, 0, -1));
                 $object = count($args) == 1 ? $args[0] : false;
                 if (!is_bool($object)) {
                     $object = false;
                 }
                 $hasPivot = $this->hasPivot($db);
                 if (true === $hasPivot) {
                     $model = $db->model();
                     $pivots = $this->pivots($model)->exec();
                     $ids = [];
                     if (!empty($pivots)) {
                         foreach ($pivots as $pivot) {
                             $id = isAke($pivot, substr($func, 0, -1) . '_id', false);
                             if (false !== $id) {
                                 array_push($ids, $id);
                             }
                         }
                         if (!empty($ids)) {
                             return $db->where(['id', 'IN', implode(',', $ids)])->exec($object);
                         } else {
                             return [];
                         }
                     }
                 } else {
                     $idField = $this->_db->table . '_id';
                     return $db->where([$idField, '=', $this->_data['id']])->exec($object);
                 }
             } else {
                 if (count($args)) {
                     $object = count($args) == 1 ? $args[0] : false;
                     $db = Db::instance($this->_db->db, $func);
                     $field = $func . '_id';
                     if (is_bool($object) && isset($this->_data[$field])) {
                         return $db->find($value, $object);
                     } elseif (is_object($object)) {
                         $this->{$field} = (int) $object->id;
                         return $this;
                     }
                 }
                 $auth = ['_hooks'];
                 if (Arrays::in($func, $auth)) {
                     return true;
                 }
                 throw new Exception("{$func} is not a model function of {$this->_db}.");
             }
         }
     }
 }
Exemple #9
0
 public function __call($method, $parameters)
 {
     if (substr($method, 0, 6) == 'findBy') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 6)));
         $field = Inflector::lower($uncamelizeMethod);
         $value = Arrays::first($parameters);
         return $this->findBy($field, $value);
     } elseif (substr($method, 0, 9) == 'findOneBy') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 9)));
         $field = Inflector::lower($uncamelizeMethod);
         $value = Arrays::first($parameters);
         return $this->findBy($field, $value, true);
     }
 }
Exemple #10
0
 public function __call($m, $a)
 {
     if ('or' == $m) {
         if (empty($this->wheres)) {
             return call_user_func_array([$this, 'where'], $a);
         }
         $this->check();
         $a[] = 'or';
         $this->wheres[] = $a;
     }
     if (fnmatch('findBy*', $m) && strlen($m) > 'findBy') {
         $field = Inflector::uncamelize(Inflector::lower(str_replace('findBy', '', $m)));
         if (strlen($field) > 0 && !empty($a)) {
             return $this->where([$field, '=', current($a)])->run();
         }
     }
     if (fnmatch('countBy*', $m) && strlen($m) > 'countBy') {
         $field = Inflector::uncamelize(Inflector::lower(str_replace('countBy', '', $m)));
         if (strlen($field) > 0 && !empty($a)) {
             return $this->where([$field, '=', current($a)])->count();
         }
     }
     if (fnmatch('groupBy*', $m) && strlen($m) > 'groupBy') {
         $field = Inflector::uncamelize(Inflector::lower(str_replace('groupBy', '', $m)));
         if (strlen($field) > 0) {
             return $this->groupBy($field);
         }
     }
     if (fnmatch('findOneBy*', $m) && strlen($m) > 'findOneBy') {
         $field = Inflector::uncamelize(Inflector::lower(str_replace('findOneBy', '', $m)));
         if (strlen($field) > 0 && !empty($a)) {
             $model = false;
             if (count($a) == 2) {
                 if (true === end($a)) {
                     $model = true;
                 }
             }
             return $this->where([$field, '=', current($a)])->first($model);
         }
     }
     if (fnmatch('firstBy*', $m) && strlen($m) > 'firstBy') {
         $field = Inflector::uncamelize(Inflector::lower(str_replace('firstBy', '', $m)));
         if (strlen($field) > 0 && !empty($a)) {
             $model = false;
             if (count($a) == 2) {
                 if (true === end($a)) {
                     $model = true;
                 }
             }
             return $this->where([$field, '=', current($a)])->first($model);
         }
     }
     if (fnmatch('lastBy*', $m) && strlen($m) > 'lastBy') {
         $field = Inflector::uncamelize(Inflector::lower(str_replace('lastBy', '', $m)));
         if (strlen($field) > 0 && !empty($a)) {
             $model = false;
             if (count($a) == 2) {
                 if (true === end($a)) {
                     $model = true;
                 }
             }
             return $this->where([$field, '=', current($a)])->last($model);
         }
     }
     return $this;
 }
Exemple #11
0
 public function __call($m, $a)
 {
     $k = Inflector::uncamelize(substr($m, 3));
     if (fnmatch('get*', $m)) {
         $default = empty($a) ? null : current($a);
         return $this->get($k, $default);
     } elseif (fnmatch('set*', $m)) {
         return $this->set($k, current($a));
     } elseif (fnmatch('has*', $m)) {
         return $this->has($k);
     } else {
         $v = !empty($a) ? current($a) : true;
         return $this->set($m, $v);
     }
 }
Exemple #12
0
 public function __call($method, $parameters)
 {
     if (substr($method, 0, 6) == 'findBy') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 6)));
         $field = Inflector::lower($uncamelizeMethod);
         $value = Arrays::first($parameters);
         return $this->findBy($field, $value);
     } elseif (substr($method, 0, strlen('findObjectsBy')) == 'findObjectsBy') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, strlen('findObjectsBy'))));
         $field = Inflector::lower($uncamelizeMethod);
         $value = Arrays::first($parameters);
         return $this->findBy($field, $value, false, true);
     } elseif (substr($method, 0, 9) == 'findOneBy') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 9)));
         $field = Inflector::lower($uncamelizeMethod);
         $value = Arrays::first($parameters);
         return $this->findBy($field, $value, true);
     } elseif (substr($method, 0, 15) == 'findOneObjectBy') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 15)));
         $field = Inflector::lower($uncamelizeMethod);
         $value = Arrays::first($parameters);
         return $this->findBy($field, $value, true, true);
     } else {
         $settings = isAke(self::$configs, $this->entity);
         $event = isAke($settings, $method);
         if (!empty($event)) {
             if (is_callable($event)) {
                 if (version_compare(PHP_VERSION, '5.4.0', ">=")) {
                     $event = $event->bindTo($this);
                 }
                 return call_user_func_array($event, $parameters);
             }
         } else {
             if (count($parameters) == 1) {
                 $c = $this;
                 $cb = function () use($c) {
                     return $c;
                 };
                 return self::configs($this->entity, $method, Arrays::first($parameters), $cb);
             } else {
                 return $this->__fire($method, $parameters);
             }
         }
     }
 }
Exemple #13
0
 /**
  *
  * @method __call
  *
  * @param  [type]
  * @param  [type]
  *
  * @return [type]
  */
 public function __call($m, $a)
 {
     if (fnmatch('get*', $m)) {
         $key = Inflector::uncamelize(substr($m, 3));
         $default = empty($a) ? null : current($a);
         return $this->get($key, $default);
     } elseif (fnmatch('set*', $m)) {
         $key = Inflector::uncamelize(substr($m, 3));
         return $this->set($key, current($a));
     } elseif (fnmatch('has*', $m)) {
         $key = Inflector::uncamelize(substr($m, 3));
         return $this->has($key);
     } elseif (fnmatch('del*', $m)) {
         $key = Inflector::uncamelize(substr($m, 3));
         return $this->delete($key);
     } else {
         $closure = $this->get($m);
         if (is_string($closure) && fnmatch('*::*', $closure)) {
             list($c, $f) = explode('::', $closure, 2);
             try {
                 $i = lib('caller')->make($c);
                 return call_user_func_array([$i, $f], $a);
             } catch (\Exception $e) {
                 $default = empty($a) ? null : current($a);
                 return empty($closure) ? $default : $closure;
             }
         } else {
             if (is_callable($closure)) {
                 return call_user_func_array($closure, $a);
             }
             if (!empty($a) && empty($closure)) {
                 if (count($a) == 1) {
                     return $this->set($m, current($a));
                 }
             }
             $default = empty($a) ? null : current($a);
             return empty($closure) ? $default : $closure;
         }
     }
 }
Exemple #14
0
 public function __call($fn, $args)
 {
     $fields = array_keys($this->map['fields']);
     $method = substr($fn, 0, 2);
     $object = lcfirst(substr($fn, 2));
     if ('is' === $method && strlen($fn) > 2) {
         $field = Inflector::uncamelize($object);
         if (!Arrays::in($field, $fields)) {
             $field = $field . '_id';
             $model = Arrays::first($args);
             if ($model instanceof Container) {
                 $idFk = $model->id();
             } else {
                 $idFk = $model;
             }
             return $this->where("{$field} = {$idFk}");
         } else {
             return $this->where($field . ' = ' . Arrays::first($args));
         }
     }
     $method = substr($fn, 0, 3);
     $object = lcfirst(substr($fn, 3));
     if (strlen($fn) > 3) {
         if ('get' === $method) {
             $object = Inflector::uncamelize($object);
             return isset($this->{$object}) ? $this->{$object} : null;
         } else {
             if ('set' === $method) {
                 $object = Inflector::uncamelize($object);
                 $this->{$object} = Arrays::first($args);
                 return $this;
             } else {
                 if ('has' === $method) {
                     $object = Inflector::uncamelize($object);
                     return isset($this->{$object});
                 }
             }
         }
     }
     $method = substr($fn, 0, 4);
     $object = lcfirst(substr($fn, 4));
     if ('orIs' === $method && strlen($fn) > 4) {
         $field = Inflector::uncamelize($object);
         if (!Arrays::in($field, $fields)) {
             $field = $field . '_id';
             $model = Arrays::first($args);
             if ($model instanceof Container) {
                 $idFk = $model->id();
             } else {
                 $idFk = $model;
             }
             return $this->where("{$field} = {$idFk}", 'OR');
         } else {
             return $this->where($field . ' = ' . Arrays::first($args), 'OR');
         }
     } elseif ('like' === $method && strlen($fn) > 4) {
         $field = Inflector::uncamelize($object);
         $op = count($args) == 2 ? Arrays::last($args) : 'AND';
         return $this->like($field, Arrays::first($args), $op);
     }
     $method = substr($fn, 0, 5);
     $object = lcfirst(substr($fn, 5));
     if (strlen($fn) > 5) {
         if ('where' == $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$field} = {$idFk}");
             } else {
                 return $this->where($field . ' ' . Arrays::first($args));
             }
         } elseif ('xorIs' === $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$field} = {$idFk}", 'XOR');
             } else {
                 return $this->where($field . ' = ' . Arrays::first($args), 'XOR');
             }
         } elseif ('andIs' === $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$field} = {$idFk}");
             } else {
                 return $this->where($field . ' = ' . Arrays::first($args));
             }
         }
     }
     $method = substr($fn, 0, 6);
     $object = Inflector::uncamelize(lcfirst(substr($fn, 6)));
     if (strlen($fn) > 6) {
         if ('findBy' == $method) {
             return $this->findBy($object, Arrays::first($args));
         }
     }
     $method = substr($fn, 0, 7);
     $object = lcfirst(substr($fn, 7));
     if (strlen($fn) > 7) {
         if ('orWhere' == $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$field} = {$idFk}", 'OR');
             } else {
                 return $this->where($field . ' ' . Arrays::first($args), 'OR');
             }
         } elseif ('orderBy' == $method) {
             $object = Inflector::uncamelize(lcfirst(substr($fn, 7)));
             if ($object == 'id') {
                 $object = $this->pk();
             }
             if (!Arrays::in($object, $fields)) {
                 $object = Arrays::in($object . '_id', $fields) ? $object . '_id' : $object;
             }
             $direction = count($args) ? Arrays::first($args) : 'ASC';
             return $this->order($object, $direction);
         } elseif ('groupBy' == $method) {
             $object = Inflector::uncamelize(lcfirst(substr($fn, 7)));
             if ($object == 'id') {
                 $object = $this->pk();
             }
             if (!Arrays::in($object, $fields)) {
                 $object = Arrays::in($object . '_id', $fields) ? $object . '_id' : $object;
             }
             return $this->groupBy($object);
         }
     }
     $method = substr($fn, 0, 9);
     $object = Inflector::uncamelize(lcfirst(substr($fn, 9)));
     if (strlen($fn) > 9) {
         if ('findOneBy' == $method) {
             return $this->findOneBy($object, Arrays::first($args));
         }
     }
     $method = substr($fn, 0, 13);
     $object = Inflector::uncamelize(lcfirst(substr($fn, 13)));
     if (strlen($fn) > 13) {
         if ('findObjectsBy' == $method) {
             return $this->findBy($object, Arrays::first($args), true);
         }
     }
     $method = substr($fn, 0, 15);
     $object = Inflector::uncamelize(lcfirst(substr($fn, 15)));
     if (strlen($fn) > 15) {
         if ('findOneObjectBy' == $method) {
             return $this->findOneBy($object, Arrays::first($args), true);
         }
     }
     $method = substr($fn, 0, 8);
     $object = lcfirst(substr($fn, 8));
     if (strlen($fn) > 8) {
         if ('xorWhere' == $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$field} = {$idFk}", 'XOR');
             } else {
                 return $this->where($field . ' ' . Arrays::first($args), 'XOR');
             }
         } elseif ('andWhere' == $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$field} = {$idFk}");
             } else {
                 return $this->where($field . ' ' . Arrays::first($args));
             }
         }
     } else {
         $field = $fn;
         $fieldFk = $fn . '_id';
         $op = count($args) == 2 ? Inflector::upper(Arrays::last($args)) : 'AND';
         if (Arrays::in($field, $fields)) {
             return $this->where($field . ' = ' . Arrays::first($args), $op);
         } else {
             if (Arrays::in($fieldFk, $fields)) {
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$fieldFk} = {$idFk}", $op);
             }
         }
     }
     throw new Exception("Method '{$fn}' is unknown.");
 }
Exemple #15
0
 public function __call($m, $a)
 {
     $c = Now::get(str_replace(DS, '.', $this->_db->dir) . '.' . $m);
     if ($c && is_callable($c)) {
         $a[] = $this;
         return call_user_func_array($c, $a);
     } else {
         if (fnmatch('set*', $m)) {
             $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('set'))));
             $field = Inflector::lower($uncamelizeMethod);
             if (!empty($a)) {
                 $val = current($a);
             } else {
                 $val = null;
             }
             $this->{$field} = $val;
             return $this;
         } elseif (fnmatch('get*', $m)) {
             $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('set'))));
             $field = Inflector::lower($uncamelizeMethod);
             $default = count($a) == 1 ? current($a) : null;
             return isset($this->{$field}) ? $this->{$field} : $default;
         } else {
             return call_user_func_array([$this->_db], $a);
         }
     }
 }
Exemple #16
0
 public function __call($fn, $args)
 {
     $fields = $this->fields();
     $method = substr($fn, 0, 2);
     $object = lcfirst(substr($fn, 2));
     if ('is' == $method && strlen($fn) > 2) {
         $field = Inflector::uncamelize($object);
         if (!Arrays::in($field, $fields)) {
             $field = $field . '_id';
             $model = Arrays::first($args);
             if ($model instanceof Container) {
                 $idFk = $model->id;
             } else {
                 $idFk = $model;
             }
             return $this->where([$field, '=', $idFk]);
         } else {
             return $this->where([$field, '=', Arrays::first($args)]);
         }
     }
     $method = substr($fn, 0, 4);
     $object = lcfirst(substr($fn, 4));
     if ('orIs' == $method && strlen($fn) > 4) {
         $field = Inflector::uncamelize($object);
         if (!Arrays::in($field, $fields)) {
             $field = $field . '_id';
             $model = Arrays::first($args);
             if ($model instanceof Container) {
                 $idFk = $model->id;
             } else {
                 $idFk = $model;
             }
             return $this->where([$field, '=', $idFk], 'OR');
         } else {
             return $this->where([$field, '=', Arrays::first($args)], 'OR');
         }
     } elseif ('like' == $method && strlen($fn) > 4) {
         $field = Inflector::uncamelize($object);
         $op = count($args) == 2 ? Arrays::last($args) : 'AND';
         return $this->like($field, Arrays::first($args), $op);
     }
     $method = substr($fn, 0, 5);
     $object = lcfirst(substr($fn, 5));
     if (strlen($fn) > 5) {
         if ('where' == $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id;
                 } else {
                     $idFk = $model;
                 }
                 return $this->where([$field, '=', $idFk]);
             } else {
                 return $this->where([$field, '=', Arrays::first($args)]);
             }
         } elseif ('xorIs' == $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id;
                 } else {
                     $idFk = $model;
                 }
                 return $this->where([$field, '=', $idFk], 'XOR');
             } else {
                 return $this->where([$field, '=', Arrays::first($args)], 'XOR');
             }
         } elseif ('andIs' == $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id;
                 } else {
                     $idFk = $model;
                 }
                 return $this->where([$field, '=', $idFk]);
             } else {
                 return $this->where([$field, '=', Arrays::first($args)]);
             }
         }
     }
     $method = substr($fn, 0, 6);
     $object = Inflector::uncamelize(lcfirst(substr($fn, 6)));
     if (strlen($fn) > 6) {
         if ('findBy' == $method) {
             return $this->findBy($object, Arrays::first($args));
         }
     }
     $method = substr($fn, 0, 7);
     $object = lcfirst(substr($fn, 7));
     if (strlen($fn) > 7) {
         if ('orWhere' == $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id;
                 } else {
                     $idFk = $model;
                 }
                 return $this->where([$field, '=', $idFk], 'OR');
             } else {
                 return $this->where([$field, '=', Arrays::first($args)], 'OR');
             }
         } elseif ('orderBy' == $method) {
             $object = Inflector::uncamelize(lcfirst(substr($fn, 7)));
             if (!Arrays::in($object, $fields) && 'id' != $object) {
                 $object = Arrays::in($object . '_id', $fields) ? $object . '_id' : $object;
             }
             $direction = !empty($args) ? Arrays::first($args) : 'ASC';
             return $this->order($object, $direction);
         } elseif ('groupBy' == $method) {
             $object = Inflector::uncamelize(lcfirst(substr($fn, 7)));
             if ($object == 'id') {
                 $object = $this->pk();
             }
             if (!Arrays::in($object, $fields)) {
                 $object = Arrays::in($object . '_id', $fields) ? $object . '_id' : $object;
             }
             return $this->groupBy($object);
         }
     }
     $method = substr($fn, 0, 9);
     $object = Inflector::uncamelize(lcfirst(substr($fn, 9)));
     if (strlen($fn) > 9) {
         if ('findOneBy' == $method) {
             return $this->findOneBy($object, Arrays::first($args));
         }
     }
     $method = substr($fn, 0, strlen('findLastBy'));
     $object = Inflector::uncamelize(lcfirst(substr($fn, strlen('findLastBy'))));
     if (strlen($fn) > strlen('findLastBy')) {
         if ('findLastBy' == $method) {
             $obj = count($args) == 2 ? $args[1] : true;
             if (!is_bool($obj)) {
                 $obj = true;
             }
             return $this->where([$object, '=', Arrays::first($args)])->last($obj);
         }
     }
     $method = substr($fn, 0, 11);
     $object = Inflector::uncamelize(lcfirst(substr($fn, 11)));
     if (strlen($fn) > 11) {
         if ('findFirstBy' == $method) {
             $obj = count($args) == 2 ? $args[1] : true;
             if (!is_bool($obj)) {
                 $obj = true;
             }
             return $this->where([$object, '=', Arrays::first($args)])->first($obj);
         }
     }
     $method = substr($fn, 0, 13);
     $object = Inflector::uncamelize(lcfirst(substr($fn, 13)));
     if (strlen($fn) > 13) {
         if ('findObjectsBy' == $method) {
             return $this->findBy($object, Arrays::first($args), true);
         }
     }
     $method = substr($fn, 0, 15);
     $object = Inflector::uncamelize(lcfirst(substr($fn, 15)));
     if (strlen($fn) > 15) {
         if ('findOneObjectBy' == $method) {
             return $this->findOneBy($object, Arrays::first($args), true);
         }
     }
     $method = substr($fn, 0, 8);
     $object = lcfirst(substr($fn, 8));
     if (strlen($fn) > 8) {
         if ('xorWhere' == $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id;
                 } else {
                     $idFk = $model;
                 }
                 return $this->where([$field, '=', $idFk], 'XOR');
             } else {
                 return $this->where([$field, '=', Arrays::first($args)], 'XOR');
             }
         } elseif ('andWhere' == $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id;
                 } else {
                     $idFk = $model;
                 }
                 return $this->where([$field, '=', $idFk]);
             } else {
                 return $this->where([$field, '=', Arrays::first($args)]);
             }
         }
     }
     $field = $fn;
     $fieldFk = $fn . '_id';
     $op = count($args) == 2 ? Inflector::upper(Arrays::last($args)) : 'AND';
     if (Arrays::in($field, $fields)) {
         return $this->where([$field, '=', Arrays::first($args)], $op);
     } else {
         if (Arrays::in($fieldFk, $fields)) {
             $model = Arrays::first($args);
             if ($model instanceof Container) {
                 $idFk = $model->id;
             } else {
                 $idFk = $model;
             }
             return $this->where([$field, '=', $idFk], $op);
         }
     }
     if (!empty($args) && Arrays::first($args) instanceof Closure) {
         $closure = Arrays::first($args);
         array_shift($args);
         return call_user_func_array($closure, $args);
     }
     throw new Exception("Method '{$fn}' is unknown.");
 }
Exemple #17
0
 public function __call($method, $parameters)
 {
     if (substr($method, 0, 6) == 'findBy') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 6)));
         $field = Inflector::lower($uncamelizeMethod);
         $value = Arrays::first($parameters);
         return $this->findBy($field, $value);
     } elseif (substr($method, 0, strlen('findObjectsBy')) == 'findObjectsBy') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, strlen('findObjectsBy'))));
         $field = Inflector::lower($uncamelizeMethod);
         $value = Arrays::first($parameters);
         return $this->findBy($field, $value, false, true);
     } elseif (substr($method, 0, 9) == 'findOneBy') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 9)));
         $field = Inflector::lower($uncamelizeMethod);
         $value = Arrays::first($parameters);
         return $this->findBy($field, $value, true);
     } else {
         $settings = isAke(self::$configs, $this->entity);
         $event = isAke($settings, $method);
         if (!empty($event)) {
             if (is_callable($event)) {
                 if (version_compare(PHP_VERSION, '5.4.0', ">=")) {
                     $event = $event->bindTo($this);
                 }
                 return call_user_func_array($event, $parameters);
             }
         } else {
             throw new Exception("The method '{$method}' is not callable.");
         }
     }
 }
Exemple #18
0
 public function __call($method, $args)
 {
     if (substr($method, 0, strlen('findBy')) == 'findBy') {
         $value = Arrays::first($args);
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 6)));
         $var = Inflector::lower($uncamelizeMethod);
         return $this->findBy($var, $value);
     } elseif (substr($method, 0, strlen('findOneBy')) == 'findOneBy') {
         $value = Arrays::first($args);
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 9)));
         $var = Inflector::lower($uncamelizeMethod);
         return $this->findBy($var, $value, true);
     }
 }
Exemple #19
0
 public function __call($func, $args)
 {
     if (substr($func, 0, strlen('get')) == 'get') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('get'))));
         $field = Inflector::lower($uncamelizeMethod);
         $default = count($args) == 1 ? Arrays::first($args) : null;
         return isAke($this->_data, $field, $default);
     } elseif (substr($func, 0, strlen('set')) == 'set') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('set'))));
         $field = Inflector::lower($uncamelizeMethod);
         if (!empty($args)) {
             $val = Arrays::first($args);
         } else {
             $val = null;
         }
         $this->_data[$field] = $val;
         return $this;
     } else {
         $cb = isake($this->_events, $func, false);
         if (false !== $cb) {
             if ($cb instanceof Closure) {
                 return call_user_func_array($cb, $args);
             }
         }
         dd("{$func} is not a model function of this object.");
     }
 }
Exemple #20
0
 public function __call($func, $args)
 {
     if (substr($func, 0, strlen('get')) == 'get') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('get'))));
         $field = Inflector::lower($uncamelizeMethod);
         $default = count($args) == 1 ? Arrays::first($args) : null;
         $res = isAke($this->_data, $field, false);
         if (false !== $res) {
             return $res;
         } else {
             $resFk = isAke($this->_data, $field . '_id', false);
             if (false !== $resFk) {
                 $db = Db::instance($this->_db->db, $field);
                 $object = count($args) == 1 ? $args[0] : false;
                 if (!is_bool($object)) {
                     $object = false;
                 }
                 return $db->find($resFk, $object);
             } else {
                 if ($field[strlen($field) - 1] == 's' && isset($this->_data[$this->_db->pk()]) && $field[0] != '_') {
                     $db = Db::instance($this->_db->db, substr($field, 0, -1));
                     $object = count($args) == 1 ? $args[0] : false;
                     if (!is_bool($object)) {
                         $object = false;
                     }
                     $idField = $this->_db->table . '_id';
                     return $db->where([$idField, '=', $this->_data[$this->_db->pk()]])->exec($object);
                 } else {
                     return $default;
                 }
             }
         }
     } elseif (substr($func, 0, strlen('has')) == 'has') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('has'))));
         $field = Inflector::lower($uncamelizeMethod);
         $res = isAke($this->_data, $field, false);
         if (false !== $res) {
             return true;
         } else {
             $resFk = isAke($this->_data, $field . '_id', false);
             if (false !== $resFk) {
                 return true;
             } else {
                 if ($field[strlen($field) - 1] == 's' && isset($this->_data[$this->_db->pk()]) && $field[0] != '_') {
                     $db = Db::instance($this->_db->db, substr($field, 0, -1));
                     $idField = $this->_db->table . '_id';
                     $count = $db->where([$idField, '=', $this->_data[$this->_db->pk()]])->count();
                     return $count > 0 ? true : false;
                 }
             }
         }
         return false;
     } elseif (substr($func, 0, strlen('set')) == 'set') {
         $fields = $this->_db->fieldsSave();
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('set'))));
         $field = Inflector::lower($uncamelizeMethod);
         if (!in_array($field, $fields) && $field != $this->_db->pk()) {
             throw new Exception("The field {$field} does not exist in the model.");
         }
         if (!empty($args)) {
             $val = Arrays::first($args);
         } else {
             $val = null;
         }
         if (is_object($val)) {
             $val = $val->id;
         }
         if (fnmatch('*_id', $field)) {
             if (is_numeric($val)) {
                 $val = (int) $val;
             }
         }
         $this->_data[$field] = $val;
         $autosave = isAke($this->_data, 'autosave', false);
         return !$autosave ? $this : $this->save();
     } else {
         $cb = isake($this->_events, $func, false);
         if (false !== $cb) {
             if ($cb instanceof Closure) {
                 return call_user_func_array($cb, $args);
             }
         } else {
             if ($func[strlen($func) - 1] == 's' && isset($this->_data[$this->_db->pk()]) && $func[0] != '_') {
                 $db = Db::instance($this->_db->db, substr($func, 0, -1));
                 $object = count($args) == 1 ? $args[0] : false;
                 if (!is_bool($object)) {
                     $object = false;
                 }
                 $idField = $this->_db->table . '_id';
                 return $db->where([$idField, '=', $this->_data[$this->_db->pk()]])->exec($object);
             } else {
                 $auth = ['checkIndices', '_hooks'];
                 if (Arrays::in($func, $auth)) {
                     return true;
                 }
                 throw new Exception("{$func} is not a model function of {$this->_db}.");
             }
         }
     }
 }
Exemple #21
0
 public static function __callStatic($fn, $args)
 {
     $method = Inflector::uncamelize($fn);
     $tab = explode('_', $method);
     $table = array_shift($tab);
     $function = implode('_', $tab);
     $function = lcfirst(Inflector::camelize($function));
     $instance = self::instance(SITE_NAME, $table);
     return call_user_func_array([$instance, $function], $args);
 }
Exemple #22
0
 public function __call($func, $argv)
 {
     if (substr($func, 0, 3) == 'get') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3)));
         $var = Inflector::lower($uncamelizeMethod);
         if (isset($this->{$var})) {
             return $this->{$var};
         } else {
             return null;
         }
     } elseif (substr($func, 0, 3) == 'set') {
         $value = Arrays::first($argv);
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3)));
         $var = Inflector::lower($uncamelizeMethod);
         $this->{$var} = $value;
         return $this;
     }
     if (!is_callable($func) || substr($func, 0, 3) !== 'set' || substr($func, 0, 3) !== 'get') {
         throw new \BadMethodCallException(__CLASS__ . ' => ' . $func);
     }
 }
Exemple #23
0
 public function __call($m, $a)
 {
     if (fnmatch('set*', $m)) {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('set'))));
         $field = Inflector::lower($uncamelizeMethod);
         if (!empty($a)) {
             $val = current($a);
         } else {
             $val = null;
         }
         $this->{$field} = $val;
         return $this;
     } elseif (fnmatch('get*', $m)) {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('set'))));
         $field = Inflector::lower($uncamelizeMethod);
         $default = count($a) == 1 ? current($a) : null;
         return isset($this->{$field}) ? $this->{$field} : $default;
     } else {
         return call_user_func_array([$this->_db], $a);
     }
 }
Exemple #24
0
 public function __call($func, $args)
 {
     if (substr($func, 0, strlen('get')) == 'get') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('get'))));
         $field = Inflector::lower($uncamelizeMethod);
         $method = lcfirst(Inflector::camelize('get_' . $field . '_attribute'));
         $methods = get_class_methods($this);
         if (in_array($method, $methods)) {
             return $this->{$method}();
         }
         $default = count($args) == 1 ? current($args) : null;
         $res = isAke($this->_data, $field, false);
         if (false !== $res) {
             return $res;
         } else {
             $resFk = isAke($this->_data, $field . '_id', false);
             if (false !== $resFk) {
                 $db = Db::instance($this->_db->db, $field);
                 $object = count($args) == 1 ? $args[0] : false;
                 if (!is_bool($object)) {
                     $object = false;
                 }
                 return $db->find($resFk, $object);
             } else {
                 if ($field[strlen($field) - 1] == 's' && isset($this->_data['id']) && $field[0] != '_') {
                     $db = Db::instance($this->_db->db, substr($field, 0, -1));
                     $object = count($args) == 1 ? $args[0] : false;
                     if (!is_bool($object)) {
                         $object = false;
                     }
                     $hasPivot = $this->hasPivot($db);
                     if (true === $hasPivot) {
                         $model = $db->model();
                         $pivots = $this->pivots($model)->get();
                         $ids = [];
                         if ($pivots->count() > 0) {
                             foreach ($pivots as $pivot) {
                                 $id = isAke($pivot, substr($field, 0, -1) . '_id', false);
                                 if (false !== $id) {
                                     array_push($ids, $id);
                                 }
                             }
                             if (!empty($ids)) {
                                 return $db->where(['id', 'IN', implode(',', $ids)])->get(null, $object);
                             } else {
                                 return [];
                             }
                         }
                     } else {
                         $idField = $this->_db->table . '_id';
                         return $db->where([$idField, '=', $this->_data['id']])->get(null, $object);
                     }
                 } else {
                     return $default;
                 }
             }
         }
     } elseif (substr($func, 0, strlen('has')) == 'has' && strlen($func) > strlen('has')) {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('has'))));
         $field = Inflector::lower($uncamelizeMethod);
         $res = isAke($this->_data, $field, false);
         if (false !== $res) {
             return true;
         } else {
             $resFk = isAke($this->_data, $field . '_id', false);
             if (false !== $resFk) {
                 return true;
             } else {
                 if ($field[strlen($field) - 1] == 's' && isset($this->_data['id']) && $field[0] != '_') {
                     $db = Db::instance($this->_db->db, substr($field, 0, -1));
                     $hasPivot = $this->hasPivot($db);
                     if (true === $hasPivot) {
                         $model = $db->model();
                         $pivots = $this->pivots($model)->get();
                         $ids = [];
                         if ($pivots->count() > 0) {
                             foreach ($pivots as $pivot) {
                                 $id = isAke($pivot, substr($field, 0, -1) . '_id', false);
                                 if (false !== $id) {
                                     return true;
                                 }
                             }
                             return false;
                         }
                     } else {
                         $idField = $this->_db->table . '_id';
                         $count = $db->where([$idField, '=', $this->_data['id']])->count();
                         return $count > 0 ? true : false;
                     }
                 }
             }
         }
         return false;
     } elseif (substr($func, 0, strlen('belongsTo')) == 'belongsTo' && strlen($func) > strlen('belongsTo')) {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('set'))));
         $field = Inflector::lower($uncamelizeMethod);
         $fk = current($args);
         if (is_object($fk)) {
             $val = isAke($this->_data, $field . '_id', false);
             $fkId = isset($fk->id) ? $fk->id : false;
             if ($val && $fkId) {
                 return intval($val) == intval($fkId);
             }
         }
         return false;
     } elseif (substr($func, 0, strlen('belongsToMany')) == 'belongsToMany' && strlen($func) > strlen('belongsToMany')) {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('set'))));
         $field = Inflector::lower($uncamelizeMethod);
         if (is_object($fk)) {
             return $this->belongsToMany($field);
         }
         return false;
     } elseif (substr($func, 0, strlen('set')) == 'set') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('set'))));
         $field = Inflector::lower($uncamelizeMethod);
         if (!empty($args)) {
             $val = current($args);
         } else {
             $val = null;
         }
         if (fnmatch('*_id', $field)) {
             if (is_numeric($val)) {
                 $val = (int) $val;
             } elseif (is_object($val)) {
                 $val = (int) $val->id;
                 $this->_data[str_replace('_id', '', $field)] = $val->toArray();
             }
         } else {
             if (is_object($val)) {
                 if ($val instanceof \Thin\TimeLib) {
                     $val = $val->timestamp;
                 } else {
                     $this->_data[$field . '_id'] = $val->id;
                     $val = $val->toArray();
                 }
             }
         }
         $method = lcfirst(Inflector::camelize('set_' . $field . '_attribute'));
         $methods = get_class_methods($this);
         if (in_array($method, $methods)) {
             $val = $this->{$method}($val);
         }
         $this->_data[$field] = $val;
         $autosave = isAke($this->_data, 'autosave', false);
         return !$autosave ? $this : $this->save();
     } else {
         $cb = isAke($this->_events, $func, false);
         if (false !== $cb) {
             if ($cb instanceof Closure) {
                 return call_user_func_array($cb, $args);
             }
         } else {
             if ($func[strlen($func) - 1] == 's' && isset($this->_data['id']) && $func[0] != '_') {
                 $db = Db::instance($this->_db->db, substr($func, 0, -1));
                 $object = count($args) == 1 ? $args[0] : false;
                 if (!is_bool($object)) {
                     $object = false;
                 }
                 $hasPivot = $this->hasPivot($db);
                 if (true === $hasPivot) {
                     $model = $db->model();
                     $pivots = $this->pivots($model)->get();
                     $ids = [];
                     if ($pivots->count() > 0) {
                         foreach ($pivots as $pivot) {
                             $id = isAke($pivot, substr($func, 0, -1) . '_id', false);
                             if (false !== $id) {
                                 array_push($ids, $id);
                             }
                         }
                         if (!empty($ids)) {
                             return $db->where(['id', 'IN', implode(',', $ids)])->get(null, $object);
                         } else {
                             return [];
                         }
                     }
                 } else {
                     $idField = $this->_db->table . '_id';
                     return $object ? $db->where([$idField, '=', $this->_data['id']])->models()->toArray() : $db->where([$idField, '=', $this->_data['id']])->cursor()->toArray();
                 }
             } else {
                 if (!empty($args)) {
                     $object = count($args) == 1 ? $args[0] : false;
                     $db = Db::instance($this->_db->db, $func);
                     $field = $func . '_id';
                     if (is_bool($object) && isset($this->_data[$field])) {
                         return $db->find($value, $object);
                     } elseif (is_object($object)) {
                         $this->{$field} = (int) $object->id;
                         return $this;
                     }
                 }
                 $auth = ['checkIndices', '_hooks', 'rel', 'boot'];
                 if (in_array($func, $auth)) {
                     return true;
                 } else {
                     $scopes = $this->_db->store->get('scopes', []);
                     $scope = Inflector::uncamelize($func);
                     $closure = isAke($scopes, $scope, false);
                     if ($closure) {
                         if (is_callable($closure)) {
                             $model = $this;
                             return call_user_func_array($closure, [$model]);
                         }
                     }
                 }
                 throw new Exception("{$func} is not a model function of {$this->_db}.");
             }
         }
     }
 }
Exemple #25
0
 public function __call($m, $a)
 {
     if (fnmatch('get*', $m)) {
         $k = Inflector::uncamelize(substr($m, 3));
         $default = empty($a) ? null : current($a);
         return $this->get($k, $default);
     } elseif (fnmatch('set*', $m)) {
         $k = Inflector::uncamelize(substr($m, 3));
         return $this->set($k, current($a));
     } elseif (fnmatch('has*', $m)) {
         $k = Inflector::uncamelize(substr($m, 3));
         return $this->has($k);
     } elseif (fnmatch('del*', $m)) {
         $k = Inflector::uncamelize(substr($m, 3));
         return $this->del($k);
     } else {
         if (!empty($a)) {
             if (count($a) == 1) {
                 return $this->set($m, current($a));
             }
         }
         $closure = $this->get($m);
         if (fnmatch('*::*', $closure)) {
             list($c, $f) = explode('::', $closure, 2);
             $i = lib('caller')->make($c);
             return call_user_func_array([$i, $f], $a);
         } else {
             if (is_callable($closure)) {
                 return call_user_func_array($closure, $a);
             }
             return $closure;
         }
     }
 }
Exemple #26
0
 private static function defaultRoute()
 {
     $tab = explode('/', substr(static::$_uri, 1));
     if (count($tab) > 1) {
         if (3 != count($tab)) {
             $module = container()->getConfig()->getDefaultModule();
             $module = Inflector::lower($module);
             $controller = Inflector::lower(Arrays::first($tab));
             $action = $tab[1];
         } else {
             list($module, $controller, $action) = $tab;
             $module = Inflector::lower($module);
             $controller = Inflector::lower($controller);
             $action = Inflector::lower($action);
         }
         $action = repl(array('.html', '.php', '.asp', '.jsp', '.cfm', '.py', '.pl'), array('', '', '', '', '', '', ''), $action);
         if (true === container()->getMultiSite()) {
             $moduleDir = APPLICATION_PATH . DS . SITE_NAME . DS . 'modules' . DS . $module;
         } else {
             $moduleDir = APPLICATION_PATH . DS . 'modules' . DS . $module;
         }
         $controllerDir = $moduleDir . DS . 'controllers';
         $controllerFile = $controllerDir . DS . $controller . 'Controller.php';
         if (true === File::exists($controllerFile)) {
             require_once $controllerFile;
             $controllerClass = 'Thin\\' . $controller . 'Controller';
             $controllerInstance = new $controllerClass();
             $actions = get_class_methods($controllerInstance);
             $actionName = $action . 'Action';
             if (Arrays::inArray($actionName, $actions)) {
                 $dispatch = new Container();
                 $dispatch->setModule($module);
                 $dispatch->setController($controller);
                 $dispatch->setAction(Inflector::uncamelize($action, '-'));
                 Utils::set('appDispatch', $dispatch);
                 return true;
             }
         }
     }
     return null;
 }
Exemple #27
0
 public function __call($fn, $args)
 {
     $method = substr($fn, 0, strlen('findLastBy'));
     $object = Inflector::uncamelize(lcfirst(substr($fn, strlen('findLastBy'))));
     if (strlen($fn) > strlen('findLastBy')) {
         if ('findLastBy' == $method) {
             $obj = count($args) == 2 ? $args[1] : true;
             if (!is_bool($obj)) {
                 $obj = true;
             }
             return $this->where([$object, '=', Arrays::first($args)])->last($obj);
         }
     }
     $method = substr($fn, 0, strlen('findFirstBy'));
     $object = Inflector::uncamelize(lcfirst(substr($fn, strlen('findFirstBy'))));
     if (strlen($fn) > strlen('findFirstBy')) {
         if ('findFirstBy' == $method) {
             $obj = count($args) == 2 ? $args[1] : true;
             if (!is_bool($obj)) {
                 $obj = true;
             }
             return $this->findFirstBy($object, Arrays::first($args), $obj);
         }
     }
     $method = substr($fn, 0, strlen('findOneBy'));
     $object = Inflector::uncamelize(lcfirst(substr($fn, strlen('findOneBy'))));
     if (strlen($fn) > strlen('findOneBy')) {
         if ('findOneBy' == $method) {
             $obj = count($args) == 2 ? $args[1] : false;
             if (!is_bool($obj)) {
                 $obj = false;
             }
             return $this->findOneBy($object, Arrays::first($args), $obj);
         }
     }
     $method = substr($fn, 0, strlen('orderBy'));
     $object = Inflector::uncamelize(lcfirst(substr($fn, strlen('orderBy'))));
     if (strlen($fn) > strlen('orderBy')) {
         if ('orderBy' == $method) {
             $fields = $this->fieldsRow();
             if (!Arrays::in($object, $fields) && 'id' != $object) {
                 $object = Arrays::in($object . '_id', $fields) ? $object . '_id' : $object;
             }
             $direction = !empty($args) ? Arrays::first($args) : 'ASC';
             return $this->order($object, $direction);
         } elseif ('groupBy' == $method) {
             $fields = $this->fieldsRow();
             if (!Arrays::in($object, $fields)) {
                 $object = Arrays::in($object . '_id', $fields) ? $object . '_id' : $object;
             }
             return $this->groupBy($object);
         }
     }
     $method = substr($fn, 0, strlen('where'));
     $object = Inflector::uncamelize(lcfirst(substr($fn, strlen('where'))));
     if (strlen($fn) > strlen('where')) {
         if ('where' == $method) {
             return $this->where([$object, '=', Arrays::first($args)]);
         }
     }
     $method = substr($fn, 0, strlen('sortBy'));
     $object = Inflector::uncamelize(lcfirst(substr($fn, strlen('sortBy'))));
     if (strlen($fn) > strlen('sortBy')) {
         if ('sortBy' == $method) {
             $fields = $this->fieldsRow();
             if (!Arrays::in($object, $fields) && 'id' != $object) {
                 $object = Arrays::in($object . '_id', $fields) ? $object . '_id' : $object;
             }
             $direction = !empty($args) ? Arrays::first($args) : 'ASC';
             return $this->order($object, $direction);
         } elseif ('findBy' == $method) {
             $obj = count($args) == 2 ? $args[1] : false;
             if (!is_bool($obj)) {
                 $obj = false;
             }
             return $this->findBy($object, Arrays::first($args), false, $obj);
         }
     }
     $model = $this->model();
     $scope = lcfirst(Inflector::camelize('scope_' . Inflector::uncamelize($fn)));
     if (method_exists($model, $scope)) {
         return call_user_func_array([$model, $scope], $args);
     }
     throw new Exception("Method '{$fn}' is unknown.");
 }
Exemple #28
0
 public function __call($func, $argv)
 {
     $key = sha1('orm' . $this->_token);
     $orm = isAke($this->values, $key, false);
     $key = sha1('model' . $this->_token);
     $dbjson = isAke($this->values, $key, false);
     if (substr($func, 0, 4) == 'link' && false !== $orm) {
         $value = Arrays::first($argv);
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 4)));
         $var = Inflector::lower($uncamelizeMethod);
         if (!empty($var)) {
             $var = setter($var . '_id');
             $this->{$var}($value->id);
             return $this;
         }
     } elseif (substr($func, 0, 3) == 'get') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3)));
         $var = Inflector::lower($uncamelizeMethod);
         if (isset($this->{$var})) {
             if (isset($this->thin_type)) {
                 $type = $this->thin_type;
                 Data::getModel($type);
                 $settings = Arrays::exists($type, Data::$_settings) ? Data::$_settings[$type] : [];
                 if (Arrays::exists('relationships', $settings)) {
                     if (Arrays::exists($var, $settings['relationships'])) {
                         return Data::getById($var, $this->{$var});
                     }
                 }
             }
             if (Arrays::is($this->{$var}) && count($argv) == 1) {
                 $o = new self();
                 $getter = getter(Arrays::first($argv));
                 $o->populate($this->{$var});
                 return $o->{$getter}();
             }
             if ($this->{$var} instanceof Closure) {
                 if (is_callable($this->{$var}) && count($argv)) {
                     return call_user_func_array($this->{$var}, $argv);
                 }
             }
             return count($argv) && is_null($this->{$var}) ? Arrays::first($argv) : $this->{$var};
         } else {
             if (isset($this->db_instance)) {
                 return $this->db_instance->getValue($this, $var);
             }
             if (isset($this->thin_type)) {
                 $type = $this->thin_type;
                 Data::getModel($type);
                 $settings = Arrays::exists($type, Data::$_settings) ? Data::$_settings[$type] : [];
                 $relationships = Arrays::exists('relationships', $settings) ? $settings['relationships'] : [];
                 if (Arrays::exists($var, $relationships) && 's' == $var[strlen($var) - 1]) {
                     if (Arrays::exists($var, $relationships)) {
                         $res = dm(substr($var, 0, -1))->where("{$type} = " . $this->id)->get();
                         $collection = [];
                         if (count($res)) {
                             foreach ($res as $obj) {
                                 array_push($collection, $obj);
                             }
                         }
                         return $collection;
                     }
                 } elseif (Arrays::exists('defaultValues', $settings)) {
                     if (Arrays::is($settings['defaultValues'])) {
                         if (Arrays::exists($this->{$var}, $settings['defaultValues'])) {
                             return $settings['defaultValues'][$this->{$var}];
                         }
                     }
                 }
             }
             if (count($argv) == 1) {
                 return Arrays::first($argv);
             }
             return null;
         }
     } elseif (substr($func, 0, 3) == 'has') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3)));
         $var = Inflector::lower($uncamelizeMethod);
         if (isset($this->{$var})) {
             return !empty($this->{$var});
         } elseif (isset($this->db_instance)) {
             return $this->db_instance->hasValue($this, $var);
         }
     } elseif (substr($func, 0, 3) == 'set') {
         $value = Arrays::first($argv);
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3)));
         $var = Inflector::lower($uncamelizeMethod);
         if (!empty($var)) {
             if (isset($this->thin_type)) {
                 Data::getModel($this->thin_type);
                 $fields = Arrays::exists($this->thin_type, Data::$_fields) ? Data::$_fields[$this->thin_type] : [];
                 if (!Arrays::exists($var, $fields)) {
                     throw new Exception($var . ' is not defined in the model => ' . $this->thin_type);
                 } else {
                     $settingsField = $fields[$var];
                     if (Arrays::exists('checkValue', $settingsField)) {
                         $functionCheck = $settingsField['checkValue'];
                         $value = $functionCheck($value);
                     }
                     if (is_object($value)) {
                         if (isset($value->thin_type)) {
                             if ($value->thin_type == $var) {
                                 $value = $value->id;
                             }
                         }
                     }
                 }
             }
             $this->{$var} = $value;
             if (!Arrays::in($var, $this->_fields)) {
                 $this->_fields[] = $var;
             }
             if (isset($this->is_thin_object)) {
                 $name = $this->is_thin_object;
                 $objects = Utils::get('thinObjects');
                 $this->values = null;
                 $objects[$name] = $this;
                 Utils::set('thinObjects', $objects);
             }
             if (isset($this->is_app)) {
                 if (true === $this->is_app) {
                     Utils::set('ThinAppContainer', $this);
                 }
             }
         } elseif (isset($this->db_instance)) {
             return $this->db_instance->setValue($this, $var, $value);
         }
         return $this;
     } elseif (substr($func, 0, 3) == 'add') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3)));
         $var = Inflector::lower($uncamelizeMethod) . 's';
         $value = Arrays::first($argv);
         if (!isset($this->{$var})) {
             $this->{$var} = [];
         }
         if (!Arrays::is($this->{$var})) {
             $this->{$var} = [];
         }
         array_push($this->{$var}, $value);
         return $this;
     } elseif (substr($func, 0, 6) == 'remove') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 6)));
         $var = Inflector::lower($uncamelizeMethod) . 's';
         $value = Arrays::first($argv);
         if (isset($this->{$var})) {
             if (Arrays::is($this->{$var})) {
                 if (count($this->{$var})) {
                     $remove = false;
                     foreach ($this->{$var} as $key => $tmpValue) {
                         $comp = md5(serialize($value)) == md5(serialize($tmpValue));
                         if (true === $comp) {
                             $remove = true;
                             break;
                         }
                     }
                     if (true === $remove) {
                         unset($this->{$var}[$key]);
                     }
                 }
             }
         }
         return $this;
     }
     if (Arrays::in($func, $this->_fields)) {
         if ($this->{$func} instanceof Closure) {
             return call_user_func_array($this->{$func}, $argv);
         }
     }
     if (Arrays::exists($func, $this->_closures)) {
         if ($this->_closures[$func] instanceof Closure) {
             return call_user_func_array($this->_closures[$func], $argv);
         }
     }
     if (isset($this->_token)) {
         $id = sha1($func . $this->_token);
         if (Arrays::is($this->values)) {
             if (Arrays::exists($id, $this->values)) {
                 return call_user_func_array($this->values[$id], $argv);
             }
         }
     }
     if (true === hasEvent($func)) {
         array_push($argv, $this);
         return fire($func, $argv);
     }
     if (!is_callable($func) || substr($func, 0, 6) !== 'array_' || substr($func, 0, 3) !== 'set' || substr($func, 0, 3) !== 'get' || substr($func, 0, 3) !== 'has' || substr($func, 0, 3) !== 'add' || substr($func, 0, 6) !== 'remove') {
         $callable = strrev(repl('_', '', $func));
         if (!is_callable($callable)) {
             if (method_exists($this, $callable)) {
                 return call_user_func_array(array($this, $callable), $argv);
             }
         } else {
             return call_user_func_array($callable, $argv);
         }
         if (isset($this->thin_litedb)) {
             $closure = isAke($this->thin_litedb->closures, $func);
             if (!empty($closure) && $closure instanceof Closure) {
                 return $closure($this);
             }
         }
         if (isset($this->db_instance)) {
             return $this->db_instance->{$func}($this, $var, $value);
             call_user_func_array(array($this->db_instance, $func), array_merge(array($this), $argv));
         }
         if (false !== $orm) {
             $db = call_user_func_array($orm, []);
             $fields = array_keys($db->map['fields']);
             if (Arrays::in($func, $fields)) {
                 if (!count($argv)) {
                     return $this->{$func};
                 } else {
                     $setter = setter($func);
                     $this->{$setter}(Arrays::first($argv));
                     return $this;
                 }
             }
             $tab = str_split($func);
             $many = false;
             if (Arrays::last($tab) == 's') {
                 array_pop($tab);
                 $table = implode('', $tab);
                 $many = true;
             } else {
                 $table = $func;
             }
             $object = count($argv) == 1 ? Arrays::first($argv) : false;
             $model = model($table);
             return true === $many ? $model->where($db->table . '_id = ' . $this->id)->exec($object) : $model->where($db->table . '_id = ' . $this->id)->first($object);
         }
         if (false !== $dbjson) {
             $db = $this->model()->db();
             $fields = $db->fields();
             $modelMethods = get_class_methods('Dbjson\\Model');
             if (Arrays::in($func, $fields)) {
                 if (!count($argv)) {
                     return $this->{$func};
                 } else {
                     $setter = setter($func);
                     $this->{$setter}(Arrays::first($argv));
                     return $this;
                 }
             }
             if (Arrays::in($func, $modelMethods)) {
                 return call_user_func_array([$this->model(), $func], $argv);
             }
             $tab = str_split($func);
             $many = false;
             if (Arrays::last($tab) == 's') {
                 array_pop($tab);
                 $table = implode('', $tab);
                 $many = true;
             } else {
                 $table = $func;
             }
             $object = count($argv) == 1 ? Arrays::first($argv) : true;
             $model = jdb($db->db, $table);
             return true === $many ? $model->where($db->table . '_id = ' . $this->id)->exec($object) : $model->where($db->table . '_id = ' . $this->id)->first($object);
         }
         return null;
     }
     return call_user_func_array($func, array_merge(array($this->getArrayCopy()), $argv));
 }
Exemple #29
0
 function repo($entity)
 {
     $em = Doctrine::em();
     $class = ucfirst(Inflector::uncamelize($entity));
     $file = APPLICATION_PATH . DS . 'models' . DS . 'Doctrine' . DS . preg_replace('#\\\\|_(?!.+\\\\)#', DS, $class) . '.php';
     if (File::exists($file)) {
         require_once $file;
         return $em->getRepository('\\Thin\\Doctrine' . ucfirst($entity) . 'Entity');
     }
     return null;
 }
Exemple #30
0
 public function __call($method, $args)
 {
     if (substr($method, 0, strlen('get')) == 'get' && strlen($method) > strlen('get')) {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, strlen('get'))));
         $field = Inflector::lower($uncamelizeMethod);
         $default = count($args) ? reset($args) : null;
         return $this->get($field, $default);
     } elseif (substr($method, 0, strlen('set')) == 'set' && strlen($method) > strlen('set')) {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, strlen('get'))));
         $field = Inflector::lower($uncamelizeMethod);
         $value = count($args) ? reset($args) : null;
         $this->set($field, $value);
         return $this;
     } elseif (substr($method, 0, strlen('has')) == 'has' && strlen($method) > strlen('has')) {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, strlen('get'))));
         $field = Inflector::lower($uncamelizeMethod);
         return $this->has($field);
     } elseif (substr($method, 0, strlen('forget')) == 'forget' && strlen($method) > strlen('forget')) {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, strlen('get'))));
         $field = Inflector::lower($uncamelizeMethod);
         return $this->forget($field);
     } elseif (substr($method, 0, strlen('remove')) == 'remove' && strlen($method) > strlen('remove')) {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, strlen('get'))));
         $field = Inflector::lower($uncamelizeMethod);
         return $this->forget($field);
     } else {
         if (isset($this->__token)) {
             $id = sha1($method . $this->__token);
             $cb = isAke($this->__events, $id, false);
             if (false !== $cb) {
                 return call_user_func_array($cb, $args);
             }
         }
     }
     throw new Exception("Method '{$method}' does not exist.");
 }