Ejemplo n.º 1
0
 public function make($data)
 {
     $data = is_object($data) && $data instanceof Container ? $data->assoc() : $data;
     if (!Arrays::isAssoc($data)) {
         throw new Exception("This method needs a valid object to process.");
     }
     $e = $data['entity'] = $this->entity;
     $class = $this;
     $obj = new Container();
     $store = function () use($obj, $e) {
         $class = new Attributes($e);
         return $class->save($obj);
     };
     $remove = function () use($obj, $e) {
         $class = new Attributes($e);
         return $class->delete($obj);
     };
     $date = function ($f) use($obj) {
         return date('Y-m-d H:i:s', $obj->{$f});
     };
     $display = function ($f, $echo = true) use($obj) {
         if (false === $echo) {
             return Html\Helper::display($obj->{$f});
         } else {
             echo Html\Helper::display($obj->{$f});
         }
     };
     $obj->event('store', $store)->event('trash', $remove)->event('date', $date)->event('display', $display);
     foreach ($data as $k => $v) {
         if (!isset($obj->{$k})) {
             $obj->{$k} = $v;
         }
     }
     return $obj;
 }
Ejemplo n.º 2
0
 public static function createObject($className, $classParams = null)
 {
     if (is_array($classParams) && !Arrays::isAssoc($classParams)) {
         // Invoke Object with multiple arguments
         return new $className(...$classParams);
     }
     // Invoke Object with single argument
     return new $className($classParams);
 }
Ejemplo n.º 3
0
 public function __construct(array $array, $callback = null)
 {
     $path = CACHE_PATH . DS . Utils::UUID();
     File::mkdir($path);
     File::put($file, "<?php\nreturn " . var_export($array, 1) . ';');
     $this->assoc = Arrays::isAssoc($array);
     $this->file = $file;
     $this->callback = $callback;
     $this->count = count($array);
 }
Ejemplo n.º 4
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;
     $extend = function ($name, $callable) use($obj) {
         if (is_callable($callable)) {
             $share = function () use($obj, $callable) {
                 $args = func_get_args();
                 $args[] = $obj;
                 return call_user_func_array($callable, $args);
             };
             $obj->event($name, $share);
         }
     };
     $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;
         }
     };
     $tab = function () use($obj) {
         return $obj->assoc();
     };
     $asset = function ($field) use($obj) {
         return '/storage/img/' . $obj->{$field};
     };
     $string = function () use($obj) {
         return $obj->id;
     };
     $obj->event('save', $save)->event('delete', $delete)->event('extend', $extend)->event('date', $date)->event('hydrate', $hydrate)->event('tab', $tab)->event('string', $string)->event('export', $export)->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);
         }
     }
     return $obj;
 }
Ejemplo n.º 5
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) 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};
     };
     $extend = function ($n, \Closure $f) use($obj) {
         if (version_compare(PHP_VERSION, '5.4.0', "<")) {
             $share = function () use($obj, $f) {
                 return $f($obj);
             };
         } else {
             $share = $f->bindTo($obj);
         }
         $obj->event($n, $share);
     };
     $obj->event('save', $save)->event('delete', $delete)->event('extend', $extend)->event('date', $date)->event('hydrate', $hydrate)->event('tab', $tab)->event('export', $export)->event('display', $display);
     $functions = isAke($settings, 'functions');
     if (count($functions)) {
         foreach ($functions as $closureName => $callable) {
             $closureName = lcfirst(Inflector::camelize($closureName));
             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.º 6
0
 public function row(array $values)
 {
     $class = $this;
     $class->results = null;
     $class->wheres = null;
     $obj = new Container();
     $save = function () use($class, $obj) {
         return $class->push($obj->assoc());
     };
     $delete = function () use($class, $obj) {
         return $class->pop($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) use($obj) {
         return Html\Helper::display($obj->{$field});
     };
     $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);
     return $obj->populate($values);
 }
Ejemplo n.º 7
0
 public static function getFullPrice()
 {
     $ret = 0;
     $items = null === static::$session->getItems() ? array() : static::$session->getItems();
     if (count($items)) {
         foreach ($items as $item) {
             if (Arrays::isAssoc($item)) {
                 $quantity = isAke($item, 'quantity');
                 $price = isAke($itemd, 'price');
                 if (!empty($quantity) && !empty($price)) {
                     $ret += $item['price'] * $item['quantity'];
                 }
             }
         }
     }
     return $ret;
 }
Ejemplo n.º 8
0
 /**
  * Sets up the TinyQueries environment
  *
  */
 public function setup()
 {
     $setupFile = dirname(__FILE__) . '/../config/setup.php';
     // Don't throw error, but just return informative message
     if (!file_exists($setupFile)) {
         return array('message' => 'Nothing to do - No setup script found');
     }
     include $setupFile;
     if (!function_exists('TinyQueries\\setup')) {
         throw new \Exception('There is no function \'TinyQueries\\setup\' defined in setup.php');
     }
     $result = setup();
     if (Arrays::isAssoc($result)) {
         return $result;
     }
     return array('message' => 'TinyQueries setup complete');
 }
Ejemplo n.º 9
0
 private function closures($obj)
 {
     $db = $this->model;
     $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, $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 ($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(Db::$config, "{$this->model}->db.{$this->model}->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);
 }