Browse Source

Force types

Nathaniel van Diepen 7 years ago
parent
commit
cf0878100b
6 changed files with 32 additions and 29 deletions
  1. 6 6
      app.class.php
  2. 4 4
      dypath.class.php
  3. 7 4
      path.class.php
  4. 1 1
      request.class.php
  5. 7 7
      response.class.php
  6. 7 7
      router.class.php

+ 6 - 6
app.class.php

@@ -11,7 +11,7 @@
 		private $routers;
 		private $router;
 		public $_onerror;
-		public function __construct($name, Callable $fn = null){
+		public function __construct(string $name, Callable $fn = null){
 			$this->name = $name;
 			$this->router = new Router();
 			$this->routers = [];
@@ -80,7 +80,7 @@
 				}
 			}
 		}
-		public function handle($verb, $url, $data, $headers = null){
+		public function handle(string $verb, array $url, string $data, array $headers = null){
 			if(is_null($headers)){
 				$headers = getallheaders();
 			}
@@ -115,25 +115,25 @@
 			$this->_onerror = $fn;
 			return $this;
 		}
-		public function route($path, Callable $fn){
+		public function route(string $path, Callable $fn){
 			$this->router->path($path, $fn);
 			return $this;
 		}
-		public function prefix($prefix, Callable $fn){
+		public function prefix(string $prefix, Callable $fn){
 			if(!$this->routers[$prefix]){
 				$this->routers[$prefix] = new Router($prefix);
 			}
 			$fn($this->routers[$prefix]);
 			return $this;
 		}
-		public function domain($host, Callable $fn){
+		public function domain(string $host, Callable $fn){
 			if(!isset($this->domains[$host])){
 				$this->domains[$host] = new Router();
 			}
 			$fn($this->domains[$host]);
 			return $this;
 		}
-		public function onerror($req, $res, $error){
+		public function onerror(Request $req, Response $res, $error){
 			$fn = $this->_onerror;
 			if(is_callable($fn)){
 				$fn($req, $res, $error);

+ 4 - 4
dypath.class.php

@@ -2,7 +2,7 @@
 	require_once('arguments.class.php');
 	class DyPath implements JsonSerializable{
 		public $path;
-		public function __construct($path){
+		public function __construct(string $path){
 			$this->path = $path;
 		}
 		public function jsonSerialize(){
@@ -26,10 +26,10 @@
 				break;
 			}
 		}
-		public function matches($url){
-			return preg_match($this->regex,$url);
+		public function matches(string $url){
+			return preg_match($this->regex, $url);
 		}
-		public function args($url){
+		public function args(string $url){
 			$ret = [];
 			preg_match_all($this->regex,$url,$m,PREG_SET_ORDER);
 			foreach($this->arguments as $k => $arg){

+ 7 - 4
path.class.php

@@ -1,12 +1,15 @@
 <?php
 	require_once('dypath.class.php');
+	require_once('request.class.php');
+	require_once('response.class.php');
+	require_once('arguments.class.php');
 	class Path implements JsonSerializable{
 		private $handles = [];
 		public $path;
-		public function __construct($path){
+		public function __construct(string $path){
 			$this->path = new DyPath($path);
 		}
-		public function __invoke($req, $res,$args){
+		public function __invoke(Request $req, Response $res, Arguments $args){
 			$err = null;
 			foreach($this->handles as $k => $fn){
 				try{
@@ -34,10 +37,10 @@
 			array_push($this->handles,$fn);
 			return $this;
 		}
-		public function matches($path){
+		public function matches(string $path){
 			return $this->path->matches($path);
 		}
-		public function args($path){
+		public function args(string $path){
 			return $this->path->args($path);
 		}
 	}

+ 1 - 1
request.class.php

@@ -3,7 +3,7 @@
 		private $url;
 		private $headers;
 		private $body;
-		public function __construct($url, $headers = [], $body = ''){
+		public function __construct($url, array $headers = [], string $body = ''){
 			if(is_string($url) || is_array($url)){
 				$this->url = new Uri($url);
 			}elseif($url instanceof Uri){

+ 7 - 7
response.class.php

@@ -21,7 +21,7 @@
 			}
 			return $this;
 		}
-		public function clear_header($name){
+		public function clear_header(string $name){
 			foreach($this->headers as $key => $header){
 				if($header[0] == $name){
 					array_splice($this->headers, $key, 1);
@@ -29,7 +29,7 @@
 			}
 			return $this;
 		}
-		public function write($chunk){
+		public function write(string $chunk){
 			if($this->open){
 				$this->body .= $chunk;
 			}
@@ -49,7 +49,7 @@
 			}
 			return $this;
 		}
-		public function header($name,$value){
+		public function header(string $name, string $value){
 			if($this->open){
 				array_push(
 					$this->headers,
@@ -61,18 +61,18 @@
 			}
 			return $this;
 		}
-		public function redirect($url){
+		public function redirect(string $url){
 			$this->header('Location',Router::url($url));
 			return $this;
 		}
-		public function end($chunk=''){
+		public function end(string $chunk=''){
 			if($this->open){
 				$this->write($chunk);
 				$this->open = false;
 			}
 			return $this;
 		}
-		public function img($img, $type = false){
+		public function img(Image $img, string $type = null){
 			if(!$type){
 				$type = $img->type;
 			}
@@ -88,7 +88,7 @@
 			ob_end_clean();
 			return $this;
 		}
-		public function code($code=null){
+		public function code(int $code=null){
 			if(is_null($code)){
 				return $this->code;
 			}

+ 7 - 7
router.class.php

@@ -8,7 +8,7 @@
 		private $_base = '/';
 		private $responses = [];
 		private $_handled = false;
-		public function __construct($base = null, $paths = null){
+		public function __construct(string $base = null, array $paths = null){
 			if($paths != null){
 				$this->paths($paths);
 			}
@@ -35,13 +35,13 @@
 		public function __toString(){
 			return "[Router]";
 		}
-		public function base($base){
+		public function base(string $base){
 			$this->_base = $base;
 		}
-		public function url($url){
+		public function url(string $url){
 			return preg_replace('/(\/+)/','/',$url);
 		}
-		public function prefix($prefix, Callable $fn){
+		public function prefix(string $prefix, Callable $fn){
 			$found = false;
 			foreach($this->_routers as $k => $router){
 				if($router->base == $prefix){
@@ -57,7 +57,7 @@
 			}
 		}
 		// fn = function(response,args){}
-		public function path($path, Callable $fn){
+		public function path(string $path, Callable $fn){
 			$obj = false;
 			foreach($this->_paths as $k => $p){
 				if($p->path == $path){
@@ -70,7 +70,7 @@
 			}
 			return $obj->handle($fn);
 		}
-		public function paths($paths){
+		public function paths(array $paths){
 			foreach($paths as $path => $fn){
 				$this->path($path,$fn);
 			}
@@ -78,7 +78,7 @@
 		public function clear(){
 			$this->_paths = [];
 		}
-		public function handle($path, $req = null,$res = null,Callable $fn = null, Callable $onerror = null){
+		public function handle(string $path, Request $req = null,Response $res = null, Callable $fn = null, Callable $onerror = null){
 			if(strpos($path, $this->base) !== false){
 				$path = substr($path,strpos($path,$this->base)+strlen($this->base));
 				if($path[0] != '/'){