master

laravel/framework

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

CompilesErrors.php

TLDR

This file contains the CompilesErrors trait that is used in the Illuminate View Compilers namespace. The trait provides two methods: compileError() and compileEnderror(), which are used to compile error statements into valid PHP code.

Methods

compileError

This method is used to compile error statements into valid PHP code. It takes a string expression as input and returns a string. The method strips parentheses from the expression, and then generates PHP code that checks if the specified error message exists in the errors bag. If the error message exists, the message variable is set to the first error message in the bag.

compileEnderror

This method is used to compile the enderror statements into valid PHP code. It takes a string expression as input and returns a string. The method generates PHP code that unsets the message variable and restores the original message variable if it was set before.

<?php

namespace Illuminate\View\Compilers\Concerns;

trait CompilesErrors
{
    /**
     * Compile the error statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compileError($expression)
    {
        $expression = $this->stripParentheses($expression);

        return '<?php $__errorArgs = ['.$expression.'];
$__bag = $errors->getBag($__errorArgs[1] ?? \'default\');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>';
    }

    /**
     * Compile the enderror statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compileEnderror($expression)
    {
        return '<?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>';
    }
}