Skip to content

Commit

Permalink
add status short cut trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Ping Cheng committed Apr 3, 2018
1 parent 3b5a897 commit 705551c
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Packet/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 12 additions & 0 deletions src/Packet/PacketBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -227,4 +232,11 @@ public function __toString()
{
return $this->output();
}

/**
* send the message and die
*/
public function send() {
die($this->output());
}
}
59 changes: 59 additions & 0 deletions src/Traits/statusShortcuts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Created by PhpStorm.
* User: pingcheng
* Date: 3/4/18
* Time: 10:59 PM
*/

namespace PingCheng\ApiResponse\Traits;

trait statusShortcuts {
private function statusShortcut_applydata($data = null) {
if (!is_null($data)) {
$this->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;
}
}
46 changes: 46 additions & 0 deletions tests/statusTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use PHPUnit\Framework\TestCase;
use PingCheng\ApiResponse\ApiResponse;

/**
* Created by PhpStorm.
* User: pingcheng
* Date: 3/4/18
* Time: 11:29 PM
*/

class statusTraitTest extends TestCase
{
private $data = [
'data' => '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);
}
}

0 comments on commit 705551c

Please sign in to comment.