Exemple #1
0
 public function session($k, $d = null)
 {
     $session = session('front');
     if (is_null($d)) {
         $getter = getter($k);
         $val = $session->{$getter}();
         return $val ? $val : $d;
     } else {
         $session->erase($k);
         $setter = setter($k);
         $session->{$setter}($d);
     }
 }
Exemple #2
0
 public function max($field, $results = array())
 {
     $res = count($results) ? $results : $this->results;
     $max = 0;
     if (count($res)) {
         $first = true;
         foreach ($res as $key => $id) {
             $object = $this->row($id);
             $getter = getter($field);
             $val = $object->{$getter}();
             if (true === $first) {
                 $max = $val;
             } else {
                 $max = $val > $max ? $val : $max;
             }
             $first = false;
         }
     }
     $this->reset();
     return $max;
 }
Exemple #3
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 #4
0
 function kv()
 {
     $args = func_get_args();
     $s = session('kv');
     if (count($args) == 1) {
         $get = getter(Arrays::first($args));
         return $s->{$get}();
     } elseif (count($args) == 2) {
         $set = setter(Arrays::first($args));
         return $s->{$set}(Arrays::last($args));
     }
     return null;
 }
Exemple #5
0
 public function max($field, $results = array())
 {
     $res = count($results) ? $results : $this->currrentRes();
     $max = 0;
     if (count($res)) {
         $first = true;
         $data = $this->makeData($res);
         foreach ($data as $object) {
             $getter = getter($field);
             $val = $object->{$getter}();
             if (true === $first) {
                 $max = $val;
             } else {
                 $max = $val > $max ? $val : $max;
             }
             $first = false;
         }
     }
     $this->reset();
     return $max;
 }
Exemple #6
0
 public function fetch($results = null)
 {
     $this->count = count($results);
     $results = empty($results) ? $this->results : $results;
     if (count($results)) {
         if (null !== $this->groupBy) {
             $groupBys = array();
             $ever = array();
             foreach ($results as $key => $object) {
                 $id = $object->getId();
                 $getter = getter($this->groupBy);
                 $obj = $object->{$getter}();
                 if ($obj instanceof Container) {
                     $id = $obj->getId();
                 }
                 if (!Arrays::in($id, $ever)) {
                     $groupBys[$key] = $this->find($id);
                     $ever[] = $id;
                 }
             }
             $this->results = $groupBys;
             $this->order($this->groupBy);
             $results = $this->results;
         }
         if (0 < $this->limit) {
             $max = count($results);
             $number = $this->limit - $this->offset;
             if ($number > $max) {
                 $this->offset = $max - $this->limit;
                 if (0 > $this->offset) {
                     $this->offset = 0;
                 }
                 $this->limit = $max;
             }
             $results = array_slice($results, $this->offset, $this->limit);
         }
     }
     $this->results = $results;
     return $results;
 }
Exemple #7
0
 public function get($results = null)
 {
     $resultsGet = null !== $results ? $results : $this->results;
     $queryKey = sha1(serialize($this->wheres) . serialize($resultsGet));
     $cache = Data::cache($this->type, $queryKey);
     if (!empty($cache) && true === $this->cache) {
         return $cache;
     }
     if (count($resultsGet)) {
         if (null !== $this->groupBy) {
             $groupBys = array();
             $ever = array();
             foreach ($resultsGet as $key => $id) {
                 $object = Data::getById($this->type, $id);
                 $getter = getter($this->groupBy);
                 $obj = $object->{$getter}();
                 if ($obj instanceof Container) {
                     $obj = $obj->getId();
                 }
                 if (!Arrays::in($obj, $ever)) {
                     $groupBys[$key] = $id;
                     $ever[] = $obj;
                 }
             }
             $this->results = $groupBys;
             $this->order($this->groupBy);
             $resultsGet = $this->results;
         }
         if (0 < $this->limit) {
             $max = count($resultsGet);
             $number = $this->limit - $this->offset;
             if ($number > $max) {
                 $this->offset = $max - $this->limit;
                 if (0 > $this->offset) {
                     $this->offset = 0;
                 }
                 $this->limit = $max;
             }
             $resultsGet = array_slice($resultsGet, $this->offset, $this->limit);
         }
     }
     $collection = array();
     if (count($resultsGet)) {
         $_sum = 0;
         $_avg = 0;
         $_min = 0;
         $_max = 0;
         $first = true;
         foreach ($resultsGet as $key => $id) {
             $object = Data::getById($this->type, $id);
             if (null !== $this->sum) {
                 $getter = getter($this->sum);
                 $_sum += $object->{$getter}();
             }
             if (null !== $this->avg) {
                 $getter = getter($this->avg);
                 $_avg += $object->{$getter}();
             }
             if (null !== $this->min) {
                 $getter = getter($this->min);
                 if (true === $first) {
                     $_min = $object->{$getter}();
                 } else {
                     $_min = $object->{$getter}() < $_min ? $object->{$getter}() : $_min;
                 }
             }
             if (null !== $this->max) {
                 $getter = getter($this->max);
                 if (true === $first) {
                     $_max = $object->{$getter}();
                 } else {
                     $_max = $object->{$getter}() > $_max ? $object->{$getter}() : $_max;
                 }
             }
             $collection[] = $object;
             $first = false;
         }
     }
     if (null !== $this->min) {
         $collection = $_min;
     }
     if (null !== $this->max) {
         $collection = $_max;
     }
     if (null !== $this->sum) {
         $collection = $_sum;
     }
     if (null !== $this->avg) {
         $collection = $_avg / count($collection);
     }
     $cache = Data::cache($this->type, $queryKey, $collection);
     return $collection;
 }