diff --git a/src/Packet/JsonResponse.php b/src/Packet/JsonResponse.php index b2b8d6e..69fb0a2 100644 --- a/src/Packet/JsonResponse.php +++ b/src/Packet/JsonResponse.php @@ -13,7 +13,7 @@ class JsonResponse extends PacketBase protected function processData($data) { - return json_encode($this->initResponse()); + return json_encode($this->initResponse()) . PHP_EOL; } protected function setHeader() diff --git a/src/Packet/PacketBase.php b/src/Packet/PacketBase.php index cf876ca..129c753 100644 --- a/src/Packet/PacketBase.php +++ b/src/Packet/PacketBase.php @@ -8,8 +8,13 @@ namespace PingCheng\ApiResponse\Packet; +use PingCheng\ApiResponse\Traits\statusShortcuts; + abstract class PacketBase { + + use statusShortcuts; + // settings public $send_header = true; public $send_code = true; @@ -227,4 +232,11 @@ public function __toString() { return $this->output(); } + + /** + * send the message and die + */ + public function send() { + die($this->output()); + } } \ No newline at end of file diff --git a/src/Traits/statusShortcuts.php b/src/Traits/statusShortcuts.php new file mode 100644 index 0000000..e1326de --- /dev/null +++ b/src/Traits/statusShortcuts.php @@ -0,0 +1,59 @@ +data($data); + } + } + + public function success($data = null) { + $this->code(200); + $this->statusShortcut_applydata($data); + return $this; + } + + public function created($data = null) { + $this->code(201); + $this->statusShortcut_applydata($data); + return $this; + } + + public function error($data = null) { + $this->code(400); + $this->statusShortcut_applydata($data); + return $this; + } + + public function unauthorized($data = null) { + $this->code(401); + $this->statusShortcut_applydata($data); + return $this; + } + + public function forbidden($data = null) { + $this->code(403); + $this->statusShortcut_applydata($data); + return $this; + } + + public function notfound($data = null) { + $this->code(404); + $this->statusShortcut_applydata($data); + return $this; + } + + public function fatal($data = null) { + $this->code(500); + $this->statusShortcut_applydata($data); + return $this; + } +} \ No newline at end of file diff --git a/tests/statusTraitTest.php b/tests/statusTraitTest.php new file mode 100644 index 0000000..1b87456 --- /dev/null +++ b/tests/statusTraitTest.php @@ -0,0 +1,46 @@ + 'ok', + 'target' => 'test' + ]; + + private $status; + + public function __construct($name = null, array $data = [], $dataName = '') + { + parent::__construct($name, $data, $dataName); + $this->status = require __DIR__.'/../src/Packet/statusCodes.php'; + } + + public function testAll() { + $this->examJSON(ApiResponse::json()->success($this->data), 200); + $this->examJSON(ApiResponse::json()->created($this->data), 201); + $this->examJSON(ApiResponse::json()->error($this->data), 400); + $this->examJSON(ApiResponse::json()->unauthorized($this->data), 401); + $this->examJSON(ApiResponse::json()->forbidden($this->data), 403); + $this->examJSON(ApiResponse::json()->notfound($this->data), 404); + $this->examJSON(ApiResponse::json()->fatal($this->data), 500); + } + + private function examJSON($result, $code) { + $this->assertJson((string)$result); + + $result = json_decode($result); + $this->assertEquals($result->code, $code); + $this->assertEquals($result->status, $this->status[$code]); + $this->assertEquals($result->data, (object)$this->data); + } +} \ No newline at end of file