/** * Get an attribute from the container. * * @param string $key * @param mixed $default * @return mixed */ public function get($key, $default = null) { if (Arrays::exists($key, $this->attributes)) { return $this->attributes[$key]; } return value($default); }
/** * Constructor * * @param array $config */ public function __construct(array $config = []) { // Populate Keyring Keyring::setAppKey($config['AK']); // Application Key Keyring::setAppSecret($config['AS']); // Application Secret Keyring::setConsumerKey($config['CK']); // Consumer Key // Backward compatibility if (Arrays::exists('RG', $config)) { Keyring::setAppUrlRegion($config['RG']); // Region } else { Keyring::setAppUrlRegion("FR"); } if (Arrays::exists('protocol', $config)) { Keyring::setAppHost($config['protocol']); // protocol } else { Keyring::setAppHost(static::$zeliftApiProtocol); } if (Arrays::exists('host', $config)) { Keyring::setAppProtocol($config['host']); // host } else { Keyring::setAppProtocol(static::$zeliftApiHost); } }
/** * Getter * @param mixed $key * @return mixed */ public function __get($key) { if (Arrays::exists($key, $this->data)) { return $this->data[$key]; } else { return false; } }
/** * Get an Instantiated ReflectionClass. * * @param string $class Optional name of a class * @return mixed null or a ReflectionClass instance * @throws Exception if class was not found */ public function get($class = null) { $class = $this->getClass($class); if (Arrays::exists($class, $this->reflections)) { return $this->reflections[$class]; } throw new Exception("Class not found: {$class}"); }
public function remove($id) { if (!Arrays::exists($id, $this->services)) { throw new InvalidArgumentException(sprintf('Service "%s" is not defined.', $id)); } list($prefix, $sid) = $this->getPrefixAndSid($id); if ($prefix) { unset($this->prefixed[$prefix][$sid]); } unset($this->services[$id]); }
public function __construct(array $model) { $this->_model = $model; if (Arrays::is($model)) { if (Arrays::isAssoc($model)) { $this->_settings = isAke($model, 'settings'); if (empty($this->_settings)) { throw new Exception("Settings do not exist."); } if (Arrays::exists('entity', $this->_settings)) { $this->_entity = $this->_settings['entity']; } else { throw new Exception('The model is misconfigured. Entity missing.'); } if (Arrays::exists('table', $this->_settings)) { $this->_table = $this->_settings['table']; } else { throw new Exception('The model is misconfigured. Table missing.'); } $configs = container()->getConfig()->getDb(); $config = isAke($configs, $this->_entity); if (empty($config)) { throw new Exception("Database connection settings do not exist."); } $adapter = $config->getAdapter(); $username = $config->getUsername(); $password = $config->getPassword(); $dbName = $config->getDatabase(); $host = $config->getHost(); $dsn = "{$adapter}:dbname={$dbName};host={$host}"; $this->_keyConnexion = sha1(serialize(array($dsn, $username, $password))); $dbs = container()->getArConnexions(); if (empty($all)) { $dbs = array(); } $db = $dbs[$this->_keyConnexion] = new PDO($dsn, $username, $password); container()->setArConnexions($dbs); $this->_relationships = isAke($this->_settings, 'relationships'); $this->_fields = isAke($this->_settings, 'fields'); if (empty($this->_fields)) { $this->map(); } } else { throw new Exception('The model is misconfigured.'); } } else { throw new Exception('The model is misconfigured.'); } }
/** * Search * * @return array * @param array|string $query * @throws \ElasticSearch\Exception */ public function search($query) { if (Arrays::is($query)) { if (Arrays::exists("query", $query)) { $dsl = new Stringify($query); $q = (string) $dsl; $url = $this->buildUrl(array($this->type, "_search?q=" . $q)); $result = json_decode(redis()->get($url), true); return $result; } throw new \ElasticSearch\Exception("Redis protocol doesn t support the full DSL, only query"); } elseif (is_string($query)) { /** * String based search means http query string search */ $url = $this->buildUrl(array($this->type, '_search?q=' . $query)); $result = json_decode(redis()->get($url), true); return $result; } }
public static function configs($entity, $key, $value = null, $cb = null) { if (!strlen($entity)) { throw new Exception("An entity must be provided to use this method."); } if (!Arrays::exists($entity, static::$configs)) { self::$configs[$entity] = array(); } if (empty($value)) { if (!strlen($key)) { throw new Exception("A key must be provided to use this method."); } return isAke(self::$configs[$entity], $key, null); } if (!strlen($key)) { throw new Exception("A key must be provided to use this method."); } $reverse = strrev($key); $last = $reverse[0]; if ('s' == $last) { self::$configs[$entity][$key] = $value; } else { if (!Arrays::exists($key . 's', self::$configs[$entity])) { self::$configs[$entity][$key . 's'] = array(); } array_push(self::$configs[$entity][$key . 's'], $value); } return !is_callable($cb) ? true : $cb(); }
/** * Resolve all of the dependencies from the ReflectionParameters. * * @param array $parameters * @param array $primitives * @return array */ protected function getDependencies($parameters, array $primitives = array()) { $dependencies = array(); foreach ($parameters as $parameter) { $dependency = $parameter->getClass(); // If the class is null, it means the dependency is a string or some other // primitive type which we can not resolve since it is not a class and // we will just bomb out with an error since we have no-where to go. if (Arrays::exists($parameter->name, $primitives)) { $dependencies[] = $primitives[$parameter->name]; } elseif (is_null($dependency)) { $dependencies[] = $this->resolveNonClass($parameter); } else { $dependencies[] = $this->resolveClass($parameter); } } return (array) $dependencies; }
public function saveLite($type) { $table = $type . 's'; $fields = Arrays::exists($type, Data::$_fields) ? Data::$_fields[$type] : []; $settings = Arrays::exists($type, Data::$_settings) ? Data::$_settings[$type] : []; if (count($fields) && count($settings)) { $dbType = Arrays::exists('db', $settings) ? $settings['db'] : null; $checkId = Arrays::exists('checkId', $settings) ? $settings['checkId'] : 'id'; if (!empty($dbType)) { $data = $this->toArray(); $fields['id'] = []; $fields['date_create'] = []; $id = Arrays::exists('id', $data) ? $data['id'] : Data::getKeyLite($type); $date_create = Arrays::exists('date_create', $data) ? $data['date_create'] : time(); $this->id = $data['id'] = $id; $this->date_create = $data['date_create'] = $date_create; $fields = array_keys($fields); $db = Data::$dbType($type); $q = "SELECT {$checkId} FROM {$table} WHERE {$checkId} = '" . $data[$checkId] . "'"; $res = $db->query($q); if (false === $res->fetchArray()) { $values = []; foreach ($fields as $field) { $values[] = "'" . SQLite3::escapeString($data[$field]) . "'"; } $values = implode(', ', $values); $insert = "INSERT INTO {$table}\n (" . implode(', ', $fields) . ")\n VALUES ({$values})"; $db->exec($insert); } else { $update = "UPDATE {$table} SET "; foreach ($fields as $field) { if ($field != $checkId) { $update .= "{$field} = '" . SQLite3::escapeString($data[$field]) . "', "; } } $update = substr($update, 0, -2); $update .= " WHERE {$checkId} = '" . $data[$checkId] . "'"; $db->exec($update); } } } return $this; }
function option() { $options = null !== container()->getThinOptions() ? container()->getThinOptions() : array(); $args = func_get_args(); if (func_num_args() > 0) { $name = array_shift($args); if (is_null($name)) { $options = array(); return $options; } if (Arrays::is($name)) { $options = array_merge($options, $name); container()->setThinOptions($options); } $nargs = count($args); if ($nargs > 0) { $value = $nargs > 1 ? $args : Arrays::first($args); $options[$name] = value($value); } return Arrays::exists($name, $options) ? $options[$name] : null; } else { container()->setThinOptions(array()); } return container()->getThinOptions(); }
public function upload($field) { $bucket = new Bucket(SITE_NAME, 'http://zelift.com/bucket'); if (Arrays::exists($field, $_FILES)) { $fileupload = $_FILES[$field]['tmp_name']; $fileuploadName = $_FILES[$field]['name']; if (strlen($fileuploadName)) { $tab = explode(".", $fileuploadName); $data = File::read($fileupload); if (!strlen($data)) { return null; } $ext = strtolower(end($tab)); $res = $bucket->data($data, $ext); return $res; } } return false; }
/** * Transform search parameters to string * * @return string * @param mixed $dslSort */ protected function transformDSLSortToString($dslSort) { $string = ""; if (Arrays::is($dslSort)) { foreach ($dslSort as $sort) { if (Arrays::is($sort)) { $field = key($sort); $info = Arrays::first($sort); } else { $field = $sort; } $string .= "&sort=" . $field; if (isset($info)) { if (is_string($info) && $info == "desc") { $string .= ":reverse"; } elseif (Arrays::is($info) && Arrays::exists("reverse", $info) && $info['reverse']) { $string .= ":reverse"; } } } } return $string; }
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; }
public function render() { if (!count($this->_items)) { return '<div class="span4"><div class="alert alert-info"><button type="button" class="close" data-dismiss="alert">×</button>' . $this->_config['noResultMessage'] . '</div></div>'; } $pagination = $this->_config['pagination']; $fields = $this->_config['fields']; $addable = $this->_config['addable']; $viewable = $this->_config['viewable']; $editable = $this->_config['editable']; $deletable = $this->_config['deletable']; $duplicable = $this->_config['duplicable']; if (\Thin\Arrays::exists('export', $this->_config)) { $export = $this->_config['export']; if (count($export)) { $this->_export = $export; } } $order = !strlen($this->_request->getCrudOrder()) ? $this->_config['defaultOrder'] : $this->_request->getCrudOrder(); $orderDirection = !strlen($this->_request->getCrudOrderDirection()) ? $this->_config['defaultOrderDirection'] : $this->_request->getCrudOrderDirection(); $sorted = true === $this->_config['order'] ? 'tablesorter' : ''; $html = '<table class="table table-striped ' . $sorted . ' table-bordered table-condensed">' . NL; $html .= '<thead>' . NL; $html .= '<tr>' . NL; foreach ($fields as $field => $infosField) { if (true === $infosField['onList']) { if (true !== $infosField['sortable']) { $html .= '<th class="no-sorter">' . \Thin\Html\Helper::display($infosField['label']) . '</th>' . NL; } else { if ($field == $order) { $directionJs = 'ASC' == $orderDirection ? 'DESC' : 'ASC'; $js = 'orderGoPage(\'' . $field . '\', \'' . $directionJs . '\');'; $html .= '<th> <div onclick="' . $js . '" class="text-left field-sorting ' . \Thin\Inflector::lower($orderDirection) . '" rel="' . $field . '"> ' . \Thin\Html\Helper::display($infosField['label']) . ' </div> </th>'; } else { $js = 'orderGoPage(\'' . $field . '\', \'ASC\');'; $html .= '<th> <div onclick="' . $js . '" class="text-left field-sorting" rel="' . $field . '"> ' . \Thin\Html\Helper::display($infosField['label']) . ' </div> </th>'; } } } } $html .= '<th class="no-sorter">Actions</th>' . NL; $html .= '</tr>' . NL; $html .= '</thead>' . NL; $html .= '<tbody>' . NL; foreach ($this->_items as $item) { $html .= '<tr>' . NL; foreach ($fields as $field => $infosField) { if (true === $infosField['onList']) { $content = $infosField['content']; $options = \Thin\Arrays::exists('options', $infosField) ? $infosField['options'] : array(); if (empty($options)) { $options = array(); } if (!in_array('nosql', $options)) { $getter = 'get' . \Thin\Inflector::camelize($field); $value = $item->{$getter}(); } else { $value = $content; } if (strstr($content, '##self##') || strstr($content, '##em##') || strstr($content, '##field##') || strstr($content, '##id##')) { $content = repl(array('##self##', '##em##', '##field##', '##id##'), array($value, $this->_em, $field, $item->getId()), $content); $value = \Thin\Crud::internalFunction($content); } if (empty($value)) { $value = ' '; } $html .= '<td>' . \Thin\Html\Helper::display($value) . '</td>' . NL; } } $actions = ''; if (true === $viewable) { $actions .= '<a href="' . \Thin\Crud::getRoute('view', array('id' => $item->getId(), 'entity' => $item->_getEntity(), 'table' => $item->_getTable())) . '"><i title="afficher" class="icon-file"></i></a> '; } if (true === $editable) { $actions .= '<a href="' . \Thin\Crud::getRoute('edit', array('id' => $item->getId(), 'entity' => $item->_getEntity(), 'table' => $item->_getTable())) . '"><i title="éditer" class="icon-edit"></i></a> '; } if (true === $duplicable) { $actions .= '<a href="' . \Thin\Crud::getRoute('duplicate', array('id' => $item->getId(), 'entity' => $item->_getEntity(), 'table' => $item->_getTable())) . '"><i title="dupliquer" class="icon-plus"></i></a> '; } if (true === $deletable) { $actions .= '<a href="#" onclick="if (confirm(\'Confirmez-vous la suppression de cet élément ?\')) document.location.href = \'' . \Thin\Crud::getRoute('delete', array('id' => $item->getId(), 'entity' => $item->_getEntity(), 'table' => $item->_getTable())) . '\';"><i title="supprimer" class="icon-trash"></i></a> '; } $html .= '<td class="col_plus">' . $actions . '</td>' . NL; $html .= '</tr>' . NL; } $html .= '</tbody>' . NL; $html .= '</table>' . NL; return $html; }
/** * Determine if the request contains a given input item key. * * @param string|array $key * @return bool */ public function exists($key) { $keys = Arrays::is($key) ? $key : func_get_args(); $input = $this->all(); foreach ($keys as $value) { if (!Arrays::exists($value, $input)) { return false; } } return true; }
public static function configs($entity, $key, $value = null) { if (!strlen($entity)) { throw new Exception("An entity must be provided to use this method."); } if (!Arrays::exists($entity, static::$configs)) { self::$configs[$entity] = array(); } if (empty($value)) { if (!strlen($key)) { throw new Exception("A key must be provided to use this method."); } return isAke(self::$configs[$entity], $key, null); } if (!strlen($key)) { throw new Exception("A key must be provided to use this method."); } self::$configs[$entity][$key] = $value; }
public function setGrammar($grammar, $replace) { if (!Arrays::exists($grammar, $this->_grammar)) { $this->_grammar[$grammar] = $replace; } return $this; }
/** * Magic Method for determining if a model attribute is set. */ public function __isset($key) { return Arrays::exists($key, $this->attributes) || Arrays::exists($key, $this->ignore); }
private function sort($orderField, $orderDirection, $multiSort = false) { $fieldsEntity = $this->settings['modelFields']; $entity = $this->settings['entity']; $db = $this->prepare(); $this->ids(); $fieldsEntity['date_create'] = array(); $q = "SELECT id FROM {$entity} WHERE id IN ('" . implode("', '", $this->ids) . "') ORDER BY "; if (false === $multiSort) { $type = Arrays::exists('type', $fieldsEntity[$orderField]) ? $fieldsEntity[$orderField]['type'] : null; if ('data' == $type) { $q = repl('SELECT id', 'SELECT ' . $entity . '.id', $q); list($dummy, $foreignTable, $foreignField) = $fieldsEntity[$orderField]['contentList']; $fields = Arrays::exists($foreignTable, Data::$_fields) ? Data::$_fields[$foreignTable] : Data::noConfigFields($foreignTable); $query = "DROP TABLE IF EXISTS {$foreignTable}; CREATE TABLE {$foreignTable} (id INTEGER PRIMARY KEY, date_create"; if (count($fields)) { foreach ($fields as $field => $infos) { $query .= ", {$field}"; } } $query .= ");"; $db->exec($query); $lite = new self($foreignTable); $datas = $lite->all(); foreach ($datas as $object) { $query = "INSERT INTO {$foreignTable} (id, date_create) VALUES ('" . SQLite3::escapeString($object->id) . "', '" . SQLite3::escapeString($object->date_create) . "')"; $db->exec($query); foreach ($fields as $field => $info) { $value = is_object($object->{$field}) ? 'object' : $object->{$field}; $query = "UPDATE {$foreignTable} SET {$field} = '" . SQLite3::escapeString($value) . "' WHERE id = '" . SQLite3::escapeString($object->id) . "'"; $db->exec($query); } } $replace = " LEFT JOIN {$foreignTable} ON {$entity}.{$orderField} = {$foreignTable}.id WHERE {$entity}."; $q = repl(" WHERE ", $replace, $q); $foreignFields = explode(',', $foreignField); for ($i = 0; $i < count($foreignFields); $i++) { $order = $foreignFields[$i]; $q .= "{$foreignTable}.{$order} {$orderDirection}, "; } $q = substr($q, 0, -2); } else { $q .= "{$orderField} {$orderDirection}"; } } else { for ($i = 0; $i < count($orderField); $i++) { $order = $orderField[$i]; if (Arrays::is($orderDirection)) { $direction = isset($orderDirection[$i]) ? $orderDirection[$i] : 'ASC'; } else { $direction = $orderDirection; } $q .= "{$order} {$direction}, "; } $q = substr($q, 0, -2); } $res = $db->query($q); $collection = array(); while ($row = $res->fetchArray()) { array_push($collection, $this->find($row['id'])); } $this->results = $collection; }
public static function dispatch() { static::$_uri = $uri = trim($_SERVER['REQUEST_URI'], static::URI_DELIMITER); $containerRoute = static::containerRoute(); if (null !== $containerRoute) { return; } $defaultRoute = static::defaultRoute(); if (null !== $defaultRoute) { return; } $entities = container()->getEntities(); /* Pages non routées */ if (true === container()->getMultiSite() && !empty($entities) && null === container()->getMapRoutes()) { $url = substr($_SERVER['REQUEST_URI'], 1); $db = new Querydata('page'); $res = $db->where('is_home = ' . getBool('true')->getId())->get(); $home = $db->first($res); $_homeUrl = Cms::__($home->getUrl()); $homeUrl = null !== $_homeUrl ? $_homeUrl : 'home'; $url = !strlen($url) ? $homeUrl : $url; $pages = Cms::getPages(); $cmsRoutes = array(); $lngs = explode(',', cms_option('page_languages')); if (count($pages)) { foreach ($pages as $pageTmp) { if (1 < count($lngs)) { $urlTab = $pageTmp->getUrl(); foreach ($lngs as $lng) { if (ake($lng, $urlTab)) { $cmsRoutes[$urlTab[$lng]] = $pageTmp; } } } else { $cmsRoutes[Cms::__($pageTmp->getUrl())] = $pageTmp; } } } $found = Arrays::exists($url, $cmsRoutes); if (false === $found) { $found = Cms::match($cmsRoutes); } if (true === $found && ake($url, $cmsRoutes)) { $page = $cmsRoutes[$url]; $status = Inflector::lower($page->getStatuspage()->getName()); $dateDepub = $page->getDateOut(); $now = time(); $continue = true; if (strlen($dateDepub)) { list($d, $m, $y) = explode('-', $dateDepub, 3); $dateDepub = "{$y}-{$m}-{$d}"; $dateDepub = new Date($dateDepub); } if ($dateDepub instanceof Date) { $ts = $dateDepub->getTimestamp(); if ($ts < $now) { $page = $cmsRoutes['home']; } } if ('offline' == $status) { $continue = false; } else { if ('online' != $status) { $page = ake($status, $cmsRoutes) ? $cmsRoutes[$status] : $cmsRoutes['home']; } } if (true === $continue) { return static::isCms($page); } } } if (true === container()->getMultiSite()) { $file = APPLICATION_PATH . DS . 'config' . DS . SITE_NAME . DS . 'routes.php'; } else { $file = APPLICATION_PATH . DS . 'config' . DS . 'routes.php'; } if (File::exists($file)) { $configRoutes = (include $file); $routes = $configRoutes['collection']; $routes += null !== container()->getRoutes() ? container()->getRoutes() : array(); foreach ($routes as $route) { if (!$route instanceof Container) { continue; } $path = $route->getPath(); if ($path == $uri) { return static::make($route); } } foreach ($routes as $route) { if (!$route instanceof Container) { continue; } if (!strlen($route->getPath())) { continue; } $matched = static::match($route->getPath()); if (false === $matched) { continue; } else { return static::make($route); } } } else { static::make(container()->getNotFoundRoute()); return; } if (null === container()->getMapRoutes()) { static::is404(); } else { static::make(container()->getNotFoundRoute()); return; } }
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); if (Arrays::is($this->__events)) { if (Arrays::exists($id, $this->__events)) { return call_user_func_array($this->__events[$id], $args); } } } else { throw new Exception("Method '{$method}' is not declared."); } } }
public function order($fieldOrder = 'date_create', $orderDirection = 'ASC', $results = array()) { $res = count($results) ? $results : $this->results; $fields = $this->fields; $fields['id'] = array(); $fields['date_create'] = array(); if (!Arrays::is($fieldOrder)) { if (null !== $fieldOrder && !Arrays::exists($fieldOrder, $fields)) { $fields[$fieldOrder] = array(); } } else { foreach ($fields as $tmpField => $info) { if (null !== $tmpField && !Arrays::exists($tmpField, $fields)) { $fields[$tmpField] = array(); } } } $sort = array(); foreach ($res as $id) { $objectCreated = $this->row($id); foreach ($fields as $key => $infos) { $type = Arrays::exists('type', $fields[$key]) ? $fields[$key]['type'] : null; if ('data' == $type) { list($dummy, $foreignTable, $foreignFieldKey) = $fields[$key]['contentList']; $foreignFields = Arrays::exists($foreignTable, Data::$_fields) ? Data::$_fields[$foreignTable] : Data::noConfigFields($foreignTable); $foreignStorage = new self($foreignTable); $foreignRow = $foreignStorage->find($objectCreated->{$key}); $foreignFieldKeys = explode(',', $foreignFieldKey); $value = ''; for ($i = 0; $i < count($foreignFieldKey); $i++) { $tmpKey = $foreignFieldKey[$i]; $value .= $foreignRow->{$tmpKey} . ' '; } $value = substr($value, 0, -1); } else { $value = isset($objectCreated->{$key}) ? $objectCreated->{$key} : null; } $sort[$key][] = $value; } } $asort = array(); foreach ($sort as $key => $rows) { for ($i = 0; $i < count($rows); $i++) { if (empty(${$key}) || is_string(${$key})) { ${$key} = array(); } $asort[$i][$key] = $rows[$i]; array_push(${$key}, $rows[$i]); } } if (Arrays::is($fieldOrder) && !Arrays::is($orderDirection)) { $t = array(); foreach ($fieldOrder as $tmpField) { array_push($t, $orderDirection); } $orderDirection = $t; } if (Arrays::is($fieldOrder) && Arrays::is($orderDirection)) { if (count($orderDirection) < count($fieldOrder)) { throw new Exception('You must provide the same arguments number of fields sorting and directions sorting.'); } if (count($fieldOrder) == 1) { $fieldOrder = Arrays::first($fieldOrder); if ('ASC' == Inflector::upper(Arrays::first($orderDirection))) { array_multisort(${$fieldOrder}, SORT_ASC, $asort); } else { array_multisort(${$fieldOrder}, SORT_DESC, $asort); } } elseif (count($fieldOrder) > 1) { $params = array(); foreach ($fieldOrder as $k => $tmpField) { $tmpSort = isset($orderDirection[$k]) ? $orderDirection[$k] : 'ASC'; $params[] = ${$tmpField}; $params[] = 'ASC' == $tmpSort ? SORT_ASC : SORT_DESC; } $params[] = $asort; call_user_func_array('array_multisort', $params); } } else { if ('ASC' == Inflector::upper($orderDirection)) { array_multisort(${$fieldOrder}, SORT_ASC, $asort); } else { array_multisort(${$fieldOrder}, SORT_DESC, $asort); } } $collection = array(); foreach ($asort as $key => $row) { $tmpId = $row['id']; array_push($collection, $tmpId); } $this->results = $collection; return $this; }
public function offsetExists($key) { return Arrays::exists($key, $this->_data); }
private function upload($field) { $bucket = container()->bucket(); if (Arrays::exists($field, $_FILES)) { $fileupload = $_FILES[$field]['tmp_name']; $fileuploadName = $_FILES[$field]['name']; if (strlen($fileuploadName)) { $tab = explode(".", $fileuploadName); $data = fgc($fileupload); if (!strlen($data)) { return null; } $ext = Inflector::lower(Arrays::last($tab)); $res = $bucket->data($data, $ext); return $res; } } return null; }
/** * @param $key * @return mixed */ public function __get($key) { if (Arrays::exists($key, $this->attributes)) { return $this->attributes[$key]; } elseif (Arrays::exists($key, $this->ignore)) { return $this->ignore[$key]; } elseif (method_exists($this, $key)) { $query = $this->{$key}(); return $this->ignore[$key] = Arrays::in($this->relating, array('one', 'oneToOne')) ? $query->first() : $query->get(); } }
/** * Eagerly load a 1:1 belonging relationship. * * @param object $relationship * @param array $parents * @param string $relating_key * @param string $include * @return void */ private static function belongs_to($relationship, &$parents, $relating_key, $include) { $keys = array(); foreach ($parents as &$parent) { $keys[] = $parent->{$relating_key}; } $children = $relationship->where_in(Model::pk(get_class($relationship)), array_unique($keys))->get(); foreach ($parents as &$parent) { if (Arrays::exists($parent->{$relating_key}, $children)) { $parent->ignore[$include] = $children[$parent->{$relating_key}]; } } }
/** * Merge another Setting with this one. * * For duplicate keys, the following will be performed: * - Nested Settings will be recursively merged. * - Items in $merge with INTEGER keys will be appended. * - Items in $merge with STRING keys will overwrite current values. * * @param Setting $merge * @return Setting */ public function merge(Setting $merge) { /** @var Setting $value */ foreach ($merge as $key => $value) { if (Arrays::exists($key, $this->data)) { if (is_int($key)) { $this->data[] = $value; } elseif ($value instanceof self && $this->data[$key] instanceof self) { $this->data[$key]->merge($value); } else { if ($value instanceof self) { $this->data[$key] = new static($value->toArray(), $this->allowModifications); } else { $this->data[$key] = $value; } } } else { if ($value instanceof self) { $this->data[$key] = new static($value->toArray(), $this->allowModifications); } else { $this->data[$key] = $value; } $this->count++; } } return $this; }