Skip to content

Commit 705551c

Browse files
author
Ping Cheng
committed
add status short cut trait
1 parent 3b5a897 commit 705551c

File tree

4 files changed

+118
-1
lines changed

4 files changed

+118
-1
lines changed

src/Packet/JsonResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class JsonResponse extends PacketBase
1313

1414
protected function processData($data)
1515
{
16-
return json_encode($this->initResponse());
16+
return json_encode($this->initResponse()) . PHP_EOL;
1717
}
1818

1919
protected function setHeader()

src/Packet/PacketBase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88

99
namespace PingCheng\ApiResponse\Packet;
1010

11+
use PingCheng\ApiResponse\Traits\statusShortcuts;
12+
1113
abstract class PacketBase
1214
{
15+
16+
use statusShortcuts;
17+
1318
// settings
1419
public $send_header = true;
1520
public $send_code = true;
@@ -227,4 +232,11 @@ public function __toString()
227232
{
228233
return $this->output();
229234
}
235+
236+
/**
237+
* send the message and die
238+
*/
239+
public function send() {
240+
die($this->output());
241+
}
230242
}

src/Traits/statusShortcuts.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: pingcheng
5+
* Date: 3/4/18
6+
* Time: 10:59 PM
7+
*/
8+
9+
namespace PingCheng\ApiResponse\Traits;
10+
11+
trait statusShortcuts {
12+
private function statusShortcut_applydata($data = null) {
13+
if (!is_null($data)) {
14+
$this->data($data);
15+
}
16+
}
17+
18+
public function success($data = null) {
19+
$this->code(200);
20+
$this->statusShortcut_applydata($data);
21+
return $this;
22+
}
23+
24+
public function created($data = null) {
25+
$this->code(201);
26+
$this->statusShortcut_applydata($data);
27+
return $this;
28+
}
29+
30+
public function error($data = null) {
31+
$this->code(400);
32+
$this->statusShortcut_applydata($data);
33+
return $this;
34+
}
35+
36+
public function unauthorized($data = null) {
37+
$this->code(401);
38+
$this->statusShortcut_applydata($data);
39+
return $this;
40+
}
41+
42+
public function forbidden($data = null) {
43+
$this->code(403);
44+
$this->statusShortcut_applydata($data);
45+
return $this;
46+
}
47+
48+
public function notfound($data = null) {
49+
$this->code(404);
50+
$this->statusShortcut_applydata($data);
51+
return $this;
52+
}
53+
54+
public function fatal($data = null) {
55+
$this->code(500);
56+
$this->statusShortcut_applydata($data);
57+
return $this;
58+
}
59+
}

tests/statusTraitTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use PingCheng\ApiResponse\ApiResponse;
5+
6+
/**
7+
* Created by PhpStorm.
8+
* User: pingcheng
9+
* Date: 3/4/18
10+
* Time: 11:29 PM
11+
*/
12+
13+
class statusTraitTest extends TestCase
14+
{
15+
private $data = [
16+
'data' => 'ok',
17+
'target' => 'test'
18+
];
19+
20+
private $status;
21+
22+
public function __construct($name = null, array $data = [], $dataName = '')
23+
{
24+
parent::__construct($name, $data, $dataName);
25+
$this->status = require __DIR__.'/../src/Packet/statusCodes.php';
26+
}
27+
28+
public function testAll() {
29+
$this->examJSON(ApiResponse::json()->success($this->data), 200);
30+
$this->examJSON(ApiResponse::json()->created($this->data), 201);
31+
$this->examJSON(ApiResponse::json()->error($this->data), 400);
32+
$this->examJSON(ApiResponse::json()->unauthorized($this->data), 401);
33+
$this->examJSON(ApiResponse::json()->forbidden($this->data), 403);
34+
$this->examJSON(ApiResponse::json()->notfound($this->data), 404);
35+
$this->examJSON(ApiResponse::json()->fatal($this->data), 500);
36+
}
37+
38+
private function examJSON($result, $code) {
39+
$this->assertJson((string)$result);
40+
41+
$result = json_decode($result);
42+
$this->assertEquals($result->code, $code);
43+
$this->assertEquals($result->status, $this->status[$code]);
44+
$this->assertEquals($result->data, (object)$this->data);
45+
}
46+
}

0 commit comments

Comments
 (0)