Example #1
0
 public function testExplodeUrl()
 {
     $url = '      /controller/action////id/1////';
     $right = ['controller', 'action', 'id', '1'];
     $result = \Cloudstash\Point\Helper\Routing::explodeUrl($url);
     $result = \Cloudstash\Helper\Arr::similar($right, $result, false);
     $this->assertTrue($result, 'Wrong URL explode');
     $url = '     /     ';
     $right = [];
     $result = \Cloudstash\Point\Helper\Routing::explodeUrl($url);
     $result = \Cloudstash\Helper\Arr::similar($right, $result, false);
     $this->assertTrue($result, 'Wrong empty URL explode');
 }
Example #2
0
 /**
  * @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;
 }
Example #3
0
 /**
  * @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);
 }
Example #4
0
 /**
  * @return string
  */
 public function getRequestMethod()
 {
     return Arr::get($this->server, 'REQUEST_METHOD', 'GET');
 }
Example #5
0
 /**
  * @param string $name
  * @param mixed $default
  * @return mixed
  */
 public function getVariable($name, $default = null)
 {
     return Arr::get($this->variables, $name, $default);
 }
Example #6
0
 public function testFlattenToTree()
 {
     $source = [3944 => 'VI/Авто', 3945 => 'VI/Авто/Отечественные авто', 3946 => 'VI/Авто/Иномарки', 3947 => 'VI/Рынок/Фондовый', 3948 => 'AiData/Магазин/Книги', 3949 => 'AiData/Магазин/Колёса,Шины'];
     $wait = ['VI' => ['__tree' => ['Авто' => ['__value' => 3944, '__tree' => ['Отечественные авто' => ['__value' => 3945], 'Иномарки' => ['__value' => 3946]]], 'Рынок' => ['__tree' => ['Фондовый' => ['__value' => 3947]]]]], 'AiData' => ['__tree' => ['Магазин' => ['__tree' => ['Книги' => ['__value' => 3948], 'Колёса,Шины' => ['__value' => 3949]]]]]];
     $tree = \Cloudstash\Helper\Arr::flattenToTree($source, '/');
     $this->assertTrue($tree == $wait, 'Bad flatten to tree');
     $tree = \Cloudstash\Helper\Arr::flattenToTree($source, ['/']);
     $this->assertTrue($tree == $wait, 'Bad flatten to tree from single array delimiters stack');
     $source = [3944 => 'VI/Авто', 3945 => 'VI/Авто/Отечественные авто', 3946 => 'VI/Авто/Иномарки', 3947 => 'VI_Рынок_Фондовый', 3948 => 'AiData/Магазин/Книги', 3949 => 'AiData/Магазин/Колёса_Шины'];
     $wait = ['VI' => ['__tree' => ['Авто' => ['__value' => 3944, '__tree' => ['Отечественные авто' => ['__value' => 3945], 'Иномарки' => ['__value' => 3946]]], 'Рынок' => ['__tree' => ['Фондовый' => ['__value' => 3947]]]]], 'AiData' => ['__tree' => ['Магазин' => ['__tree' => ['Книги' => ['__value' => 3948], 'Колёса_Шины' => ['__value' => 3949]]]]]];
     $tree = \Cloudstash\Helper\Arr::flattenToTree($source, ['/', '_']);
     $this->assertTrue($tree == $wait, 'Bad flatten to tree from multiple array delimiters stack');
 }