2017 © Pedro Peláez
 

library bindto

Simple way to bind Request to DTO/Commands

image

myschoolmanagement/bindto

Simple way to bind Request to DTO/Commands

  • Wednesday, January 31, 2018
  • by iainmckay
  • Repository
  • 7 Watchers
  • 0 Stars
  • 46 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 6 Forks
  • 0 Open issues
  • 15 Versions
  • 10 % Grown

The README.md

Bindto

Simplest way to bind Request to DTO/Commands.

Bindto helps you to work with API and data validation using DTO and Commands., (*1)

Is really fast (it doesn't use Reflection) and binds the Request against your class., (*2)

It's the smartest way to stop using the slow and complex Symfony Form component for API., (*3)

Install

composer require pugx/bindto, (*4)

Usage

Example, you have to create a Post/Patch/Put Api, (*5)

1. Create a simple class that is the body of a request

protip: you can use with Symfony validation component annotations

``` php, (*6)

use Symfony\Component\Validator\Constraints as Assert;, (*7)

Class CreateFeedback {, (*8)

/**
 * @Assert\NotBlank(groups={"POST"})
 * @Assert\Type(type="string")
 */
public $subject;

/**
 * @Assert\NotNull(groups={"POST"})
 * @Assert\Type(type="string")
 * @Assert\Length(
 *      min = 10,
 *      max = 500)
 */
public $body;

}, (*9)


### 2. In your controller if you use Silex you may want to enforce the input validation, improving the example at http://silex.sensiolabs.org/doc/usage.html#example-post-route: ``` php require_once __DIR__.'/../vendor/autoload.php'; use Bindto\Binder; $app = new Silex\Application(); $app->post('/feedback', function (Request $request) { $binder = Binder::createSimpleProductionBinder(); $result = $binder->bind($request, CreateFeedback::class); if (!$result->isValid()) { throw new \Exception($result->getViolations()); } $createFeedBack = $result->getData(); mail($createFeedBack->subject, '[YourSite] Feedback', $createFeedBack->body); return new Response('Thank you for your feedback!', 201); }); $app->run();

PATCH support with partial modification

Use the validation groups if you want to PATCH partially:, (*10)

``` php /** * @Assert\NotNull(groups={"POST"}) * @Assert\Type(type="string") * @Assert\Length( * min = 10, * max = 500) */ public $body;, (*11)


With a POST request all the assertions will be used, with a PUT and PATCH only the `Type` and `Length` assertions. ## TODO - recursive binder? - collection? - twig helper? - tests decouple maptest from bindertest - silex provider? - sf bundle? ## Run tests ``` bash composer dump-autoload bin/phpunit

The Versions

31/01 2018

dev-master

9999999-dev

Simple way to bind Request to DTO/Commands

  Sources   Download

MIT

The Requires

 

The Development Requires

by Giulio De Donato

31/01 2018

0.4.4

0.4.4.0

Simple way to bind Request to DTO/Commands

  Sources   Download

MIT

The Requires

 

The Development Requires

by Giulio De Donato

22/12 2017

0.4.3

0.4.3.0

Simple way to bind Request to DTO/Commands

  Sources   Download

MIT

The Requires

 

The Development Requires

by Giulio De Donato

05/05 2017

0.4.2

0.4.2.0

Simple way to bind Request to DTO/Commands

  Sources   Download

MIT

The Requires

 

The Development Requires

by Giulio De Donato

07/04 2017

0.4.1

0.4.1.0

Simple way to bind Request to DTO/Commands

  Sources   Download

MIT

The Requires

 

The Development Requires

by Giulio De Donato

17/03 2017

dev-Add-translation-key

dev-Add-translation-key

Simple way to bind Request to DTO/Commands

  Sources   Download

MIT

The Requires

 

The Development Requires

by Giulio De Donato

28/02 2017

0.4.0

0.4.0.0

Simple way to bind Request to DTO/Commands

  Sources   Download

MIT

The Requires

 

The Development Requires

by Giulio De Donato

23/02 2017

0.3.1

0.3.1.0

Simple way to bind Request to DTO/Commands

  Sources   Download

MIT

The Requires

 

The Development Requires

by Giulio De Donato

16/02 2017

0.3.0

0.3.0.0

Simple way to bind Request to DTO/Commands

  Sources   Download

MIT

The Requires

 

The Development Requires

by Giulio De Donato

25/01 2017

0.2.0

0.2.0.0

Simple way to bind Request to DTO/Commands

  Sources   Download

MIT

The Requires

 

The Development Requires

by Giulio De Donato

25/01 2017

dev-feature-casting-object-mapper

dev-feature-casting-object-mapper

Simple way to bind Request to DTO/Commands

  Sources   Download

MIT

The Requires

 

The Development Requires

by Giulio De Donato

25/01 2017

dev-feature-converters

dev-feature-converters

Simple way to bind Request to DTO/Commands

  Sources   Download

MIT

The Requires

 

The Development Requires

by Giulio De Donato

12/05 2016

dev-bug-request-get-method

dev-bug-request-get-method

Simple way to bind Request to DTO/Commands

  Sources   Download

MIT

The Requires

 

The Development Requires

by Giulio De Donato

02/05 2016

dev-changed-member-accessibility

dev-changed-member-accessibility

Simple way to bind Request to DTO/Commands

  Sources   Download

MIT

The Requires

 

The Development Requires

by Giulio De Donato

20/04 2016

v0.1.0

0.1.0.0

Simple way to bind Request to DTO/Commands

  Sources   Download

MIT

The Requires

 

The Development Requires

by Giulio De Donato