-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
136 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Jiannei\Response\Laravel\Contracts; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
use Illuminate\Http\Resources\Json\ResourceCollection; | ||
use Illuminate\Pagination\AbstractPaginator; | ||
|
||
interface Format | ||
{ | ||
/** | ||
* Format return data structure. | ||
* | ||
* @param array|null $data | ||
* @param string|null $message | ||
* @param int $code | ||
* @param null $errors | ||
* @return array | ||
*/ | ||
public function data(?array $data, ?string $message, int $code, $errors = null): array; | ||
|
||
/** | ||
* Http status code. | ||
* | ||
* @param $code | ||
* @return int | ||
*/ | ||
public function statusCode($code): int; | ||
|
||
/** | ||
* Format paginator data. | ||
* | ||
* @param AbstractPaginator $resource | ||
* @param string $message | ||
* @param int $code | ||
* @param array $headers | ||
* @param int $option | ||
* @return array | ||
*/ | ||
public function paginator(AbstractPaginator $resource, string $message = '', int $code = 200, array $headers = [], int $option = 0):array; | ||
|
||
/** | ||
* Format collection resource data. | ||
* | ||
* @param ResourceCollection $resource | ||
* @param string $message | ||
* @param int $code | ||
* @param array $headers | ||
* @param int $option | ||
* @return array | ||
*/ | ||
public function resourceCollection(ResourceCollection $resource, string $message = '', int $code = 200, array $headers = [], int $option = 0): array; | ||
|
||
/** | ||
* Format JsonResource Data. | ||
* | ||
* @param JsonResource $resource | ||
* @param string $message | ||
* @param int $code | ||
* @param array $headers | ||
* @param int $option | ||
* @return array | ||
*/ | ||
public function jsonResource(JsonResource $resource, string $message = '', int $code = 200, array $headers = [], int $option = 0): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Jiannei\Response\Laravel\Tests; | ||
|
||
use Jiannei\Response\Laravel\Support\Facades\Response; | ||
|
||
class FormatTest extends TestCase | ||
{ | ||
public function testAddExtraField() | ||
{ | ||
$response = Response::success(); | ||
|
||
$this->assertEquals(200, $response->status()); | ||
|
||
$this->assertArrayHasKey('extra',$response->getData(true)); | ||
$this->assertArrayHasKey('time',$response->getData(true)['extra']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Jiannei\Response\Laravel\Tests\Support; | ||
|
||
use Illuminate\Support\Facades\Config; | ||
|
||
class Format extends \Jiannei\Response\Laravel\Support\Format | ||
{ | ||
public function data(?array $data, ?string $message, int $code, $errors = null): array | ||
{ | ||
if (! $message && class_exists($enumClass = Config::get('response.enum'))) { | ||
$message = $enumClass::fromValue($code)->description; | ||
} | ||
|
||
return $this->formatDataFields([ | ||
'status' => $this->formatStatus($code), | ||
'code' => $code, | ||
'message' => $message, | ||
'data' => $data ?: (object) $data, | ||
'error' => $errors ?: (object) [], | ||
'extra' => [ | ||
'time' => time() | ||
], | ||
], Config::get('response.format.fields', [])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters