getValue() публичный статический Метод

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 getValue ( array $elements, string | integer $key, mixed | null $default = null ) : mixed | null
$elements array
$key string | integer
$default mixed | null
Результат mixed | null
Пример #1
0
 public static function newInstance($options)
 {
     if (Arrays::getValue($options, Options::EMULATE_PREPARES)) {
         return new EmulatedPDOPreparedStatementExecutor();
     }
     return new PDOPreparedStatementExecutor();
 }
Пример #2
0
 /**
  * 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);
 }
Пример #3
0
 private function _parseUrlParams($url)
 {
     $urlComponents = parse_url($url);
     $query = Arrays::getValue($urlComponents, 'query', '');
     parse_str($query, $array);
     return $array;
 }
Пример #4
0
 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);
 }
Пример #5
0
 private function _prepareParameters($uri)
 {
     preg_match_all('#:(\\w+)#', $uri, $matches);
     $parameters = Arrays::getValue($matches, 1, array());
     return Arrays::map($parameters, function ($parameter) {
         return '$' . $parameter;
     });
 }
Пример #6
0
 public function getRelation($name)
 {
     $value = Arrays::getValue($this->relations, $name);
     if (is_null($value)) {
         throw new Exception("Relation not found");
     }
     return $value;
 }
Пример #7
0
 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 '';
     }
 }
Пример #8
0
 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';
 }
Пример #9
0
 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;
 }
Пример #10
0
 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 '';
     }
 }
Пример #11
0
 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');
 }
Пример #12
0
 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;
 }
Пример #13
0
 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;
 }
Пример #14
0
 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);
     }
 }
Пример #15
0
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;
}
Пример #16
0
 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();
     }
 }
Пример #17
0
 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;
 }
Пример #18
0
 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;
 }
Пример #19
0
 /**
  * @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;
 }
Пример #21
0
 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;
     }
 }
Пример #22
0
 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;
     }
 }
Пример #23
0
 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);
         }
     }
 }
Пример #24
0
 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;
     }
 }
Пример #25
0
 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');
 }
Пример #26
0
 public function assignAttributes($params)
 {
     $this->userIds = Arrays::getValue($params, 'players', []);
     $this->type = Arrays::getValue($params['game'], 'type');
 }
Пример #27
0
 public function getErrorCode($errorInfo)
 {
     return Arrays::getValue($errorInfo, 1);
 }
Пример #28
0
 private function _parseAndSetName()
 {
     preg_match('#\\$(\\w+)#', $this->_parameter, $name);
     $this->_name = OuzoArrays::getValue($name, 1, '');
 }
Пример #29
0
 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);
 }
Пример #30
0
 private static function _errorCodesFromErrorInfo($errorInfo)
 {
     return Arrays::getValue($errorInfo, 0) . " " . Arrays::getValue($errorInfo, 1);
 }