Ejemplo n.º 1
0
 public function __construct(array $array)
 {
     $this->key = sha1(serialize($array));
     if (Arrays::isAssoc($array)) {
         $tab = new SplFixedArray(1);
         $tab[0] = $array;
         $this->key .= 'AAA';
     } else {
         $tab = SplFixedArray::fromArray($array);
     }
     lib('resource')->set($this->key, $tab);
 }
Ejemplo n.º 2
0
 public function read($name, $default = null)
 {
     $file = $this->getFile($name);
     $content = $this->import($file);
     if ($content) {
         if (Arrays::isAssoc($content)) {
             return $content;
         }
         return current($content);
     }
     return $default;
 }
Ejemplo n.º 3
0
 public function newRow($data = array())
 {
     $object = o(sha1(time() . $this->settings['entity'] . session_id() . Utils::token()));
     $object->thin_litedb = $this;
     $object->id = null;
     if (count($data) && Arrays::isAssoc($data)) {
         foreach ($this->settings['modelFields'] as $field => $infos) {
             $value = ake($field, $data) ? $data[$field] : null;
             $object->{$field} = $value;
         }
     }
     return $object;
 }
Ejemplo n.º 4
0
 public function save($obj)
 {
     $data = $obj->tab();
     $fields = $this->fields;
     $id = Arrays::exists('id', $data) ? $data['id'] : $this->makeKey();
     $new = !Arrays::exists('id', $data);
     $datemodify = time();
     $checkTuple = Arrays::exists('checkTuple', $this->settings) ? $this->settings['checkTuple'] : null;
     $store = array();
     if (true === $new) {
         $datecreate = time();
     } else {
         $datecreate = $data['date_create'];
     }
     if (count($data) && Arrays::isAssoc($data)) {
         foreach ($fields as $field => $info) {
             $val = Arrays::exists($field, $data) ? $data[$field] : null;
             if (empty($val)) {
                 if (!Arrays::exists('canBeNull', $info)) {
                     if (!Arrays::exists('default', $info)) {
                         throw new Exception('The field ' . $field . ' cannot be null.');
                     } else {
                         $val = $info['default'];
                     }
                 }
             } else {
                 if (Arrays::exists('sha1', $info)) {
                     if (!preg_match('/^[0-9a-f]{40}$/i', $val) || strlen($val) != 40) {
                         $val = sha1($val);
                     }
                 } elseif (Arrays::exists('md5', $info)) {
                     if (!preg_match('/^[0-9a-f]{32}$/i', $val) || strlen($val) != 32) {
                         $val = md5($val);
                     }
                 } elseif (Arrays::exists('checkValue', $info)) {
                     $closure = $info['checkValue'];
                     if ($closure instanceof Closure) {
                         $val = $closure($val);
                     }
                 }
             }
             $store[$field] = $val;
         }
         if (!empty($checkTuple)) {
             if (is_string($checkTuple)) {
                 $cond = "{$checkTuple} = " . $store[$checkTuple];
                 $res = $this->query($cond);
             }
             if (Arrays::is($checkTuple)) {
                 $query = '';
                 foreach ($checkTuple as $ct) {
                     $query .= $ct . ' = ' . $store[$ct] . ' && ';
                 }
                 $query = substr($query, 0, -4);
                 $tabConditions = explode(' && ', $query);
                 $init = true;
                 foreach ($tabConditions as $cond) {
                     $res = $this->query($cond);
                     if (true === $init) {
                         $init = false;
                         $results = $res;
                     } else {
                         $results = array_intersect($results, $res);
                     }
                 }
                 $res = $this->fetch($results);
             }
             if (count($res) && !Arrays::in($id, $res)) {
                 $tupleId = is_object(Arrays::first($res)) ? Arrays::first($res)->getId() : Arrays::first($res);
                 return $this->row($tupleId);
             }
         }
         if (true === $new) {
             $q = "INSERT INTO datas (id, datecreate, datemodify, value) VALUES (\n                        '" . SQLite3::escapeString($id) . "',\n                        '" . SQLite3::escapeString($datecreate) . "',\n                        '" . SQLite3::escapeString($datemodify) . "',\n                        '" . SQLite3::escapeString(serialize($store)) . "'\n                    )";
         } else {
             $q = "UPDATE datas SET\n                        datemodify = '" . SQLite3::escapeString($datemodify) . "',\n                        value = '" . SQLite3::escapeString(serialize($store)) . "'\n                        WHERE id = '" . SQLite3::escapeString($id) . "'\n                    ";
         }
         if (false === $this->transac) {
             $this->db->exec($q);
         } else {
             array_push($this->transactions, $q);
         }
         $data = $this->session->getData();
         $store['id'] = $id;
         $store['date_create'] = $datecreate;
         $row = $this->make($store);
         $data[$id] = $row;
         $this->session->setData($data);
         return $row;
     }
     return $obj;
 }
Ejemplo n.º 5
0
 public function populate(array $datas, $namespace = null)
 {
     if (null !== $namespace) {
         if (!isset($this->{$namespace})) {
             $this->{$namespace} = [];
         }
         foreach ($datas as $k => $v) {
             if (Arrays::is($k)) {
                 $this->populate($k, $namespace);
             } else {
                 $this->{$namespace} = array_merge($this->{$namespace}, [$k => $v]);
             }
         }
     } else {
         foreach ($datas as $k => $v) {
             $id = isAke($datas, 'id', false);
             if (Arrays::is($v) && false === $id) {
                 if (Arrays::isAssoc($v)) {
                     $o = new self();
                     $o->populate($v);
                     $this->{$k} = $o;
                 } else {
                     $this->{$k} = $v;
                 }
             } else {
                 $this->{$k} = $v;
             }
             if (!Arrays::in($k, $this->_fields)) {
                 $this->_fields[] = $k;
             }
         }
     }
     return $this;
 }
Ejemplo n.º 6
0
 public function hydrate(array $data = [])
 {
     $data = empty($data) ? $_POST : $data;
     if (Arrays::isAssoc($data)) {
         foreach ($data as $k => $v) {
             if ($k != 'id') {
                 if ('true' == $v) {
                     $v = true;
                 } elseif ('false' == $v) {
                     $v = false;
                 } elseif ('null' == $v) {
                     $v = null;
                 }
                 if (fnmatch('*_id', $k)) {
                     if (is_numeric($v)) {
                         $v = (int) $v;
                     } elseif (is_object($value)) {
                         $v = (int) $v->id;
                         $this->_data[str_replace('_id', '', $k)] = $v->toArray();
                     }
                 } else {
                     if (is_object($value)) {
                         $this->_data[$k . '_id'] = $v->id;
                         $value = $v->toArray();
                     }
                 }
                 $this->_data[$k] = $v;
             }
         }
     }
     return $this;
 }
Ejemplo n.º 7
0
 function isAke($tab, $key, $default = array())
 {
     return Arrays::is($tab) ? Arrays::isAssoc($tab) ? Arrays::exists($key, $tab) ? $tab[$key] : $default : $default : $default;
 }
Ejemplo n.º 8
0
 private function treatCast($tab)
 {
     if (!empty($tab) && Arrays::isAssoc($tab)) {
         foreach ($tab as $k => $v) {
             if (fnmatch('*_id', $k) && !empty($v)) {
                 if (is_numeric($v)) {
                     $tab[$k] = (int) $v;
                 }
             }
         }
     }
     return $tab;
 }
Ejemplo n.º 9
0
 private function closures($obj)
 {
     $params = $this->args;
     $fn = function ($name, Closure $callable) use($obj, $params) {
         if (is_callable($callable)) {
             list($db, $table, $host, $username, $password) = $params;
             $dbi = Database::instance($db, $table, $host, $username, $password);
             $share = function () use($obj, $callable, $dbi) {
                 $args = func_get_args();
                 $args[] = $obj;
                 $args[] = $dbi;
                 return call_user_func_array($callable, $args);
             };
             $obj->event($name, $share);
             return $obj;
         }
     };
     $save = function ($object = true) use($obj, $params) {
         list($db, $table, $host, $username, $password) = $params;
         $db = Database::instance($db, $table, $host, $username, $password);
         return $db->save($obj, $object);
     };
     $db = function () use($params) {
         list($db, $table, $host, $username, $password) = $params;
         return Database::instance($db, $table, $host, $username, $password);
     };
     $query = function () use($params) {
         list($db, $table, $host, $username, $password) = $params;
         return new Query(Database::instance($db, $table, $host, $username, $password));
     };
     $delete = function () use($obj, $params) {
         list($db, $table, $host, $username, $password) = $params;
         $db = Database::instance($db, $table, $host, $username, $password);
         $pk = $db->pk();
         return $db->deleteRow($obj->{$pk});
     };
     $id = function () use($obj, $params) {
         list($db, $table, $host, $username, $password) = $params;
         $db = Database::instance($db, $table, $host, $username, $password);
         $pk = $db->pk();
         return $obj->{$pk};
     };
     $exists = function () use($obj, $params) {
         list($db, $table, $host, $username, $password) = $params;
         $db = Database::instance($db, $table, $host, $username, $password);
         $pk = $db->pk();
         return isset($obj->{$pk});
     };
     $touch = function () use($obj) {
         if (!isset($obj->created_at)) {
             $obj->created_at = time();
         }
         $obj->updated_at = time();
         return $obj;
     };
     $duplicate = function ($object = true) use($obj, $params) {
         list($db, $table, $host, $username, $password) = $params;
         $db = Database::instance($db, $table, $host, $username, $password);
         $pk = $db->pk();
         if (isset($obj->{$pk})) {
             unset($obj->{$pk});
         }
         if (isset($obj->created_at)) {
             unset($obj->created_at);
         }
         return $obj->save($object);
     };
     $orm = function () use($params) {
         list($db, $table, $host, $username, $password) = $params;
         return Database::instance($db, $table, $host, $username, $password);
     };
     $cache = function ($bool = true) use($params) {
         list($db, $table, $host, $username, $password) = $params;
         $database = Database::instance($db, $table, $host, $username, $password);
         $database->cache($bool);
         return $database;
     };
     $as = $this->as;
     $foreign = function () use($as) {
         return $as;
     };
     $many = function ($table, $field = null, $object = false) use($obj) {
         $tab = $obj->assoc();
         $db = model($table);
         $pk = is_null($field) ? $db->pk() : $field;
         $value = $tab[$db->table . '_id'];
         return $db->where("{$pk} = {$value}")->execute($object);
     };
     $one = function ($table, $field = null, $object = false) use($obj) {
         $tab = $obj->assoc();
         $db = model($table);
         $pk = is_null($field) ? $db->pk() : $field;
         $value = $tab[$db->table . '_id'];
         return $db->where("{$pk} = {$value}")->first($object);
     };
     $hydrate = function ($data = array()) use($obj) {
         $data = empty($data) ? $_POST : $data;
         if (Arrays::isAssoc($data)) {
             foreach ($data as $k => $v) {
                 if ("true" == $v) {
                     $v = true;
                 } elseif ("false" == $v) {
                     $v = false;
                 } elseif ("null" == $v) {
                     $v = null;
                 }
                 $obj->{$k} = $v;
             }
         }
         return $obj;
     };
     $obj->event('save', $save)->event('delete', $delete)->event('exists', $exists)->event('id', $id)->event('touch', $touch)->event('duplicate', $duplicate)->event('foreign', $foreign)->event('orm', $orm)->event('many', $many)->event('hydrate', $hydrate)->event('one', $one)->event('cache', $cache)->event('fn', $fn);
     list($db, $table, $host, $username, $password) = $params;
     $settings = isAke(self::$config, "{$db}.{$table}");
     $functions = isAke($settings, 'functions');
     if (count($functions)) {
         foreach ($functions as $closureName => $callable) {
             $closureName = lcfirst(Inflector::camelize($closureName));
             $share = function () use($obj, $params, $callable) {
                 list($db, $table, $host, $username, $password) = $params;
                 $args = func_get_args();
                 $args[] = $obj;
                 $args[] = Database::instance($db, $table, $host, $username, $password);
                 return call_user_func_array($callable, $args);
             };
             $obj->event($closureName, $share);
         }
     }
     return $this->related($obj);
 }
Ejemplo n.º 10
0
 private function closures($obj)
 {
     $db = $this;
     $db->results = null;
     $db->wheres = null;
     $save = function () use($obj, $db) {
         return $db->save($obj);
     };
     $database = function () use($db) {
         return $db;
     };
     $delete = function () use($obj, $db) {
         return $db->deleteRow($obj->id);
     };
     $deleteSoft = function () use($obj, $db) {
         $obj->deleted_at = time();
         return $db->save($obj);
     };
     $id = function () use($obj) {
         return isset($obj->id) ? $obj->id : null;
     };
     $exists = function () use($obj) {
         return isset($obj->id);
     };
     $touch = function () use($obj) {
         if (!isset($obj->created_at)) {
             $obj->created_at = time();
         }
         $obj->updated_at = time();
         return $obj;
     };
     $duplicate = function () use($obj, $db) {
         $obj->copyrow = Utils::token();
         $data = $obj->assoc();
         unset($data['id']);
         unset($data['created_at']);
         unset($data['updated_at']);
         $obj = $db->row($data);
         return $obj->save();
     };
     $hydrate = function ($data = []) use($obj) {
         $data = empty($data) ? $_POST : $data;
         if (Arrays::isAssoc($data)) {
             foreach ($data as $k => $v) {
                 if ('true' == $v) {
                     $v = true;
                 } elseif ('false' == $v) {
                     $v = false;
                 } elseif ('null' == $v) {
                     $v = null;
                 }
                 $obj->{$k} = $v;
             }
         }
         return $obj;
     };
     $date = function ($field, $format = 'Y-m-d H:i:s') use($obj) {
         return date($format, $obj->{$field});
     };
     $string = function () use($obj) {
         if (isset($obj->name)) {
             return $obj->name;
         }
         return $obj->id;
     };
     $model = function () use($db, $obj) {
         return Model::instance($db, $obj);
     };
     $obj->event('save', $save)->event('delete', $delete)->event('deletesoft', $deleteSoft)->event('date', $date)->event('exists', $exists)->event('id', $id)->event('db', $database)->event('touch', $touch)->event('hydrate', $hydrate)->event('string', $string)->event('model', $model)->event('duplicate', $duplicate);
     $settings = isAke(self::$config, "{$this->db}.{$this->table}");
     $functions = isAke($settings, 'functions');
     if (!empty($functions)) {
         foreach ($functions as $closureName => $callable) {
             $closureName = lcfirst(Inflector::camelize($closureName));
             if (Arrays::is($callable)) {
                 list($callable, $callableArgs) = $callable;
             } else {
                 $callableArgs = [];
             }
             $share = function () use($obj, $callable, $db, $callableArgs) {
                 $args = [];
                 if (!empty($callableArgs)) {
                     $args[] = $callableArgs;
                 }
                 $args[] = $obj;
                 $args[] = $db;
                 return call_user_func_array($callable, $args);
             };
             $obj->event($closureName, $share);
         }
     }
     return $this->related($obj);
 }
Ejemplo n.º 11
0
 private function functions($obj)
 {
     $settings = isAke(static::$configs, $this->entity);
     $class = $this;
     $class->results = null;
     $class->wheres = null;
     $save = function () use($class, $obj) {
         return $class->save($obj->assoc());
     };
     $delete = function () use($class, $obj) {
         return $class->delete($obj->getId());
     };
     $date = function ($f) use($obj) {
         return date('Y-m-d H:i:s', $obj->{$f});
     };
     $hydrate = function ($data) use($obj) {
         if (Arrays::isAssoc($data)) {
             foreach ($data as $k => $v) {
                 $obj->{$k} = $v;
             }
         }
         return $obj;
     };
     $display = function ($field, $echo = true) use($obj) {
         $val = Html\Helper::display($obj->{$field});
         if (true === $echo) {
             echo $val;
         } else {
             return $val;
         }
     };
     $tab = function () use($obj) {
         return $obj->assoc();
     };
     $asset = function ($field) use($obj) {
         return '/storage/img/' . $obj->{$field};
     };
     $obj->event('save', $save)->event('delete', $delete)->event('date', $date)->event('hydrate', $hydrate)->event('tab', $tab)->event('asset', $asset)->event('display', $display);
     $functions = isAke($settings, 'functions');
     if (count($functions)) {
         foreach ($functions as $closureName => $callable) {
             if (version_compare(PHP_VERSION, '5.4.0', "<")) {
                 $share = function () use($obj, $callable) {
                     return $callable($obj);
                 };
             } else {
                 $share = $callable->bindTo($obj);
             }
             $obj->event($closureName, $share);
         }
     }
     return $obj;
 }
Ejemplo n.º 12
0
 public function row(array $data, $recursive = true, $extends = array())
 {
     if (Arrays::isAssoc($data)) {
         $obj = o(sha1(serialize($data)));
         $obj->db_instance = $this;
         if (count($extends)) {
             foreach ($extends as $name => $instance) {
                 $closure = function ($object) use($name, $instance) {
                     $idx = $object->is_thin_object;
                     $objects = Utils::get('thinObjects');
                     return $instance->{$name}($objects[$idx]);
                 };
                 $obj->_closures[$name] = $closure;
             }
         }
         $fields = $this->fields();
         foreach ($fields as $field) {
             $hasFk = $this->hasFk($field);
             if (false === $hasFk) {
                 $obj->{$field} = $data[$field];
             } else {
                 extract($hasFk);
                 $ar = ar($foreignEntity, $foreignTable);
                 $one = contain('toone', Inflector::lower($type));
                 if ($one && $recursive) {
                     $foreignObj = $ar->findBy($foreignKey, $data[$field], $one);
                     $obj->{$relationKey} = $foreignObj;
                 }
             }
         }
         $hasFk = ake('relationships', $this->_settings);
         if (true === $hasFk && $recursive) {
             $rs = $this->_settings['relationships'];
             if (count($rs)) {
                 foreach ($rs as $field => $infos) {
                     extract($infos);
                     $ar = ar($foreignEntity, $foreignTable);
                     if (!Arrays::in($field, $fields)) {
                         $pk = $this->pk();
                         $obj->{$field} = $ar->findBy($foreignKey, $obj->{$pk}, false, false);
                     }
                 }
             }
         }
         return $obj;
     }
     return null;
 }
Ejemplo n.º 13
0
 private function delete()
 {
     if ($this->isDelete()) {
         $data = isAke($_REQUEST, 'data', []);
         if (!empty($data)) {
             if (Arrays::isAssoc($data)) {
                 $id = isAke($data, 'id', false);
                 if ($id) {
                     $row = $this->db->find($id);
                     if ($row) {
                         $row->delete();
                         Api::render(['status' => 200, 'execution_time' => Timer::get(), 'token' => $this->token, 'message' => 'Row id [' . $id . '] deleted']);
                     }
                 } else {
                     $where = isAke($data, 'where', false);
                     if ($where) {
                         $where = eval('return ' . $where . ';');
                         $cursor = $this->db->multiQuery($where)->cursor();
                         $count = $cursor->count();
                         $cursor->delete();
                         Api::render(['status' => 200, 'execution_time' => Timer::get(), 'token' => $this->token, 'message' => $count . ' rows deleted']);
                     }
                 }
             }
         }
     }
     Api::forbidden();
 }
Ejemplo n.º 14
0
 private function closures($obj)
 {
     $settings = isAke(self::$configs, $this->entity);
     $class = $this;
     $class->results = null;
     $class->wheres = null;
     $class->joins = null;
     $class->transactions = null;
     $export = function () use($class, $obj) {
         if (isset($obj->id)) {
             $class->where("id = " . $obj->id)->export();
         }
     };
     $save = function () use($class, $obj) {
         return $class->save($obj->assoc());
     };
     $delete = function () use($class, $obj) {
         return $class->delete($obj->getId());
     };
     $date = function ($f) use($obj) {
         return date('Y-m-d H:i:s', $obj->{$f});
     };
     $hydrate = function ($data = array()) use($obj) {
         $data = empty($data) ? $_POST : $data;
         if (Arrays::isAssoc($data)) {
             foreach ($data as $k => $v) {
                 if ("true" == $v) {
                     $v = true;
                 } elseif ("false" == $v) {
                     $v = false;
                 } elseif ("null" == $v) {
                     $v = null;
                 }
                 $obj->{$k} = $v;
             }
         }
         return $obj;
     };
     $display = function ($field, $echo = true) use($obj) {
         $val = Html\Helper::display($obj->{$field});
         if (true === $echo) {
             echo $val;
         } else {
             return $val;
         }
     };
     $hook = function ($callable, $primary, $when = 'before') use($obj) {
         if (!is_callable($obj->{$primary})) {
             throw new Exception("Hook method does not exist.");
         }
         $args = func_get_args();
         array_shift($args);
         array_shift($args);
         $newName = '_' . $primary;
         $obj->{$newName} = $obj->{$primary};
         $share = function () use($callable, $primary, $when, $obj, $args, $newName) {
             if ($when == 'before') {
                 array_push($args, $obj);
                 $obj = call_user_func_array($callable, $args);
                 array_pop($args);
                 return call_user_func_array($obj->{$newName}, $args);
             } elseif ($when == 'after') {
                 $obj = call_user_func_array($obj->{$newName}, $args);
                 array_push($args, $obj);
                 return call_user_func_array($callable, $args);
             }
         };
         $obj->event($primary, $share);
         return $obj;
     };
     $id = function () use($obj) {
         return $obj->getId();
     };
     $exists = function () use($obj) {
         $id = $obj->getId();
         return !is_null($id);
     };
     $obj->event('save', $save)->event('delete', $delete)->event('hook', $hook)->event('date', $date)->event('hydrate', $hydrate)->event('export', $export)->event('id', $id)->event('exists', $exists)->event('display', $display);
     $functions = isAke($settings, 'functions');
     if (count($functions)) {
         foreach ($functions as $closureName => $callable) {
             $closureName = lcfirst(Inflector::camelize($closureName));
             $share = function () use($obj, $callable) {
                 $args = func_get_args();
                 $args[] = $obj;
                 return call_user_func_array($callable, $args);
             };
             $obj->event($closureName, $share);
         }
     }
     $hidden = isAke($settings, 'hidden');
     if (count($hidden)) {
         foreach ($hidden as $hiddenField) {
             if (isset($obj->{$hiddenField}) && !is_callable($obj->{$hiddenField})) {
                 unset($obj->{$hiddenField});
             }
         }
     }
     return $this->related($obj);
 }
Ejemplo n.º 15
0
 private function closures($obj)
 {
     $db = $this;
     $db->results = null;
     $db->wheres = null;
     $save = function () use($obj, $db) {
         return $db->save($obj);
     };
     $database = function () use($db) {
         return $db;
     };
     $delete = function () use($obj, $db) {
         return $db->deleteRow($obj->id);
     };
     $id = function () use($obj) {
         return $obj->id;
     };
     $exists = function () use($obj) {
         return isset($obj->id);
     };
     $touch = function () use($obj) {
         if (!isset($obj->created_at)) {
             $obj->created_at = time();
         }
         $obj->updated_at = time();
         return $obj;
     };
     $duplicate = function () use($obj) {
         if (isset($obj->id)) {
             unset($obj->id);
         }
         if (isset($obj->created_at)) {
             unset($obj->created_at);
         }
         return $obj->save();
     };
     $hydrate = function ($data = array()) use($obj) {
         $data = empty($data) ? $_POST : $data;
         if (Arrays::isAssoc($data)) {
             foreach ($data as $k => $v) {
                 if ('true' == $v) {
                     $v = true;
                 } elseif ('false' == $v) {
                     $v = false;
                 } elseif ('null' == $v) {
                     $v = null;
                 }
                 $obj->{$k} = $v;
             }
         }
         return $obj;
     };
     $date = function ($f) use($obj) {
         return date('Y-m-d H:i:s', $obj->{$f});
     };
     $obj->event('save', $save)->event('delete', $delete)->event('date', $date)->event('exists', $exists)->event('id', $id)->event('db', $database)->event('touch', $touch)->event('hydrate', $hydrate)->event('duplicate', $duplicate);
     $settings = isAke(self::$config, "{$this->db}.{$this->table}");
     $functions = isAke($settings, 'functions');
     if (count($functions)) {
         foreach ($functions as $closureName => $callable) {
             $closureName = lcfirst(Inflector::camelize($closureName));
             $share = function () use($obj, $callable, $db) {
                 $args[] = $obj;
                 $args[] = $db;
                 return call_user_func_array($callable, $args);
             };
             $obj->event($closureName, $share);
         }
     }
     return $this->related($obj);
 }
Ejemplo n.º 16
0
 public function read($name)
 {
     $file = $this->getFile($name);
     if (File::exists($file)) {
         $content = (include $file);
         if (Arrays::isAssoc($content)) {
             return $content;
         }
         return current($content);
     }
     return null;
 }
Ejemplo n.º 17
0
 public function hydrate(array $data = [])
 {
     $data = empty($data) ? $_POST : $data;
     if (Arrays::isAssoc($data)) {
         foreach ($data as $k => $v) {
             if ($k != 'id') {
                 if ('true' == $v) {
                     $v = true;
                 } elseif ('false' == $v) {
                     $v = false;
                 } elseif ('null' == $v) {
                     $v = null;
                 }
                 if (fnmatch('*_id', $k)) {
                     if (is_numeric($v)) {
                         $v = (int) $v;
                     }
                 }
                 $this->_data[$k] = $v;
             }
         }
     }
     return $this;
 }
Ejemplo n.º 18
0
 public function row(array $values)
 {
     $class = $this;
     $obj = new Container();
     $key = $this->key;
     $record = function () use($class, $obj, $key) {
         $tab = $obj->assoc();
         return $class->push($key, $tab);
     };
     $purge = function () use($class, $obj, $key) {
         $tab = $obj->assoc();
         return $class->pop($key, $tab['id']);
     };
     $date = function ($f) use($obj) {
         return date('Y-m-d H:i:s', $obj->{$f});
     };
     $hydrate = function ($data) use($obj) {
         if (Arrays::isAssoc($data)) {
             foreach ($data as $k => $v) {
                 $obj->{$k} = $v;
             }
         }
         return $obj;
     };
     $display = function ($field) use($obj) {
         return \Thin\Html\Helper::display($obj->{$field});
     };
     $tab = function () use($obj) {
         return $obj->assoc();
     };
     $asset = function ($field) use($obj) {
         return '/storage/img/' . $obj->{$field};
     };
     $obj->event('record', $record)->event('purge', $purge)->event('date', $date)->event('hydrate', $hydrate)->event('tab', $tab)->event('asset', $asset)->event('display', $display);
     return $obj->populate($values);
 }
Ejemplo n.º 19
0
 private function closures($obj)
 {
     $settings = isAke(self::$configs, $this->entity);
     $class = $this;
     $class->results = null;
     $class->wheres = null;
     $class->joins = null;
     $class->transactions = null;
     $export = function () use($class, $obj) {
         if (isset($obj->id)) {
             $class->where("id = " . $obj->id)->export();
         }
     };
     $save = function () use($class, $obj) {
         return $class->save($obj->assoc());
     };
     $delete = function () use($class, $obj) {
         return $class->delete($obj->getId());
     };
     $date = function ($f) use($obj) {
         return date('Y-m-d H:i:s', $obj->{$f});
     };
     $hydrate = function ($data = array()) use($obj) {
         $data = empty($data) ? $_POST : $data;
         if (Arrays::isAssoc($data)) {
             foreach ($data as $k => $v) {
                 if ("true" == $v) {
                     $v = true;
                 } elseif ("false" == $v) {
                     $v = false;
                 } elseif ("null" == $v) {
                     $v = null;
                 }
                 $obj->{$k} = $v;
             }
         }
         return $obj;
     };
     $tab = function () use($obj) {
         return $obj->assoc();
     };
     $asset = function ($field) use($obj) {
         return '/storage/img/' . $obj->{$field};
     };
     $string = function () use($obj) {
         return $obj->id;
     };
     $id = function () use($obj) {
         return $obj->getId();
     };
     $exists = function () use($obj) {
         $id = $obj->getId();
         return !is_null($id);
     };
     $obj->event('save', $save)->event('delete', $delete)->event('date', $date)->event('hydrate', $hydrate)->event('tab', $tab)->event('string', $string)->event('export', $export)->event('id', $id)->event('exists', $exists);
     $functions = isAke($settings, 'functions');
     if (count($functions)) {
         foreach ($functions as $closureName => $callable) {
             $closureName = lcfirst(Inflector::camelize($closureName));
             $share = function () use($obj, $callable) {
                 $args = func_get_args();
                 $args[] = $obj;
                 return call_user_func_array($callable, $args);
             };
             $obj->event($closureName, $share);
         }
     }
     return $this->related($obj);
 }
Ejemplo n.º 20
0
 public static function row($type, array $data, $extends = array())
 {
     if (Arrays::isAssoc($data)) {
         $obj = o(sha1(serialize($data)));
         if (count($extends)) {
             foreach ($extends as $name => $instance) {
                 $closure = function ($object) use($name, $instance) {
                     $idx = $object->is_thin_object;
                     $objects = Utils::get('thinObjects');
                     return $instance->{$name}($objects[$idx]);
                 };
                 $obj->_closures[$name] = $closure;
             }
         }
         $settings = Arrays::exists($type, static::$_settings) ? static::$_settings[$type] : array();
         if (count($settings)) {
             $db = Arrays::exists('db', $settings) ? $settings['db'] : null;
             if (!empty($db)) {
                 $methods = array('save', 'delete');
                 foreach ($methods as $method) {
                     if (!Arrays::exists($method, $obj->_closures)) {
                         $closure = function () use($type, $method, $obj, $db) {
                             $name = $method . Inflector::camelize($db);
                             return $obj->{$name}($type);
                         };
                         $obj->_closures[$method] = $closure;
                     }
                 }
             }
         }
         return $obj->populate($data);
     }
     return null;
 }