Ubiquity  2.0.0
php rapid development framework
Route.php
Go to the documentation of this file.
1 <?php
2 
4 
5 class Route {
6  private $path;
7  private $controller;
8  private $action;
9  private $parameters=[];
10  private $cache;
11  private $duration;
12  private $name;
13  private $methods;
14  private $id;
15  private $messages;
16 
17  public function __construct($path="",$array=[]){
18  $this->messages=[];
19  $this->path=$path;
20  if(isset($array["controller"])){
21  $this->fromArray($array);
22  }else{
23  $this->methods=\array_keys($array);
24  $this->fromArray(\reset($array));
25  }
26  $this->id=\uniqid();
27  }
28 
29  private function fromArray($array){
30  $this->controller=$array["controller"];
31  $this->action=$array["action"];
32  $this->name=$array["name"];
33  $this->cache=$array["cache"];
34  $this->duration=$array["duration"];
35  if(isset($array["parameters"]) && \sizeof($array["parameters"])>0){
36  if(\class_exists($this->controller)){
37  if(\method_exists($this->controller, $this->action)){
38  $method=new \ReflectionMethod($this->controller,$this->action);
39  $params=$method->getParameters();
40  foreach ($array["parameters"] as $paramIndex){
41  if($paramIndex==="*"){
42  $pName=$this->getVariadicParam($params);
43  if($pName!==false){
44  $this->parameters[]="...".$pName;
45  }
46  }else{
47  $index=\intval(\str_replace("~", "", $paramIndex));
48  if(isset($params[$index])){
49  if(\substr($paramIndex,0,1)==="~")
50  $this->parameters[]=$params[$index]->getName();
51  else
52  $this->parameters[]=$params[$index]->getName()."*";
53  }
54  }
55  }
56  }else{
57  $this->messages[]="The method <b>".$this->action."</b> does not exists in the class <b>".$this->controller."</b>.\n";
58  }
59  }else{
60  $this->messages[$this->controller]="The class <b>".$this->controller."</b> does not exist.\n";
61  }
62  }
63  }
64  private function getVariadicParam($parameters){
65  foreach ($parameters as $param){
66  if($param->isVariadic()){
67  return $param->getName();
68  }
69  }
70  return false;
71  }
72 
73  public function getPath() {
74  return $this->path;
75  }
76 
77  public function setPath($path) {
78  $this->path=$path;
79  return $this;
80  }
81 
82  public function getController() {
83  return $this->controller;
84  }
85 
86  public function setController($controller) {
87  $this->controller=$controller;
88  return $this;
89  }
90 
91  public function getAction() {
92  return $this->action;
93  }
94 
95  public function setAction($action) {
96  $this->action=$action;
97  return $this;
98  }
99 
100  public function getParameters() {
101  return $this->parameters;
102  }
103 
104  public function setParameters($parameters) {
105  $this->parameters=$parameters;
106  return $this;
107  }
108 
109  public function getCache() {
110  return $this->cache;
111  }
112 
113  public function setCache($cache) {
114  $this->cache=$cache;
115  return $this;
116  }
117 
118  public function getDuration() {
119  return $this->duration;
120  }
121 
122  public function setDuration($duration) {
123  $this->duration=$duration;
124  return $this;
125  }
126 
127  public function getName() {
128  return $this->name;
129  }
130 
131  public function setName($name) {
132  $this->name=$name;
133  return $this;
134  }
135 
136  public function getCompiledParams(){
137  return " (".((\is_array($this->parameters))?\implode(", ", $this->parameters):$this->parameters).")";
138  }
139 
140  public static function init($array){
141  $result=[];
142  foreach ($array as $k=>$v){
143  $result[]=new Route($k, $v);
144  }
145  return $result;
146  }
147 
148  public function getId() {
149  return $this->id;
150  }
151 
152  public function getMessages() {
153  return $this->messages;
154  }
155 
156  public function getMethods() {
157  return $this->methods;
158  }
159 
160  public function setMethods($methods) {
161  $this->methods=$methods;
162  return $this;
163  }
164 }
__construct($path="", $array=[])
Definition: Route.php:17