<?php

declare(strict_types=1);

namespace App\Http\Requests\Concerns;

trait NormalizesBooleanQueryParams
{
    /**
     * Accept query-string booleans like "true"/"false" for Laravel's boolean rule.
     *
     * @param  list<string>  $keys
     */
    protected function normalizeBooleanQueryParams(array $keys = ['noPaginate']): void
    {
        $payload = [];

        foreach ($keys as $key) {
            if (! $this->exists($key)) {
                continue;
            }

            $value = $this->input($key);

            if (is_bool($value) || is_int($value)) {
                continue;
            }

            $normalized = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);

            if ($normalized !== null) {
                $payload[$key] = $normalized;
            }
        }

        if ($payload !== []) {
            $this->merge($payload);
        }
    }
}
