ilexn/slim-route-strategies

Slim 4 route strategies for parameter upcasting

0.1.2 2019-06-10 13:20 UTC

This package is auto-updated.

Last update: 2024-10-15 02:27:15 UTC


README

Latest Stable Version Total Downloads

Coverage Status Build Status Scrutinizer Code Quality Infection MSI

Slim 4 route strategies for parameter upcasting

Install

Via Composer

$ composer require ilexn/slim-route-strategies

Usage

$resolver = new RouteArgsResolver();
$resolver->add(new Case1());

$app = AppFactory::create();
$routeCollector = $app->getRouteCollector();
$routeCollector->setDefaultInvocationStrategy($resolver);

$app->get('/hello/{name}', function ($request, $response, Name $name) {
    $response->getBody()->write("Hello ". $name->getName());
    return $response;
});
final class Case1 implements RouteArgsResolverInterface
{
    public function __invoke(string $value): int
    {
        return 1;
    }

    public function get(string $value): Name
    {
        return new Name($value);
    }
    
    /**
    * @return callable[]
    */
    public function getArgsResolver(): array
    {
        return [
            'name' => [$this, 'get'],
            'b' => static function ($value) {
                return $value;
            },
            'c' => $this,
        ];
    }
}