Overview

Namespaces

  • Deimos
    • Route
      • Exceptions
  • PHP

Classes

  • Deimos\Route\Route

Interfaces

  • Throwable

Exceptions

  • Deimos\Route\Exceptions\PathNotFound
  • Exception
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: namespace Deimos\Route;
  4: 
  5: use Deimos\Route\Exceptions\PathNotFound;
  6: 
  7: class Route
  8: {
  9: 
 10:     /**
 11:      * @var string
 12:      */
 13:     protected $path;
 14: 
 15:     /**
 16:      * @var array
 17:      */
 18:     protected $defaults = [];
 19: 
 20:     /**
 21:      * @var array
 22:      */
 23:     protected $allowMethods = [];
 24: 
 25:     /**
 26:      * @var array
 27:      */
 28:     protected $regExp = [];
 29: 
 30:     /**
 31:      * @var string
 32:      */
 33:     protected $defaultRegExp = '[\w-А-ЯЁа-яё]+';
 34: 
 35:     /**
 36:      * Route constructor.
 37:      *
 38:      * @param array $path
 39:      * @param array $defaults
 40:      * @param array $allowMethods
 41:      *
 42:      * @throws PathNotFound
 43:      */
 44:     public function __construct(array $path, array $defaults = [], array $allowMethods = [])
 45:     {
 46:         $this->path = current($path);
 47: 
 48:         if (empty($this->path))
 49:         {
 50:             throw new PathNotFound('Path not found');
 51:         }
 52: 
 53:         $this->regExp       = next($path) ?: [];
 54:         $this->defaults     = $defaults;
 55:         $this->allowMethods = $allowMethods;
 56: 
 57:         $this->init();
 58:     }
 59: 
 60:     /**
 61:      * init route
 62:      */
 63:     protected function init()
 64:     {
 65:         $this->path = preg_replace_callback(
 66:             '~\<(?<key>\w+)(\:(?<value>.+?))?\>~',
 67:             function ($matches)
 68:             {
 69:                 if (!empty($matches['value']))
 70:                 {
 71:                     $this->regExp[$matches['key']] = $matches['value'];
 72:                 }
 73: 
 74:                 return '<' . $matches['key'] . '>';
 75:             },
 76:             $this->path
 77:         );
 78:     }
 79: 
 80:     /**
 81:      * @return string
 82:      */
 83:     public function route()
 84:     {
 85:         return $this->path;
 86:     }
 87: 
 88:     /**
 89:      * @param string $name
 90:      *
 91:      * @return string
 92:      */
 93:     public function regExp($name)
 94:     {
 95:         return isset($this->regExp[$name]) ?
 96:             $this->regExp[$name] :
 97:             $this->defaultRegExp;
 98:     }
 99: 
100:     /**
101:      * @return array
102:      */
103:     public function attributes()
104:     {
105:         return $this->defaults;
106:     }
107: 
108:     /**
109:      * @param $needle
110:      *
111:      * @return bool
112:      */
113:     public function methodIsAllow($needle)
114:     {
115:         if (empty($this->allowMethods))
116:         {
117:             return true;
118:         }
119: 
120:         return in_array($needle, $this->allowMethods, true);
121:     }
122: 
123: }
API documentation generated by ApiGen