kodeops/rro

丰富的响应对象插件。

1.4.3 2023-04-17 17:30 UTC

README

 _     _  _____  ______  _______  _____   _____  _______
 |____/  |     | |     \ |______ |     | |_____] |______
 |    \_ |_____| |_____/ |______ |_____| |       ______|
 

rro | 丰富的响应对象

设置

添加Composer包

$ composer require kodeops/rro:dev-master

将函数辅助器复制到您的项目中(推荐,但不是必需的)

https://raw.githubusercontent.com/kodeops/rro/master/src/helpers.php

rro-helpers.php 添加到 composer.json 的自动加载部分

"autoload": {
    ...
        
   "files": [
       "vendor/kodeops/rro/src/helpers.php"
   ]
}

使用方法

假设辅助函数已在 composer.json 中加载

$rro = error()
  ->type('error_type')
  ->message('This is a sample rich response object.')
  ->code(404)
  ->data(['foo' => 'bar']);

检查状态

if ($rro->isError()) {
  // Response contains an error payload
}

if ($rro->isSuccess()) {
  // Response contains a success payload
}

此外,isErrorisSuccess 接受两个参数来检查 typemessage 内容

if ($rro->isError('type', 'error_type')) {
  // Response contains an error type equals to "error_type"
}

if ($rro->isError('message', 'Error message')) {
  // Response contains an error message equals to "error_type"
}

if ($rro->isSuccess('type', 'success_type')) {
  // Response contains a success type equals to "success_type"
}

if ($rro-> isSuccess('message', 'Success message')) {
  // Response contains a message equals to "Success message"
}

或者,您可以独立访问,无论它是 error 还是 response

if ($rro->response('is_type', 'error_type')) {
  // returns true if given type matches response type
}

if ($rro->response('is_message', 'Descriptive error message')) {
  // returns true if given message matches response message
}

访问响应

$type = $rro->response('type');
$message = $rro->response('message');
$foo = $rro->response('data', 'foo');
$code = $rro->response('code');
$add = $rro->response('add', ['bar' => 'foo']);

方法

构建响应

type(string $string)

设置响应的类型。

message(string $string)

设置响应的消息。

data(array $data)

设置响应的数据。

code(int $code)

设置响应的状态码。

trans(string $translation)

使用翻译来设置 type,翻译本身放置在 message 中。

必须输入有效的翻译路径,否则将抛出 rroException

检查响应状态

isError()

响应是否为错误。

isSuccess()

响应是否成功。

访问响应详细信息

response('type')

获取响应类型。

response('message')

获取响应消息。

response('data', $dot)

获取响应数据数组(使用点语法检索特定键)。

示例: response('data', 'user.id')

response('code')

获取响应代码。

response('add', array $data)

将更多项目添加到数据数组(将自动合并到现有数据)。

response('is_type', $type)

响应类型是否等于发送的参数。

response('is_message', $message)

响应消息是否等于发送的参数。

渲染响应

HTML原始代码 toHtml()

将以以下方式输出消息和类型

<h1>{{ $message }}</h1>
<h3><code>{{ $type }}</code></h1>

所以这个片段

return success()
	->type('item_updated')
	->message('Item successfully updated!')
	->toHtml();

将产生

<h1>Item successfully updated!</h1>
<h3><code>item_update</code></h3>

如果 type 未设置,则不会渲染 h3 标签。

渲染为数组 toArray()

将响应渲染为简单数组(否则返回 rro 类的实例)。

所以这个片段

return success()
  ->type('item_updated')
  ->message('Item successfully updated!')
  ->toArray();

将产生

[
  "response" => [
    "type" => "item_updated",
    "message" => "Item successfully updated!",
  ]
];

对于Laravel响应 toResponse()

以Laravel的方式渲染响应。