Browse Source

* Make the following JsonSerializable
* Request
* Response
* Uri
* Automatically set the response content length when you write to it (Clears the old value)
* Add Settings::has(name)
* Fix PDO->stringFilter
* Fix ORM::table()

Nathaniel van Diepen 6 years ago
parent
commit
f2ab50c20c
6 changed files with 27 additions and 5 deletions
  1. 9 1
      Http/request.class.php
  2. 10 1
      Http/response.class.php
  3. 4 1
      Http/uri.class.php
  4. 1 1
      orm.abstract.class.php
  5. 0 1
      pdo.class.php
  6. 3 0
      settings.class.php

+ 9 - 1
Http/request.class.php

@@ -2,7 +2,7 @@
 	namespace Juju\Http;
 	require_once('uri.class.php');
 
-	class Request {
+	class Request implements \JsonSerializable {
 		private $verb;
 		private $url;
 		private $headers;
@@ -33,6 +33,14 @@
 				break;
 			}
 		}
+		public function jsonSerialize(){
+			return [
+				'verb'=> $this->verb,
+				'url'=> $this->url,
+				'headers'=> $this->headers,
+				'body'=> $this->body
+			];
+		}
 		public function header($name){
 			return $this->headers[$name] ?? null;
 		}

+ 10 - 1
Http/response.class.php

@@ -4,7 +4,7 @@
 	require_once(realpath(dirname(__DIR__).'/App/router.class.php'));
 	use \Juju\{App\Router, Events};
 
-	class Response {
+	class Response implements \JsonSerializable {
 		use Events;
 		public $output = '';
 		public $body = '';
@@ -16,6 +16,13 @@
 		public function __toString(){
 			return $this->body;
 		}
+		public function jsonSerialize(){
+			return [
+				'code'=> $this->code,
+				'headers'=> $this->headers,
+				'body'=> $this->body
+			];
+		}
 		public function clear(){
 			if($this->open){
 				$this->fire('clear');
@@ -43,6 +50,8 @@
 			$this->fire('write', $chunk);
 			if($this->open){
 				$this->body .= $chunk;
+				$this->clear_header('Content-Length')
+					->header('Content-Length', strlen($this->body));
 			}
 			return $this;
 		}

+ 4 - 1
Http/uri.class.php

@@ -1,7 +1,7 @@
 <?php
 	namespace Juju\Http;
 
-	class Uri{
+	class Uri implements \JsonSerializable {
 		private $url;
 		public function __construct($url){
 			if(is_array($url)){
@@ -54,5 +54,8 @@
 			// @todo - handle when scheme requires other formats
 			return "{$this->scheme}://{$auth}{$this->host}{$port}{$this->path}{$query}{$fragmanet}";
 		}
+		public function jsonSerialize(){
+			return "$this";
+		}
 	}
 ?>

+ 1 - 1
orm.abstract.class.php

@@ -163,7 +163,7 @@
 			return static::$table_name;
 		}
 		public static function table(){
-			if(is_null(static::$table)){
+			if(is_null(static::$table) || static::$table->name !== static::table_name()){
 				static::$table = self::$pdo->table(static::table_name());
 			}
 			return static::$table;

+ 0 - 1
pdo.class.php

@@ -150,7 +150,6 @@
 			if(!is_null($filter)){
 				$where = 'where ';
 				foreach($filter as $name => $value){
-					$value = $column['default'];
 					if($value instanceof Filter){
 						$where .= "`{$name}` {$value} and";
 					}else{

+ 3 - 0
settings.class.php

@@ -15,6 +15,9 @@
 			}
 			return self::$settings[$name];
 		}
+		public static function has(string $name){
+			return isset(self::$settings[$name]);
+		}
 		private static $settings = [];
 	}
 ?>