codingculture/request-resolver-bundle

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

3.0.2 2024-03-27 10:04 UTC

This package is auto-updated.

Last update: 2024-03-27 10:05:04 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