pugx / bindto Goto Github PK
View Code? Open in Web Editor NEWSimple way to bind Request to DTO/Commands.
License: MIT License
Simple way to bind Request to DTO/Commands.
License: MIT License
Are there plans to release a stable version of this?
Can you give packagist a poke so it picks up v0.1.0?
I'd like to open a discussion on a new piece of functionality we need in our app. I provide a rationale and some possible solutions.
Feedback would be appreciated before I start implementing this.
To provide an automatic system for resolving object references to instances.
Our service layer accepts a lot of commands/dto's which contain references to entities or value objects in our domain. We have a lot of repetitive lookup code that we would like to get rid of.
It is essentially the functionality provided by Symfony ParamConverter but without the requirement to pass a Request object.
This feels very much like reinventing the wheel when there is a battle tested implementation that already exists but we've discussed it internally and don't see a way around it.
This would work similarly to PR #11 in that you would annotate a property on a DTO. The binder would then create an instance of the registered converter to produce an object instance. If the conversion process fails a violation would be added to the violation list.
The converter would accept the object identifier and convert it by whatever means necessary to an instance of that object or fail trying.
I provide two possible solutions for making this feature work.
In this example the naming and documentation of the properties are as if they are already instances. It is the most natural but may be a little confusing for the developer dispatching the DTO to the service layer as we're passing in object identifiers but the documentation (and auto-complete) says instances.
<?php
class MyDTO
{
public $name;
/**
* @var ValueObject\Country
* @Lookup(converter="country_converter")
*/
public $country;
/**
* @var Entity\User
* @Lookup(converter="doctrine_converter")
*/
public $user;
}
$myDto = new MyDTO();
$result = $binder->bind([
'name' => 'Some Person',
'country' => 1,
'user' => 1024
], $myDto);
if ($result->isValid()) {
echo($myDto->user->getName());
}
In this example the naming and documentation of the properties are more obvious for the consumer as to what the expectations on the service consuming the DTO are. Accessing the converted objects is more obtuse and lacks support for autocomplete.
<?php
class MyDTO
{
public $name;
/**
* @var int
* @Lookup(converter="country_converter")
*/
public $countryId;
/**
* @var int
* @Lookup(converter="doctrine_converter")
*/
public $userId;
}
$myDto = new MyDTO();
$result = $binder->bind([
'name' => 'Some Person',
'countryId' => 1,
'userId' => 1024
], $myDto);
if ($result->isValid()) {
echo($myDto->get('user')->getName());
}
A converter would follow a very simple interface and would be registered with the Binder to be made available.
<?php
interface ConverterInterface
{
/**
* @param mixed $input
* @param array options
* @throws ConverterException
* @returns mixed
*/
public function convert($input, array $options);
}
I noticed that bindto depends on "phpunit/phpunit": "^5.0@dev",
wich in turn depends on php 5.6
whereas binddto declares to depend on php >= 5.5
Which php version win?
Do we have to upgrade the php requirements or downgrade the phpunit dependency?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.