1 <?php
2
3 /*
4 * This file is part of the ICanBoogie package.
5 *
6 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace ICanBoogie;
13
14 use ICanBoogie\Routing\Pattern;
15
16 /**
17 * A route.
18 *
19 * @property-read \ICanBooogie\Routing\Pattern $pattern The pattern of the route.
20 */
21 class Route extends Object
22 {
23 /**
24 * Pattern of the route.
25 *
26 * @var \ICanBooogie\Routing\Pattern
27 */
28 private $pattern;
29
30 protected function get_pattern()
31 {
32 return $this->pattern;
33 }
34
35 /**
36 * Controller's class name or function.
37 *
38 * @var string
39 */
40 private $controller;
41
42 protected function get_controller()
43 {
44 return $this->controller;
45 }
46
47 /**
48 * Identifier of the route.
49 *
50 * @var string
51 */
52 public $id;
53
54 /**
55 * Redirect location.
56 *
57 * If the property is defined the route is considered an alias.
58 *
59 * @var string
60 */
61 public $location;
62
63 /**
64 * Request methods accepted by the route.
65 *
66 * @var string
67 */
68 public $via;
69
70 /**
71 * Initializes the {@link $pattern} property and the properties provided.
72 *
73 * @param string $pattern
74 * @param array $properties
75 */
76 public function __construct($pattern, array $properties)
77 {
78 $this->pattern = Pattern::from($pattern);
79
80 unset($properties['pattern']);
81
82 foreach ($properties as $property => $value)
83 {
84 $this->$property = $value;
85 }
86 }
87
88 public function __get($property)
89 {
90 switch ($property)
91 {
92 case 'url':
93 {
94 if (isset($this->url_provider))
95 {
96 $class = $this->url_provider;
97 $provider = new $class();
98
99 return $provider($this);
100 }
101 }
102 break;
103 }
104
105 return parent::__get($property);
106 }
107
108 /**
109 * Returns the pattern of the route.
110 *
111 * @return string
112 */
113 public function __toString()
114 {
115 return (string) $this->pattern;
116 }
117
118 /**
119 * Formats the route with the specified values.
120 *
121 * Note: The formatting of the route is defered to its {@link Pattern} instance.
122 *
123 * @return string
124 */
125 public function format($values=null)
126 {
127 return $this->pattern->format($values);
128 }
129 }