Example:
$array = array('id' => 1, 'name' => 'john');
$value = Arrays::getValue($array, 'name');
Result:
john
Example:
$array = array('id' => 1, 'name' => 'john');
$value = Arrays::getValue($array, 'surname', '--not found--');
Result:
--not found--
public static function newInstance($options) { if (Arrays::getValue($options, Options::EMULATE_PREPARES)) { return new EmulatedPDOPreparedStatementExecutor(); } return new PDOPreparedStatementExecutor(); }
/** * Returns object from cache. * If there's no object for the given key and $functions is passed, $function result will be stored in cache under the given key. * * Example: * <code> * $countries = Cache::get("countries", function() {{ * //expensive computation that returns a list of countries * return Country:all(); * }) * </code> * * @param $key * @param null $function * @return mixed|null */ public static function get($key, $function = null) { if (!self::contains($key) && $function) { self::put($key, call_user_func($function)); } return Arrays::getValue(self::$_cache, $key); }
private function _parseUrlParams($url) { $urlComponents = parse_url($url); $query = Arrays::getValue($urlComponents, 'query', ''); parse_str($query, $array); return $array; }
private static function newRelation($name, $localKey, $foreignKey, $collection, $params) { $class = $params['class']; $condition = Arrays::getValue($params, 'conditions', ''); $order = Arrays::getValue($params, 'order', ''); return new Relation($name, $class, $localKey, $foreignKey, $collection, $condition, $order); }
private function _prepareParameters($uri) { preg_match_all('#:(\\w+)#', $uri, $matches); $parameters = Arrays::getValue($matches, 1, array()); return Arrays::map($parameters, function ($parameter) { return '$' . $parameter; }); }
public function getRelation($name) { $value = Arrays::getValue($this->relations, $name); if (is_null($value)) { throw new Exception("Relation not found"); } return $value; }
private function _getPrimaryKey($tableName) { $primaryKey = Db::getInstance()->query("SHOW KEYS FROM {$tableName} WHERE Key_name = 'PRIMARY'")->fetch(); if ($primaryKey) { return Arrays::getValue($primaryKey, 'Column_name'); } else { return ''; } }
private static function getViewPostfix($responseType) { $availableViewsMap = array('text/xml' => '.xml.phtml', 'application/json' => '.json.phtml', 'text/json' => '.json.phtml'); $viewForType = Arrays::getValue($availableViewsMap, $responseType, false); if ($viewForType) { return $viewForType; } return Uri::isAjax() ? '.ajax.phtml' : '.phtml'; }
public function getPath() { $uri = Arrays::getValue($_SERVER, 'REDIRECT_URL'); if (!$uri) { return Arrays::getValue($_SERVER, 'REQUEST_URI', '/'); } $queryString = Arrays::getValue($_SERVER, 'REDIRECT_QUERY_STRING'); return $queryString ? $uri . '?' . $queryString : $uri; }
private function _getPrimaryKey($tableName) { $primaryKey = Db::getInstance()->query("SELECT pg_attribute.attname\n FROM pg_index, pg_class, pg_attribute\n WHERE\n pg_class.oid = '{$tableName}'::REGCLASS AND\n indrelid = pg_class.oid AND\n pg_attribute.attrelid = pg_class.oid AND\n pg_attribute.attnum = ANY(pg_index.indkey)\n AND indisprimary;\n ")->fetch(); if ($primaryKey) { return Arrays::getValue($primaryKey, 'attname'); } else { return ''; } }
public static function resolve() { $accept = array_keys(RequestHeaders::accept()) ?: array('*/*'); $supported = array('application/json' => 'application/json', 'application/xml' => 'application/xml', 'application/*' => 'application/json', 'text/html' => 'text/html', 'text/*' => 'text/html'); $intersection = array_intersect($accept, array_keys($supported)); if ($intersection) { return $supported[Arrays::first($intersection)]; } return Arrays::getValue($supported, ContentType::value(), 'text/html'); }
public static function ip() { $ip = Arrays::getValue($_SERVER, 'HTTP_CLIENT_IP'); if (!$ip) { $ip = Arrays::getValue($_SERVER, 'HTTP_X_FORWARDED_FOR'); } if (!$ip) { $ip = Arrays::getValue($_SERVER, 'REMOTE_ADDR'); } return $ip; }
public static function _checkCredentials($authUser, $authPassword, $realm) { $login = Arrays::getValue($_SERVER, 'PHP_AUTH_USER'); $pass = Arrays::getValue($_SERVER, 'PHP_AUTH_PW'); if ($authUser != $login || $authPassword != $pass) { $code = defined('UNAUTHORIZED') ? UNAUTHORIZED : 0; $error = new Error($code, I18n::t('exception.unauthorized')); throw new UnauthorizedException($error, array('WWW-Authenticate: Basic realm="' . $realm . '"')); } return true; }
protected function log($writeToLogFunction, $level, $levelName, $message, $params) { $minimalLevel = $this->_minimalLevels ? Arrays::getValue($this->_minimalLevels, $this->_name, LOG_DEBUG) : LOG_DEBUG; if ($level <= $minimalLevel) { $message = $this->_messageFormatter->format($this->_name, $levelName, $message); if (!empty($params)) { $message = call_user_func_array('sprintf', array_merge(array($message), $params)); } $writeToLogFunction($message); } }
function postButton($label, $url, $options = []) { $class = Arrays::getValue($options, 'class', ''); $id = Arrays::getValue($options, 'id', ''); $idHtml = $id ? " id=\"{$id}\" " : ""; return <<<TAG <form action="{$url}" {$idHtml} method="post" class="post-button {$class}"> <button type="submit" class="btn btn-primary">{$label}</button> </form> TAG; }
public static function validate() { $csrfToken = self::getCsrfToken(); if (!isset($_COOKIE['csrftoken']) || $_COOKIE['csrftoken'] != $csrfToken) { self::_throwException(); } $headerToken = Arrays::getValue(RequestHeaders::all(), 'X-Csrftoken'); $postToken = Arrays::getValue($_POST, 'csrftoken'); if ($headerToken != $csrfToken && $postToken != $csrfToken) { self::_throwException(); } }
private function _parseArgs($args, $parameters) { $args = Arrays::getValue($args, 0, new stdClass()); $newArgs = array(); $parameterNames = Arrays::map($parameters, Functions::extract()->getName()); foreach ($parameterNames as $name) { if (isset($args->{$name})) { $newArgs[] = $args->{$name}; } } return $newArgs; }
public function __get($name) { $value = Arrays::getValue($this->attributes, $name); if ($value) { return $value; } if ($this->relations->hasRelation($name)) { $this->fetchRelation($name); return $this->attributes[$name]; } return null; }
/** * @param Relation[] $relations * @param string|string[]|null $aliases * @return RelationWithAlias[] */ public static function associateRelationsWithAliases(array $relations, $aliases) { $aliases = Arrays::toArray($aliases); if (count($relations) < count($aliases)) { throw new InvalidArgumentException("More aliases than relations in join"); } $relationWithAliases = array(); foreach ($relations as $index => $relation) { $alias = Arrays::getValue($aliases, $index, Arrays::getValue($aliases, $relation->getName())); $relationWithAliases[] = new RelationWithAlias($relation, $alias); } return $relationWithAliases; }
/** * @return array * @throws Exception */ public static function getTypeNames() { $typeNamesMap = []; foreach (self::$classMap as $type => $class) { $name = Arrays::getValue(self::$nameMap, $type); if ($name) { $typeNamesMap[$type] = $name; } else { throw new Exception('Not defined name for the game type [' . $type . ']'); } } return $typeNamesMap; }
public static function _compare($a, $b) { $a_q = floatval(Arrays::getValue($a, 'q', 1)); $b_q = floatval(Arrays::getValue($b, 'q', 1)); if ($a_q === $b_q) { if ($r = self::_compareSubType($a['type'], $b['type'])) { return $r; } else { return self::_compareSubType($a['subtype'], $b['subtype']); } } else { return $a_q < $b_q; } }
public function streamMediaFile(array $fileData) { $location = $fileData['path']; $filename = $fileData['label']; $mimeType = Arrays::getValue($fileData, 'mime', 'application/octet-stream'); if (!file_exists($location)) { header("HTTP/1.1 404 Not Found"); return; } $size = filesize($location); $time = date('r', filemtime($location)); $fm = @fopen($location, 'rb'); if (!$fm) { header("HTTP/1.1 505 Internal server error"); return; } $begin = 0; $end = $size - 1; if (isset($_SERVER['HTTP_RANGE'])) { if (preg_match('/bytes=\\h*(\\d+)-(\\d*)[\\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches)) { $begin = intval($matches[1]); if (!empty($matches[2])) { $end = intval($matches[2]); } } } if (isset($_SERVER['HTTP_RANGE'])) { header('HTTP/1.1 206 Partial Content'); } else { header('HTTP/1.1 200 OK'); } header("Content-Type: {$mimeType}"); header('Cache-Control: public, must-revalidate, max-age=0'); header('Pragma: no-cache'); header('Accept-Ranges: bytes'); header('Content-Length:' . ($end - $begin + 1)); if (isset($_SERVER['HTTP_RANGE'])) { header("Content-Range: bytes {$begin}-{$end}/{$size}"); } header("Content-Disposition: inline; filename={$filename}"); header("Content-Transfer-Encoding: binary"); header("Last-Modified: {$time}"); $cur = $begin; fseek($fm, $begin, 0); while (!feof($fm) && $cur <= $end && connection_status() == 0) { print fread($fm, min(1024 * 16, $end - $cur + 1)); $cur += 1024 * 16; } }
private function injectDependencies(InstanceRepository $repository, $instance) { $annotations = $this->provider->getMetadata($instance); $class = new ReflectionClass($instance); $properties = $class->getProperties(); foreach ($properties as $property) { $annotation = Arrays::getValue($annotations, $property->getName()); if ($annotation) { $binder = $this->bindings->getBinder($annotation['className'], $annotation['name']); $dependencyInstance = $repository->getInstance($this, $binder); $property->setAccessible(true); $property->setValue($instance, $dependencyInstance); } } }
private static function addRoute($method, $uri, $action, $requireAction = true, $options = array(), $isResource = false) { $methods = Arrays::toArray($method); if (self::$isDebug && $requireAction && self::$validate && self::existRouteRule($methods, $uri)) { $methods = implode(', ', $methods); throw new InvalidArgumentException('Route rule for method ' . $methods . ' and URI "' . $uri . '" already exists'); } $elements = explode('#', $action); $controller = Arrays::first($elements); $actionToRule = Arrays::getValue($elements, 1); $routeRule = new RouteRule($method, $uri, $controller, $actionToRule, $requireAction, $options, $isResource); if ($routeRule->hasRequiredAction()) { throw new InvalidArgumentException('Route rule ' . $uri . ' required action'); } self::$routes[] = $routeRule; foreach ($methods as $method) { self::$routeKeys[$method . $uri] = true; } }
private static function _createUrlFromArray($params) { $prefixSystem = Config::getValue('global', 'prefix_system'); $controller = Arrays::getValue($params, 'controller'); $action = Arrays::getValue($params, 'action'); $extraParams = Arrays::getValue($params, 'extraParams'); if ($controller && $action) { $url = Joiner::on('/')->join(array($prefixSystem, $controller, $action)); if ($extraParams) { $url .= self::_mergeParams($extraParams); } return $url; } $string = Arrays::getValue($params, 'string'); if ($string) { return $prefixSystem . $string; } throw new InvalidArgumentException('Illegal arguments'); }
public function assignAttributes($params) { $this->userIds = Arrays::getValue($params, 'players', []); $this->type = Arrays::getValue($params['game'], 'type'); }
public function getErrorCode($errorInfo) { return Arrays::getValue($errorInfo, 1); }
private function _parseAndSetName() { preg_match('#\\$(\\w+)#', $this->_parameter, $name); $this->_name = OuzoArrays::getValue($name, 1, ''); }
private function _createPdo($params) { $dsn = Arrays::getValue($params, 'dsn'); $options = Arrays::getValue($params, 'options', array()); if ($dsn) { return new PDO($dsn, '', '', $options); } $dsn = $this->_buildDsn($params); return new PDO($dsn, $params['user'], $params['pass'], $options); }
private static function _errorCodesFromErrorInfo($errorInfo) { return Arrays::getValue($errorInfo, 0) . " " . Arrays::getValue($errorInfo, 1); }