Skip to content

Instantly share code, notes, and snippets.

@tlfbrito
Last active January 4, 2017 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tlfbrito/9aaff6055c02cde7d22c1a49b22c8250 to your computer and use it in GitHub Desktop.
Save tlfbrito/9aaff6055c02cde7d22c1a49b22c8250 to your computer and use it in GitHub Desktop.
<?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];
}
}
}
<?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();
//...
}
}
<?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