/** * @param string|array $args * @return string */ private function prepareCmdArgs($args) { if (!Arrays::isArray($args)) { return (string) $args; } $result = ''; foreach ($args as $arg => $value) { if (!is_int($arg)) { $arg = (string) $arg; $result .= " -{$arg}"; } $value = (string) $value; $result .= " {$value}"; } return trim($result); }
public function query($condition) { $this->firstQuery = false; Data::_incQueries(Data::_getTime()); $queryKey = sha1(serialize($condition) . 'QueryData'); $cache = Data::cache($this->type, $queryKey); if (!empty($cache) && true === $this->cache) { return $cache; } $this->wheres[] = $condition; if (is_string($condition)) { $res = Data::query($this->type, $condition); $collection = array(); if (count($res)) { foreach ($res as $row) { if (is_string($row)) { $tab = explode(DS, $row); $id = repl(".data", '', Arrays::last($tab)); } else { if (is_object($row)) { $id = $row->id; } } $collection[] = $id; } } } else { if (Arrays::isArray($condition)) { $collection = $condition; } else { $collection = array(); } } $cache = Data::cache($this->type, $queryKey, $collection); return $collection; }
public function isRoute($routeName) { $routes = container()->getMapRoutes(); if (Arrays::isArray($routes)) { if (ake($routeName, $routes)) { $route = $routes[$routeName]; $actualRoute = $this->getRoute(); return $actualRoute === $route; } } return false; }
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; }
/** * Format argument in called method * * @param mixed $arg */ protected static function _formatCalledArgument($arg) { $out = ''; if (is_object($arg)) { $out .= sprintf("&%s#%s#", get_class($arg), spl_object_hash($arg)); } else { if (is_resource($arg)) { $out .= '#[' . get_resource_type($arg) . ']'; } else { if (Arrays::isArray($arg)) { $isAssociative = false; $args = array(); foreach ($arg as $k => $v) { if (!is_numeric($k)) { $isAssociative = true; } $args[$k] = static::_formatCalledArgument($v); } if ($isAssociative) { $arr = array(); foreach ($args as $k => $v) { $arr[] = static::_formatCalledArgument($k) . ' => ' . $v; } $out .= 'array(' . join(', ', $arr) . ')'; } else { $out .= 'array(' . join(', ', $args) . ')'; } } else { if (is_null($arg)) { $out .= 'NULL'; } else { if (is_numeric($arg) || is_float($arg)) { $out .= $arg; } else { if (is_string($arg)) { if (strlen($arg) > static::$argLength) { $arg = substr($arg, 0, static::$argLength) . "..."; } $arg = strtr($arg, array("\t" => '\\t', "\r" => '\\r', "\n" => '\\n', "'" => '\\\'')); $out .= "'" . $arg . "'"; } else { if (is_bool($arg)) { $out .= $arg === true ? 'true' : 'false'; } } } } } } } return $out; }
/** * Process a key. * * @param string $key * @param string $value * @param array $config * @return array * @throws Exception */ protected function processKey($key, $value, array &$config) { if (strpos($key, $this->nestSeparator) !== false) { $pieces = explode($this->nestSeparator, $key, 2); if (!strlen($pieces[0]) || !strlen($pieces[1])) { throw new Exception(sprintf('Invalid key "%s"', $key)); } elseif (!isset($config[$pieces[0]])) { if ($pieces[0] === '0' && !empty($config)) { $config = array($pieces[0] => $config); } else { $config[$pieces[0]] = array(); } } elseif (!Arrays::isArray($config[$pieces[0]])) { throw new Exception(sprintf('Cannot create sub-key for "%s", as key already exists', $pieces[0])); } $this->processKey($pieces[1], $value, $config[$pieces[0]]); } else { if ($key === '@include') { if ($this->directory === null) { throw new Exception('Cannot process @include statement for a string config'); } $reader = clone $this; $include = $reader->fromFile($this->directory . '/' . $value); $config = array_replace_recursive($config, $include); } else { $config[$key] = $value; } } }
/** * Update the quantity of one row of the cart * * @param string $rowId The rowid of the item you want to update * @param integer|Array $attribute New quantity of the item|Array of attributes to update * @return boolean */ public function update($rowId, $attribute) { if (!$this->hasRowId($rowId)) { throw new Exception('This cart does not contain this row.'); } if (Arrays::isArray($attribute)) { return $this->updateAttribute($rowId, $attribute); } return $this->updateQty($rowId, $attribute); }
public static function query($entity, $conditions = '', $offset = 0, $limit = 0, $orderField = null, $orderDirection = 'ASC') { $dataClass = static::$_dataClass; static::_exec('_incQueries', array(static::_exec('_getTime', array()))); $queryKey = sha1(serialize(func_get_args())); if (true === $dataClass::$_buffer) { $buffer = static::_exec('_buffer', array($queryKey)); if (false !== $buffer) { return $buffer; } } $results = array(); $resultsAnd = array(); $resultsOr = array(); $resultsXor = array(); $fields = static::fields($entity); if (!Arrays::isArray($orderField)) { if (null !== $orderField && !ake($orderField, $fields)) { $fields[$orderField] = array(); } } else { foreach ($orderField as $tmpField) { if (null !== $tmpField && !ake($tmpField, $fields)) { $fields[$tmpField] = array(); } } } $datas = static::getAll($entity); if (!count($datas)) { return $results; } if (!strlen($conditions)) { $conditionsAnd = array(); $conditionsOr = array(); $conditionsXor = array(); $results = $datas; } else { $conditionsAnd = explode(' && ', $conditions); $conditionsOr = explode(' || ', $conditions); $conditionsXor = explode(' XOR ', $conditions); } if (count($conditionsOr) == count($conditionsAnd)) { if (current($conditionsOr) == current($conditionsAnd)) { $conditionsAnd = array(); } } if (count($conditionsXor) == count($conditionsOr)) { if (current($conditionsXor) == current($conditionsOr)) { $conditionsXor = array(); } } if (count($conditionsAnd)) { foreach ($conditionsAnd as $condition) { foreach ($datas as $tmpObject) { $object = static::_exec('getObject', array($tmpObject, 'eavrecord')); if (!is_object($object)) { continue; } $object = $object->getData()->setId($object->getId()); $condition = repl('NOT LIKE', 'NOTLIKE', $condition); $condition = repl('NOT IN', 'NOTIN', $condition); list($field, $op, $value) = explode(' ', $condition, 3); $continue = true; if (null !== $object->{$field}) { $continue = static::_exec('analyze', array($object->{$field}, $op, $value)); } else { $continue = false; } if (true === $continue) { if (!count($resultsAnd)) { array_push($resultsAnd, $tmpObject); } else { $tmpResult = array($tmpObject); $resultsAnd = array_intersect($resultsAnd, $tmpResult); } } } } if (!count($results)) { $results = $resultsAnd; } else { $results = array_intersect($results, $resultsAnd); } } if (count($conditionsOr)) { foreach ($conditionsOr as $condition) { foreach ($datas as $tmpObject) { $object = static::_exec('getObject', array($tmpObject, 'eavrecord')); if (!is_object($object)) { continue; } $object = $object->getData()->setId($object->getId()); $condition = repl('NOT LIKE', 'NOTLIKE', $condition); $condition = repl('NOT IN', 'NOTIN', $condition); list($field, $op, $value) = explode(' ', $condition, 3); if (!isset($object->{$field})) { $continue = false; } else { if (null !== $object->{$field}) { $continue = static::_exec('analyze', array($object->{$field}, $op, $value)); } else { $continue = false; } } if (true === $continue) { if (!count($resultsOr)) { array_push($resultsOr, $tmpObject); } else { $tmpResult = array($tmpObject); $resultsOr = array_merge($resultsOr, $tmpResult); } } } } if (!count($results)) { $results = $resultsOr; } else { $results = array_merge($results, $resultsOr); } } if (count($conditionsXor)) { foreach ($conditionsXor as $condition) { foreach ($datas as $tmpObject) { $object = static::_exec('getObject', array($tmpObject, 'eavrecord')); if (!is_object($object)) { continue; } $object = $object->getData()->setId($object->getId()); $condition = repl('NOT LIKE', 'NOTLIKE', $condition); $condition = repl('NOT IN', 'NOTIN', $condition); list($field, $op, $value) = explode(' ', $condition, 3); $continue = true; if (null !== $object->{$field}) { $continue = static::_exec('analyze', array($object->{$field}, $op, $value)); } else { $continue = false; } if (true === $continue) { if (!count($resultsXor)) { array_push($resultsXor, $tmpObject); } else { $tmpResult = array($tmpObject); $resultsXor = array_merge(array_diff($resultsXor, $tmpResult), array_diff($tmpResult, $resultsXor)); } } } } if (!count($results)) { $results = $resultsXor; } else { $results = array_merge(array_diff($results, $resultsXor), array_diff($resultsXor, $results)); } } if (count($results)) { if (0 < $limit) { $max = count($results); $number = $limit - $offset; if ($number > $max) { $offset = $max - $limit; if (0 > $offset) { $offset = 0; } $limit = $max; } $results = array_slice($results, $offset, $limit); } } if (count($results) && null !== $orderField) { if (Arrays::isArray($orderField)) { $orderFields = $orderField; } else { $orderFields = array($orderField); } foreach ($orderFields as $orderField) { $sort = array(); foreach ($results as $object) { $objectCreated = static::_exec('getObject', array($object, 'eavrecord')); foreach ($fields as $key => $infos) { $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 ('ASC' == Inflector::upper($orderDirection)) { array_multisort(${$orderField}, SORT_ASC, $asort); } else { array_multisort(${$orderField}, SORT_DESC, $asort); } $collection = array(); foreach ($asort as $key => $row) { $tmpId = $row['id']; $tmpObject = static::getById($tmpId); array_push($collection, $tmpObject); } $results = $collection; } } if (true === $dataClass::$_buffer) { static::_exec('_buffer', array($queryKey, $results)); } return $results; }
/** * 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::isArray($value)) { return '<ARRAY>'; } if (is_object($value)) { return get_class($value); } if (is_resource($value)) { return '<RESOURCE>'; } if ($value === null) { return '<NULL>'; } return 'unknown'; }
/** * Filter empty values to be treated as nulls * * @param mixed $keys * @param array $data * * @return array */ public static function nulls(array $data, $keys = null) { if ($keys === null) { $keys = array_keys($data); } if (!Arrays::isArray($keys)) { $keys = array($keys); } foreach ($keys as $key) { if (array_key_exists($key, $data) && empty($data[$key])) { $data[$key] = null; } } return $data; }
public function getConfigData($key) { if (null === $this->_classConfigs) { $file = APPLICATION_PATH . DS . 'config' . DS . 'moneris.php'; if (File::exists($file)) { $this->_classConfigs = (include $file); } } if (Arrays::isArray($this->_classConfigs)) { if (Arrays::exists($key, $this->_classConfigs)) { return $this->_classConfigs[$key]; } } return null; }
function varIsObject($var) { $var_ser = serialize($var); array_push($this->arrHistory, $var_ser); $this->makeTableHeader("object", "object"); if (is_object($var)) { $arrObjVars = get_object_vars($var); foreach ($arrObjVars as $key => $value) { $value = !is_object($value) && !Arrays::isArray($value) && trim($value) == "" ? "[empty string]" : $value; $this->makeTDHeader("object", $key); //check for recursion if (is_object($value) || Arrays::isArray($value)) { $var_ser = serialize($value); if (Arrays::inArray($var_ser, $this->arrHistory, TRUE)) { $value = is_object($value) ? "*RECURSION* -> \$" . get_class($value) : "*RECURSION*"; } } if (Arrays::inArray(gettype($value), $this->arrType)) { $this->checkType($value); } else { echo $value; } echo $this->closeTDRow(); } $arrObjMethods = get_class_methods(get_class($var)); foreach ($arrObjMethods as $key => $value) { $this->makeTDHeader("object", $value); echo "[function]" . $this->closeTDRow(); } } else { echo "<tr><td>" . $this->error("object") . $this->closeTDRow(); } array_pop($this->arrHistory); echo "</table><hr class=thinDebugHr />"; }
public static function __($value, $lng = null) { if (null === $lng) { $lng = getLanguage(); if (null === $lng) { $lng = static::getOption('default_language'); } } if (Arrays::isArray($value)) { if (Arrays::exists($lng, $value)) { return $value[$lng]; } } return ''; }
public function attach($path) { // if not array... if (!Arrays::isArray($path)) { // add $this->attachments[] = $path; } else { // spin array... foreach ($path as $p) { // add $this->attachments[] = $p; } } return $this; }
public static function order($type, $results, $field = 'date_create', $orderDirection = 'ASC') { $fields = static::getModel($type); $fields['id'] = array(); $fields['date_create'] = array(); if (!Arrays::isArray($field)) { if (null !== $field && !ake($field, $fields)) { $fields[$field] = array(); } } else { foreach ($field as $tmpField) { if (null !== $tmpField && !ake($tmpField, $fields)) { $fields[$tmpField] = array(); } } } $sort = array(); foreach ($results as $object) { $path = static::makePath($type, $object); $objectCreated = static::getObject($path, $type); foreach ($fields as $key => $infos) { $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::isArray($field) && Arrays::isArray($orderDirection)) { if (count($field) == 2) { $first = current($field); $second = end($field); if ('ASC' == Inflector::upper(current($orderDirection)) && 'ASC' == Inflector::upper(end($orderDirection))) { array_multisort(${$first}, SORT_ASC, ${$second}, SORT_ASC, $asort); } elseif ('DESC' == Inflector::upper(current($orderDirection)) && 'ASC' == Inflector::upper(end($orderDirection))) { array_multisort(${$first}, SORT_DESC, ${$second}, SORT_ASC, $asort); } elseif ('DESC' == Inflector::upper(current($orderDirection)) && 'DESC' == Inflector::upper(end($orderDirection))) { array_multisort(${$first}, SORT_DESC, ${$second}, SORT_DESC, $asort); } elseif ('ASC' == Inflector::upper(current($orderDirection)) && 'DESC' == Inflector::upper(end($orderDirection))) { array_multisort(${$first}, SORT_ASC, ${$second}, SORT_DESC, $asort); } } } else { if ('ASC' == Inflector::upper($orderDirection)) { array_multisort(${$field}, SORT_ASC, $asort); } else { array_multisort(${$field}, SORT_DESC, $asort); } } $collection = array(); foreach ($asort as $key => $row) { $tmpId = $row['id']; $tmpObject = static::getById($type, $tmpId); array_push($collection, $tmpObject); } return $collection; }