private function format($text = '', $parameters = array()) { if (!Arrays::is($parameters) && 'NONE' == $parameters) { return $text; } if (!Arrays::is($parameters) && isset($this->styles[$parameters])) { $parameters = $this->styles[$parameters]; } $codes = array(); $fg = isAke($parameters, 'fg', null); $bg = isAke($parameters, 'bg', null); if (!empty($fg)) { $codes[] = $this->foreground[$fg]; } if (!empty($bg)) { $codes[] = $this->background[$bg]; } foreach ($this->options as $option => $value) { $paramOpt = isAke($parameters, $option, null); if (!empty($paramOpt)) { $codes[] = $value; } } return "[" . implode(';', $codes) . 'm' . $text . "[0m"; }
public function orderBy($tab, $fieldOrder, $orderDirection = 'ASC') { $sortFunc = function ($key, $direction) { return function ($a, $b) use($key, $direction) { if ('ASC' == $direction) { return $a[$key] > $b[$key]; } else { return $a[$key] < $b[$key]; } }; }; 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)) { $orderDirection = Arrays::first($orderDirection); } if (Arrays::is($fieldOrder) && Arrays::is($orderDirection)) { for ($i = 0; $i < count($fieldOrder); $i++) { usort($tab, $sortFunc($fieldOrder[$i], $orderDirection[$i])); } } else { usort($tab, $sortFunc($fieldOrder, $orderDirection)); } return $tab; }
public function keys($pattern) { $keys = $this->session->getKeys(); if (!empty($keys)) { $seg = isAke($keys, sha1($pattern)); if (!empty($seg)) { return $seg; } } $this->call('keys', array("pattern" => $pattern)); $tab = json_decode($this->response, true); $res = isAke($tab, 'message'); $collection = array(); if (Arrays::is($res)) { if (count($res)) { foreach ($res as $row) { array_push($collection, $row); } } } if (empty($keys)) { $keys = array(); } $keys[sha1($pattern)] = $collection; $this->session->setKeys($keys); return $collection; }
public function __call($method, $args) { if (true === $this->__has($method)) { return $this->__fire($method, $args); } $reverse = strrev($method); $last = $reverse[0]; if ('s' == $last) { if (!count($args)) { return isAke($this->values, $method); } else { $this->values[$method] = !Arrays::is($this->values[$method]) ? array() : $this->values[$method]; foreach ($args as $arg) { array_push($this->values[$method], $arg); } } return $this; } else { $method .= 's'; if (!count($args)) { $val = isAke($this->values, $method); return count($val) ? Arrays::first($val) : null; } else { $this->values[$method] = !Arrays::is($this->values[$method]) ? array() : $this->values[$method]; foreach ($args as $arg) { array_push($this->values[$method], $arg); } } return $this; } }
public static function send($arguments = array()) { $response = static::request('messages/send', $arguments); if (false !== $response && Arrays::is($response)) { $response = Arrays::first($response); return $response['status'] == 'sent' ? true : $response['status']; } return false; }
public function getHeader() { $this->rewind(); $current = $this->current(); if (Arrays::is($current)) { return $current; } return array(); }
public function make(array $array) { $return = array(); foreach ($array as $k => $v) { if (Arrays::is($v)) { $o = new self(); $return[$k] = $o->populate($v); } else { $return[$k] = $v; } } return $return; }
public static function clean($data) { if (Arrays::is($data)) { foreach ($data as $key => $value) { unset($data[$key]); $data[static::clean($key)] = static::clean($value); } } else { if (ini_get('magic_quotes_gpc')) { $data = stripslashes($data); } else { $data = htmlspecialchars($data, ENT_QUOTES, 'UTF-8'); } } return $data; }
/** * Add file to the archive * * @param array/string $files * @param string $string * @return bool */ public function add($files, $string = false) { if (true === $this->_enabled) { if (Arrays::is($files)) { foreach ($files as $file) { $this->_addFile((string) $file); } } else { if (!empty($string) && is_string($string)) { return $this->_zip->addFromString($files, $string); } else { return $this->_addFile($files); } } } return false; }
public function keys($pattern) { $collection = $this->data($pattern); if (!Arrays::is($collection)) { $oldPattern = $pattern; $pattern .= '*#*'; $pattern = repl('**', '*', $pattern); $files = $this->glob($this->dir . DS . $pattern); $collection = array(); if (count($files)) { foreach ($files as $row) { array_push($collection, $row); } } $this->setData($oldPattern, $collection); } return $collection; }
private static function dispatch() { $uri = str_replace(Config::get('application.base_uri'), '', isAke($_SERVER, 'REQUEST_URI', '/')); $method = strtolower(isAke($_SERVER, 'REQUEST_METHOD', 'get')); $routes = (include CONFIG_PATH . DS . 'routes.php'); if (!empty($routes)) { foreach ($routes as $route) { $methodRoute = !isset($route->method) ? 'get' : strtolower($route->method); if ($route->path == $uri && $methodRoute == $method) { return $route; } $match = static::match($route->path, $uri); if (false !== $match && $methodRoute == $method) { if (!empty($match)) { $args = $route->args; if (Arrays::is($args)) { if (count($match) == count($args)) { $continue = false; $i = 1; foreach ($args as $key => $closure) { $val = $closure($match[$i]); if (false === $val) { $continue = true; break; } $_REQUEST[$key] = $val; $i++; } if (true === $continue) { continue; } } } } return $route; } } } return static::route(['controller' => 'static', 'action' => 'is404']); }
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::is(self::$configs[$entity][$key . 's'])) { self::$configs[$entity][$key . 's'] = array(); } array_push(self::$configs[$entity][$key . 's'], $value); } return !is_callable($cb) ? true : $cb(); }
public static function mime($extension, $default = 'application/octet-stream') { Config::load('mimes'); $mimes = null !== config::get('mimes') ? config::get('mimes') : []; if (!Arrays::exists($extension, $mimes)) { return $default; } return Arrays::is($mimes[$extension]) ? Arrays::first($mimes[$extension]) : $mimes[$extension]; }
/** * Check if the array is a multidimensional array * * @param Array $array The array to check * @return boolean */ protected function isMulti(array $array) { $first = array_shift($array); return Arrays::is($first); }
/** * Merges any number of arrays of any dimensions, the later overwriting * previous keys, unless the key is numeric, in whitch case, duplicated * values will not be added. * * The arrays to be merged are passed as arguments to the function. * * @return array Resulting array, once all have been merged */ public function arrayMergeRecursiveReplace() { // Holds all the arrays passed $params = func_get_args(); // First array is used as the base, everything else overwrites on it $return = array_shift($params); // Merge all arrays on the first array foreach ($params as $array) { foreach ($array as $key => $value) { // Numeric keyed values are added (unless already there) if (is_numeric($key) && !in_array($value, $return)) { if (Arrays::is($value)) { $return[] = $this->arrayMergeRecursiveReplace($return[$key], $value); } else { $return[] = $value; } // String keyed values are replaced } else { if (isset($return[$key]) && Arrays::is($value) && Arrays::is($return[$key])) { $return[$key] = $this->arrayMergeRecursiveReplace($return[$key], $value); } else { $return[$key] = $value; } } } } return $return; }
/** * Remove an index of the keys in a collection. To set values to descending order, * you must pass values of either -1, false, 'desc', or 'DESC', else they will be * set to 1 (ASC). * * @param string $collection the collection name * @param array $keys an associative array of keys, array(field => direction) * @usage $mongodb->removeIndex($collection, array('first_name' => 'ASC', 'last_name' => -1)); */ public function removeIndex($collection = '', $keys = array()) { if (empty($collection)) { throw new Exception("No Mongo collection specified to remove an index from"); } if (empty($keys) || !Arrays::is($keys)) { throw new Exception("Index could not be removed from MongoDB Collection because no keys were specified"); } if ($this->db->{$collection}->deleteIndex($keys) == true) { $this->_clear(); return $this; } else { throw new Exception("An error occured when trying to remove an index from MongoDB Collection"); } }
public static function mergeOptions(array $array1, $array2 = null) { if (Arrays::is($array2)) { foreach ($array2 as $key => $val) { if (Arrays::is($array2[$key])) { $array1[$key] = Arrays::exists($key, $array1) && Arrays::isArray($array1[$key]) ? static::mergeOptions($array1[$key], $array2[$key]) : $array2[$key]; } else { $array1[$key] = $val; } } } return $array1; }
public function order($fieldOrder, $orderDirection = 'ASC', $results = array()) { $res = count($results) ? $results : $this->results; if (empty($res)) { return $this; } $sortFunc = function ($key, $direction) { return function ($a, $b) use($key, $direction) { if ('ASC' == $direction) { return $a[$key] > $b[$key]; } else { return $a[$key] < $b[$key]; } }; }; 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)) { $orderDirection = Arrays::first($orderDirection); } if (Arrays::is($fieldOrder) && Arrays::is($orderDirection)) { for ($i = 0; $i < count($fieldOrder); $i++) { usort($res, $sortFunc($fieldOrder[$i], $orderDirection[$i])); } } else { usort($res, $sortFunc($fieldOrder, $orderDirection)); } $this->results = $res; return $this; }
public function order($fieldOrder = 'date_create', $orderDirection = 'ASC', $results = array()) { $res = count($results) ? $results : $this->currrentRes(); if (empty($res)) { return $this; } $data = $this->makeData($res); $fields = array_keys(Arrays::first($data)->assoc()); $sort = array(); foreach ($data as $objectCreated) { foreach ($fields as $key) { $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; }
private function values($row) { if (is_object($row)) { if ($row instanceof Container) { $row = $this->toArray($row); } } if (Arrays::is($row)) { return array_values($row); } return array(); }
/** Resolve a single dependency with an collections */ private function _lookupRecursive($item) { if (Arrays::is($item)) { $collection = array(); foreach ($item as $k => $v) { $collection[$k] = $this->_lookupRecursive($v); } return $collection; } else { return $this->lookup($item); } }
public static function lng($value, $lng = null) { if (null === $lng) { $lng = container()->getCmsLanguage(); if (null === $lng) { $lng = static::getOption('default_language'); } } if (Arrays::is($value)) { if (Arrays::exists($lng, $value)) { return $value[$lng]; } } return ''; }
/** * Fire an event and call the listeners. * * @param string $event * @param mixed $payload * @param bool $halt * @return array|null */ public function fire($event, $payload = array(), $halt = false) { $responses = array(); // If an array is not given to us as the payload, we will turn it into one so // we can easily use call_user_func_array on the listeners, passing in the // payload to each of them so that they receive each of these arguments. if (!Arrays::is($payload)) { $payload = array($payload); } $this->firing[] = $event; foreach ($this->getListeners($event) as $listener) { $response = call_user_func_array($listener, $payload); // If a response is returned from the listener and event halting is enabled // we will just return this response, and not call the rest of the event // listeners. Otherwise we will add the response on the response list. if (!is_null($response) && $halt) { array_pop($this->firing); return $response; } // If a boolean false is returned from a listener, we will stop propagating // the event to any further listeners down in the chain, else we keep on // looping through the listeners and firing every one in our sequence. if ($response === false) { break; } $responses[] = $response; } array_pop($this->firing); return $halt ? null : $responses; }
public static function substr($str, $start, $length = false, $encoding = 'utf-8') { if (Arrays::is($str)) { return false; } if (function_exists('mb_substr')) { return mb_substr($str, (int) $start, $length === false ? static::length($str) : (int) $length, $encoding); } return substr($str, $start, $length === false ? static::length($str) : (int) $length); }
public function backup() { $newline = NL; $tables = $this->_query('SHOW TABLES'); if (Arrays::is($tables)) { $count = count($tables); } else { $count = $tables->rowCount(); } if (false === $tables) { throw new Exception("This database {$this->_entity} contains no table."); } $output = ''; foreach ($tables as $table) { $table = current($table); $queryTable = "SHOW CREATE TABLE `" . $this->_dbName . '`.`' . $table . '`'; $res = $this->_query($queryTable); if (is_array($res)) { $count = count($res); } else { $count = $res->rowCount(); } if (false === $res || 1 > $count) { continue; } $res = Arrays::first($res); $create = $res[1]; $output .= '#' . $newline . '# TABLE STRUCTURE FOR: ' . $table . $newline . '#' . $newline . $newline; $output .= 'DROP TABLE IF EXISTS ' . $table . ';' . $newline . $newline; $output .= $create . ';' . $newline . $newline; $res = $this->_query("SELECT * FROM {$table}"); if (Arrays::is($res)) { $count = count($res); } else { $count = $res->rowCount(); } if (false === $res || 1 > $count) { continue; } foreach ($res as $row) { $fields = array(); $values = array(); foreach ($row as $key => $value) { if (!is_numeric($key)) { $fields[] = $key; $values[] = "'" . addslashes($value) . "'"; } } $output .= 'INSERT INTO ' . $table . ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ');' . $newline; } $output .= $newline; } return $output; }
/** * Make a string version of a value. * * @param mixed $value * @return string */ private static function stringify($value) { if (is_bool($value)) { return $value ? '<TRUE>' : '<FALSE>'; } if (is_scalar($value)) { $val = (string) $value; if (strlen($val) > 100) { $val = substr($val, 0, 97) . '...'; } return $val; } if (Arrays::is($value)) { return '<ARRAY>'; } if (is_object($value)) { return get_class($value); } if (is_resource($value)) { return '<RESOURCE>'; } if ($value === null) { return '<NULL>'; } return 'unknown'; }
/** * Check the HTTP headers for signs of mobile. * This is the fastest mobile check possible; it's used * inside isMobile() method. * * @return bool */ public function checkHttpHeadersForMobile() { foreach ($this->getMobileHeaders() as $mobileHeader => $matchType) { if (isset($this->httpHeaders[$mobileHeader])) { if (Arrays::is($matchType['matches'])) { foreach ($matchType['matches'] as $_match) { if (strpos($this->httpHeaders[$mobileHeader], $_match) !== false) { return true; } } return false; } else { return true; } } } return false; }
private static function listing($type, $list, $attributes = array()) { $html = ''; if (count($list) == 0) { return $html; } foreach ($list as $key => $value) { // If the value is an array, we will recurse the function so that we can // produce a nested list within the list being built. Of course, nested // lists may exist within nested lists, etc. if (Arrays::is($value)) { if (is_int($key)) { $html .= static::listing($type, $value); } else { $html .= '<li>' . $key . static::listing($type, $value) . '</li>'; } } else { $html .= '<li>' . static::entities($value) . '</li>'; } } return '<' . $type . static::attributes($attributes) . '>' . $html . '</' . $type . '>'; }
public function order($fieldOrder, $orderDirection = 'ASC', $results = array()) { $res = count($results) ? $results : $this->results; if (empty($res)) { return $this; } $keyCache = sha1('RDB_order_' . serialize($fieldOrder) . serialize($orderDirection) . serialize($res) . $this->entity); $cached = $this->cached($keyCache); if (empty($cached)) { $settings = isAke(self::$configs, $this->entity); $_fields = isAke($settings, 'fields'); $fields = empty($_fields) ? array_keys(Arrays::first($res)) : $_fields; $sort = array(); foreach ($res as $i => $tab) { foreach ($fields as $k) { $value = isAke($tab, $k, null); $sort[$k][] = $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) { array_push($collection, $row); } $this->cached($keyCache, $collection); } else { $collection = $cached; } $this->results = $collection; return $this; }
static $i = array(); $db = isAke($i, $entity, null); if (is_null($db)) { $i[$entity] = $db = new Nodedb($ns, $entity); } return $db; } }); context('url')->to(function ($name, $args = array()) { $routes = container()->getRoutes(); foreach ($routes as $route) { $nameRoute = $route->name; if (!is_null($nameRoute)) { if ($nameRoute == $name) { $path = $route->path; $args = $args instanceof Container ? $args->assoc() : !Arrays::is($args) ? array() : $args; if (count($args)) { $max = count($args); for ($i = 1; $i <= $max; $i++) { $keyParam = "param{$i}"; $param = $route->{$keyParam}; $val = isAke($args, $param, 'undefined'); $path = strReplaceFirst('(.*)', $val, $path); } } return trim(urldecode(URLSITE), '/') . $path; } } } return urldecode(URLSITE); });