Request Filter Service which is able to filter your incoming request by:
- query
- sort
- limit
- offset
There are 3 main component in filtering:
- Obtainer
- Parser
- Processor
Is the way as you get the filtering key and its data from the request.
E.g.: QueryInHeader Obtainer
Is the way how you analyze the obtained filter.
E.g.: CommaMinus Parser
Which is processing your parsed filter.
E.g.: Orm Processor
Install it through composer.
{
"require": {
"phprest/phprest-service-request-filter": "@stable"
}
}
tip: you should browse the phprest/phprest-service-request-filter
page to choose a stable version to use, avoid the @stable
meta constraint.
For the configuration you should check the Config class.
<?php
use Phprest\Service\RequestFilter;
# ...
/** @var \Phprest\Application $app */
$app->registerService(new RequestFilter\Service(), new RequestFilter\Config());
# ...
To reach your Service from a Controller you should use the Service's Getter Trait.
<?php namespace App\Module\Controller;
use Phprest\Service;
class Index extends \Phprest\Util\Controller
{
use Service\RequestFilter\Getter;
public function get(Request $request)
{
$this->serviceRequestFilter()->processQuery(...);
}
}
Most of the Services in Phprest provides some utility mechanism (helper functions).
For the utilities you should check the Util class.
With the Orm Processor.
<?php namespace App\Module\Controller;
use Phprest\Service;
use Doctrine\Common\Collections\Criteria;
class Index extends \Phprest\Util\Controller
{
use Service\RequestFilter\Getter;
use Service\RequestFilter\Util;
public function getAll(Request $request)
{
$processor = new Service\RequestFilter\Processor\Orm(Criteria::create());
try {
$this->serviceRequestFilter()->processQuery($request, $processor);
$this->serviceRequestFilter()->processSort($request, $processor);
} catch (\Exception $e) {
throw new Exception\BadRequest(0, [$e->getMessage()]);
}
}
}