-
Notifications
You must be signed in to change notification settings - Fork 2
Implement your own handler
Implementing your own handler is really easy.
All you have to do is create a PHP object and implement the PatchOperationHandler interface.
namespace PatchManager;
use Symfony\Component\OptionsResolver\OptionsResolver;
interface PatchOperationHandler
{
/**
* implement here the logic for the handler
*
* @param mixed $subject
* @param OperationData $operationData
*/
public function handle($subject, OperationData $operationData);
/**
* the operation name
*
* @return string
*/
public function getName();
/**
* use the OptionResolver instance to configure the required
* and optional fields that needs to be passed with the request body.
* See http://symfony.com/doc/current/components/options_resolver.html
* to check all possible options
*
* @param OptionsResolver $optionsResolver
*/
public function configureOptions(OptionsResolver $optionsResolver);
}
handle is the method that gets called when an operation is matched. It receives the subject (a Patchable interface) and an instance of OperationData
Be careful! OperationData is an instance of PhpCollection\Map which returns PhpOptions when you get keys. So for example if your operation data contains a key named value, to get the value you should do:
$operationData->get('value')->get();
// for not required values you could do great things like:
$operationData->get('value')->getOrElse('default value');
$operationData->get('value')->getOrCall(function() {
return rand(1,100);
});
check the PhpCollection docs for more informations.
getName should return the operation name
configureOptions receives an OptionResolver interface to define what parameters should or could be passed along with the operation name. The "op" key is validated by the library. See the Data Handler and the FiniteHandler for reference. Also have a look to the Option Resolver Component docs.
Then just add the handler to the instance of OperationMatcher while constructing the PatchManager instance:
$operations = new Operations();
$operations->setRequestBody(
'{"op": "data", "property": "a", "value": 2}'
);
$operationMatcher = new OperationMatcher($operations);
$myCustomHandler = new MyCustomHandlerHandler(); # this line
$operationMatcher->addHandler($myCustomHandler); # and this one adds your handler
$pm = new PatchManager($operationMatcher);
Use the table of contents to the top right to navigate to other wiki sections.