1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 
<?php

declare(strict_types=1);

namespace Wtf\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Wtf\Root;

/**
 * Add `filters` key into container with current filters.
 *
 * @see https://www.slimframework.com/docs/concepts/middleware.html
 *
 * @example https://example.com?filter[created_at]=2017-05-05
 */
class Filters extends Root
{
    /**
     * @see https://www.slimframework.com/docs/concepts/middleware.html
     *
     * @param ServerRequestInterface $request
     * @param ResponseInterface      $response
     * @param callable               $next
     *
     * @return ResponseInterface
     */
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface
    {
        $this->container['filters'] = $this->process($request);

        return $next($request, $response);
    }

    /**
     * Process filters from request.
     *
     * @param ServerRequestInterface $request
     *
     * @return array
     */
    protected function process(ServerRequestInterface $request): array
    {
        $filters = $request->getQueryParam('filter') ?? [];
        $limit = [0, 100];

        // Prepare limit and offset
        if ($request->getQueryParam('offset')) {
            $limit[0] = $filters['offset'];
        }
        if ($request->getQueryParam('limit')) {
            $limit[1] = $filters['limit'];
        }

        // Fix filter symbols issue (like missing "]")
        foreach ($filters as $field => $value) {
            if ('null' === $value) {
                $filters[$field][$key] = null;
            }
            if (\strpos($field, '[')) {
                $filters[$field.']'] = $value;
                unset($filters[$field]);
            }
        }

        $filters['LIMIT'] = $limit;

        return $filters;
    }
}