master

laravel/framework

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

CreatesRegularExpressionRouteConstraints.php

TLDR

This file is a trait called CreatesRegularExpressionRouteConstraints which provides several methods for specifying route parameter constraints using regular expressions. The trait is intended to be used in the Illuminate\Routing\Router class or a class that extends it.

Methods

whereAlpha($parameters)

This method specifies that the given route parameters must be alphabetic. It takes either an array or a string parameter and returns an instance of the class it is called on.

whereAlphaNumeric($parameters)

This method specifies that the given route parameters must be alphanumeric. It takes either an array or a string parameter and returns an instance of the class it is called on.

whereNumber($parameters)

This method specifies that the given route parameters must be numeric. It takes either an array or a string parameter and returns an instance of the class it is called on.

whereUlid($parameters)

This method specifies that the given route parameters must be ULIDs (Universally Unique Lexicographically Sortable Identifiers). It takes either an array or a string parameter and returns an instance of the class it is called on.

whereUuid($parameters)

This method specifies that the given route parameters must be UUIDs (Universally Unique Identifiers). It takes either an array or a string parameter and returns an instance of the class it is called on.

whereIn($parameters, $values)

This method specifies that the given route parameters must be one of the given values. It takes either an array or a string parameter and an array of values, and returns an instance of the class it is called on.

assignExpressionToParameters($parameters, $expression)

This protected method applies the given regular expression to the given parameters. It takes either an array or a string parameter and a string expression, and returns an instance of the class it is called on.

<?php

namespace Illuminate\Routing;

use Illuminate\Support\Arr;

trait CreatesRegularExpressionRouteConstraints
{
    /**
     * Specify that the given route parameters must be alphabetic.
     *
     * @param  array|string  $parameters
     * @return $this
     */
    public function whereAlpha($parameters)
    {
        return $this->assignExpressionToParameters($parameters, '[a-zA-Z]+');
    }

    /**
     * Specify that the given route parameters must be alphanumeric.
     *
     * @param  array|string  $parameters
     * @return $this
     */
    public function whereAlphaNumeric($parameters)
    {
        return $this->assignExpressionToParameters($parameters, '[a-zA-Z0-9]+');
    }

    /**
     * Specify that the given route parameters must be numeric.
     *
     * @param  array|string  $parameters
     * @return $this
     */
    public function whereNumber($parameters)
    {
        return $this->assignExpressionToParameters($parameters, '[0-9]+');
    }

    /**
     * Specify that the given route parameters must be ULIDs.
     *
     * @param  array|string  $parameters
     * @return $this
     */
    public function whereUlid($parameters)
    {
        return $this->assignExpressionToParameters($parameters, '[0-7][0-9a-hjkmnp-tv-zA-HJKMNP-TV-Z]{25}');
    }

    /**
     * Specify that the given route parameters must be UUIDs.
     *
     * @param  array|string  $parameters
     * @return $this
     */
    public function whereUuid($parameters)
    {
        return $this->assignExpressionToParameters($parameters, '[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}');
    }

    /**
     * Specify that the given route parameters must be one of the given values.
     *
     * @param  array|string  $parameters
     * @param  array  $values
     * @return $this
     */
    public function whereIn($parameters, array $values)
    {
        return $this->assignExpressionToParameters($parameters, implode('|', $values));
    }

    /**
     * Apply the given regular expression to the given parameters.
     *
     * @param  array|string  $parameters
     * @param  string  $expression
     * @return $this
     */
    protected function assignExpressionToParameters($parameters, $expression)
    {
        return $this->where(collect(Arr::wrap($parameters))
                    ->mapWithKeys(fn ($parameter) => [$parameter => $expression])
                    ->all());
    }
}