public function testGet() { $nested = ['key' => 'value']; $source = $this->get_source_array($nested); // получаем по ключу значение (строка) $result = \Cloudstash\Helper\Arr::get($source, 'key'); $this->assertEquals($result, 'value', 'Wrong value for key "key"'); // получаем по ключу значение (число) $result = \Cloudstash\Helper\Arr::get($source, 'number'); $this->assertEquals($result, 132345, 'Wrong value for key "number"'); // получаем по ключу значение (многобайтовая строка) $result = \Cloudstash\Helper\Arr::get($source, 'mbstring'); $this->assertEquals($result, 'Тест на русском языке', 'Wrong value for key "mbstring"'); // при отсутствующем ключе, должен возвращаться null $result = \Cloudstash\Helper\Arr::get($source, 'undefined'); $this->assertEquals($result, null, 'Wrong default value (null)'); // при отсутствующем ключе, должено вернуться объявленное значение по умолчанию (число) $result = \Cloudstash\Helper\Arr::get($source, 'undefined', 1000); $this->assertEquals($result, 1000, 'Wrong default value (number)'); // получаем по ключу вложеный массив $result = \Cloudstash\Helper\Arr::get($source, 'nested'); $result = \Cloudstash\Helper\Arr::similar($result, $nested); $this->assertTrue($result, 'Wrong nested array'); }
/** * @param string $url * @return bool */ public function isCurrent($url) { $uriArray = Routing::explodeUrl($url); if (count($this->pattern_strict) != count($uriArray)) { return false; } foreach ($this->pattern_strict as $index => $segment) { $partial = Arr::get($uriArray, $index, null); if (is_null($partial)) { return false; } $type = Arr::get($segment, 'type', self::TYPE_BLOCK); $matcher = Arr::get($segment, 'matcher', null); $name = Arr::get($segment, 'name', "var{$index}"); // block must be strong assert with url partial if ($type == self::TYPE_BLOCK) { if ($partial == $matcher) { continue; } return false; } // if it is variable with callable filter if (is_callable($matcher)) { if (call_user_func_array($matcher, [$partial])) { $this->values[$name] = $partial; continue; } return false; } // if not callable then just set to variable all segment value if ($type == self::TYPE_VARIABLE) { $this->values[$name] = $partial; continue; } return false; } return true; }
/** * @return Route */ public function handleCurrent() { $uri = new Uri(); if ($uri->isRootDirectory()) { return $this->callAction($this->defaultRoute); } $requestPath = $uri->getRequestPath(); foreach ($this->collection as $routeName => $routeSettings) { /** * @var Route $route */ $route = Arr::get($routeSettings, 'route', null); $availableMethod = Arr::get($routeSettings, 'method', self::$defaultAvailableMethods); if (is_null($route)) { continue; } if (!in_array($this->getCurrentRequestMethod(), $availableMethod)) { continue; } if ($route->isCurrent($requestPath)) { return $this->callAction($route); } } return $this->callAction($this->notFoundRoute); }
/** * @return string */ public function getRequestMethod() { return Arr::get($this->server, 'REQUEST_METHOD', 'GET'); }
/** * @param string $name * @param mixed $default * @return mixed */ public function getVariable($name, $default = null) { return Arr::get($this->variables, $name, $default); }