commonhelpers/common-response

PHP 类 Response,作为其他类的结果

0.1.3 2021-11-13 20:29 UTC

This package is auto-updated.

Last update: 2024-09-23 00:02:39 UTC


README

如果你想要获取操作的结果和消息的布尔值,应该使用 CommonHelpers\Response 类。此外,你还有机会获取布尔值和消息之外的任何数据。这对 API 非常方便。

<?php

namespace App;

use CommonHelpers\ObjectResponse\Response;

class ApiController
{
    private $response;

    public function __construct()
    {
        $this->response = Response::fail();
    }

    public function run(int $a, int $b): Response
    {
        $this->response->setResult($a > $b);

        $this->response->isSuccess() ? $this->response->setMessage('ok')
                                     : $this->response->setMessage('fail')
        ;

        $this->response->payload = $a * $b;

        return $this->response;
    }
}