定制 pioniro/request-response-model-converter-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pioniro/request-response-model-converter-bundle

最新稳定版本:1.0.1

Composer 安装命令:

composer require pioniro/request-response-model-converter-bundle

包简介

Serializes and unserializes your request and response models

README 文档

README

This is a bundle, that does this magic:

before:

// Controller/MyAwfulController.php
namespace App\Controller;

use \Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;


class MyAwfulController extends AbstractController
{
    public function sum(Request $request)
    {
        if($request->isMethod('POST')) {
            $a = $request->request->getDigits('a');
            $b = $request->request->getDigits('b');
        } else {
            $a = $request->query->getDigits('a');
            $b = $request->query->getDigits('b');
        }
        if(is_null($a) || is_null($b)) {
            throw new RuntimeException('a and b should not be null');
        }
        return new JsonResponse(['result' => floatval($a) + floatval($b)]);
    }
}

Or using the forms. Awful, isn't it?

after:

// Model\Request\SumRequest.php
namespace App\Model\Request;

use JMS\Serializer\Annotation as Serializer;
use Pioniro\RequestResponseModel\RequestModelInterface;
use Symfony\Component\Validator\Constraints as Assert;

class SumRequest implements RequestModelInterface
{
    /**
    * @var float
    * @Serializer\Type("float")
    * @Assert\NotNull()
    */
    protected $a;

    /**
    * @var float
    * @Serializer\Type("float")
    * @Assert\NotNull()
    */
    protected $b;

    public function getA(): float
    {
        return $this->a;
    }

    public function getB(): float
    {
        return $this->b;
    }

}
// Model\Request\ResultResponse.php
namespace App\Model\Response;

use JMS\Serializer\Annotation as Serializer;
use Pioniro\RequestResponseModel\RequestModelInterface;
use Pioniro\RequestResponseModel\ResponseModelInterface;
use Symfony\Component\Validator\Constraints as Assert;

class ResultResponse implements ResponseModelInterface
{
    /**
    * @var float
    * @Serializer\Type("float")
    */
    protected $result;

    public function __construct(float $result) {
        $this->result = $result;
    }
}

// Controller/MyAwesomeController.php
namespace App\Controller;

use \Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;


class MyAwesomeController extends AbstractController
{
    public function sum(SumRequest $request)
    {
        return new ResultResponse($request->getA() + $request->getB());
    }
}

Did you see this? No manual validation, no manual model filling, no working with exception, no badly testable Response! Just your logic.

That's why this bundle is.

统计信息

  • 总下载量: 191
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-03