codingculture/request-resolver-bundle

Joins the power of symfony OptionsResolver and Requests to create a clean API for your requests

3.0.3 2024-11-18 09:45 UTC

README

Build Status Codacy Badge Latest Stable Version Total Downloads

This Symfony bundle tries to make request assertion a little bit easier.

This bundle is still a work in progress.

PHP7.1+ only.

Usage

<?php

...

class SomeController extends Controller
{
    public function someAction()
    {
        $request = $this->get('codingculture.requestresolver.resolver')->resolve(new SomeRequest());
        
        $request->getId();
    }
}
<?php

...

final class SomeRequest implements ResolvableRequestInterface
{
    private $options = [];
    
    public function getId(): string
    {
        return $this->options['id'];
    }

    public function defineOptions(OptionsResolver $resolver): OptionsResolver
    {
        $resolver->setRequired('id')
    }
    
    public function setOptions(array $options)
    {
        $this->options = $options;
    }
    
    public function getContentType(): string
    {
        return RequestResolver::CONTENT_TYPE_ALLOW_ALL;
    }
}

If a user sent a bad request, an InvalidArgumentException (or an extension of it) will be thrown on resolve method.

Todo

  • Write better docs