phprest / phprest-service-request-filter
Phprest Request Filter Service.
Requires
- php: >=5.4.0
- phprest/phprest: dev-master
This package is not auto-updated.
Last update: 2025-01-04 17:15:40 UTC
README
Description
Request Filter Service which is able to filter your incoming request by:
- query
- sort
- limit
- offset
Components
There are 3 main component in filtering:
- Obtainer
- Parser
- Processor
Obtainer
Is the way as you get the filtering key and its data from the request.
E.g.: QueryInHeader Obtainer
Parser
Is the way how you analyze the obtained filter.
E.g.: CommaMinus Parser
Processor
Which is processing your parsed filter.
E.g.: Orm Processor
Installation
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.
Usage
Configuration
For the configuration you should check the Config class.
Registration
<?php use Phprest\Service\RequestFilter; # ... /** @var \Phprest\Application $app */ $app->registerService(new RequestFilter\Service(), new RequestFilter\Config()); # ...
Reaching from a Controller
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(...); } }
Utils
Most of the Services in Phprest provides some utility mechanism (helper functions).
For the utilities you should check the Util class.
Example
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()]); } } }