master

laravel/framework

Last updated at: 29/12/2023 09:23

TrimStrings.php

TLDR

This file, TrimStrings.php, is part of the Illuminate\Foundation\Http\Middleware namespace. It contains the TrimStrings class, which is a middleware that trims the whitespace from incoming request data.

Methods

handle

The handle method is responsible for handling an incoming request. It loops through registered skip callbacks and if any of them return true, the method immediately returns the result of the next middleware in the stack. Otherwise, it calls the handle method of the parent class.

transform

The transform method is a protected method that trims the whitespace from the given value. It first checks if the key is in the $except array or the $neverTrim array, or if the value is not a string. If any of these conditions are met, the method returns the original value. Otherwise, it removes leading and trailing whitespace and other special characters using regular expressions or falls back to the trim function.

except

The except method allows specifying attributes that should never be trimmed. It accepts an array or string of attributes and adds them to the $neverTrim array.

skipWhen

The skipWhen method is used to register a callback that instructs the middleware to be skipped. It accepts a closure that will be added to the $skipCallbacks array.

flushState

The flushState method is used to clear the middleware's global state by resetting the $skipCallbacks array.

<?php

namespace Illuminate\Foundation\Http\Middleware;

use Closure;
use Illuminate\Support\Arr;

class TrimStrings extends TransformsRequest
{
    /**
     * The attributes that should not be trimmed.
     *
     * @var array<int, string>
     */
    protected $except = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    /**
     * The globally ignored attributes that should not be trimmed.
     *
     * @var array
     */
    protected static $neverTrim = [];

    /**
     * All of the registered skip callbacks.
     *
     * @var array
     */
    protected static $skipCallbacks = [];

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        foreach (static::$skipCallbacks as $callback) {
            if ($callback($request)) {
                return $next($request);
            }
        }

        return parent::handle($request, $next);
    }

    /**
     * Transform the given value.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @return mixed
     */
    protected function transform($key, $value)
    {
        $except = array_merge($this->except, static::$neverTrim);

        if (in_array($key, $except, true) || ! is_string($value)) {
            return $value;
        }

        return preg_replace('~^[\s\x{FEFF}\x{200B}]+|[\s\x{FEFF}\x{200B}]+$~u', '', $value) ?? trim($value);
    }

    /**
     * Indicate that the given attributes should never be trimmed.
     *
     * @param  array|string  $attributes
     * @return void
     */
    public static function except($attributes)
    {
        static::$neverTrim = array_values(array_unique(
            array_merge(static::$neverTrim, Arr::wrap($attributes))
        ));
    }

    /**
     * Register a callback that instructs the middleware to be skipped.
     *
     * @param  \Closure  $callback
     * @return void
     */
    public static function skipWhen(Closure $callback)
    {
        static::$skipCallbacks[] = $callback;
    }

    /**
     * Flush the middleware's global state.
     *
     * @return void
     */
    public static function flushState()
    {
        static::$skipCallbacks = [];
    }
}