Last active
January 4, 2017 10:32
-
-
Save tlfbrito/9aaff6055c02cde7d22c1a49b22c8250 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace YourNameSpace\Request; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
/** | |
* This class map request data into a DTO object | |
*/ | |
abstract class AbstractRequest | |
{ | |
/** | |
* @param array $data | |
*/ | |
public function __construct(array $data) | |
{ | |
$options = new OptionsResolver(); | |
$this->configureOptions($options); | |
$options->setDefined(array_keys($data)); | |
$options = $options->resolve($data); | |
$this->map($options); | |
} | |
/** | |
* @param OptionsResolver $options | |
*/ | |
abstract protected function configureOptions(OptionsResolver $options); | |
/** | |
* @param array $options | |
*/ | |
protected function map(array $options) | |
{ | |
// define any undefined options | |
foreach ($options as $prop) { | |
$this->$prop = $options[$prop]; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace YourNameSpace\Controller; | |
/** | |
* @Route("/foo") | |
*/ | |
class FooController extends BaseController | |
{ | |
/** | |
* Sample requests: | |
* - OK: api.com/foo/bars?lat=21.1211&lon=10&phone=912222222 | |
* - OK: api.com/foo/bars?lat=21.1211&lon=10&phone=912222222&country=en | |
* - OK: api.com/foo/bars?lat=21.1211&lon=10&phone=912222222&country=pt | |
* - Wrong: api.com/foo/bars?lat=21.1211&lon=10&phone=912222222&country=pl | |
* - Wrong: api.com/foo/bars?lat=aaaa&lon=10&phone=912222222&country=pt | |
* | |
* @Route("/bars") | |
*/ | |
public function barsAction() | |
{ | |
$request = new GetSearchFilterRequest($this->getRequest()->query->all()); | |
$request->lat(); | |
$request->country(); | |
//... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace YourNameSpace\Request; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
/** | |
* DTO example to handle Search filter requests | |
*/ | |
class GetSearchFilterRequest extends AbstractRequest | |
{ | |
protected $lat; | |
protected $lon; | |
protected $country; | |
protected $phone; | |
/** | |
* @param array $data | |
*/ | |
public function __construct(array $data = []) | |
{ | |
parent::__construct($data); | |
} | |
/** | |
* @return float | |
*/ | |
public function lat(): float | |
{ | |
return $this->lat; | |
} | |
/** | |
* @return float | |
*/ | |
public function lon(): float | |
{ | |
return $this->lon; | |
} | |
/** | |
* @return string | |
*/ | |
public function country(): string | |
{ | |
return $this->country; | |
} | |
/** | |
* @return int | |
*/ | |
public function phone(): int | |
{ | |
return $this->phone; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function map(array $options) | |
{ | |
//if the query parameters have the same name as the properties, this isn't required | |
$this->lat = $options['lat']; | |
$this->lon = $options['lon']; | |
$this->country = $options['country']; | |
$this->phone = $options['phone']; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function configureOptions(OptionsResolver $options) | |
{ | |
$options->setRequired( | |
[ | |
'lat', | |
'lon' | |
] | |
); | |
$resolver->setAllowedTypes(array( | |
'lat' => array('numeric'), | |
'lon' => array('numeric'), | |
'phone' => array('numeric') | |
)); | |
$options->setDefaults(['country' => 'en']); | |
$resolver->setAllowedValues(array( | |
'country' => array('pt', 'en'), | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment