master

laravel/framework

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

CanBePrecognitive.php

TLDR

The CanBePrecognitive trait provides methods for handling precognition in HTTP requests.

Methods

filterPrecognitiveRules

Filter the given array of rules into an array of rules that are included in precognitive headers.

  • Parameters:

    • $rules (array): The array of rules to filter.
  • Returns:

    • (array): The filtered array of rules.

isAttemptingPrecognition

Determine if the request is attempting to be precognitive.

  • Returns:
    • (bool): true if the request is attempting to be precognitive, false otherwise.

isPrecognitive

Determine if the request is precognitive.

  • Returns:
    • (bool): true if the request is precognitive, false otherwise.
<?php

namespace Illuminate\Http\Concerns;

use Illuminate\Support\Collection;

trait CanBePrecognitive
{
    /**
     * Filter the given array of rules into an array of rules that are included in precognitive headers.
     *
     * @param  array  $rules
     * @return array
     */
    public function filterPrecognitiveRules($rules)
    {
        if (! $this->headers->has('Precognition-Validate-Only')) {
            return $rules;
        }

        return Collection::make($rules)
            ->only(explode(',', $this->header('Precognition-Validate-Only')))
            ->all();
    }

    /**
     * Determine if the request is attempting to be precognitive.
     *
     * @return bool
     */
    public function isAttemptingPrecognition()
    {
        return $this->header('Precognition') === 'true';
    }

    /**
     * Determine if the request is precognitive.
     *
     * @return bool
     */
    public function isPrecognitive()
    {
        return $this->attributes->get('precognitive', false);
    }
}