master

laravel/framework

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

ViewErrorBag.php

TLDR

The ViewErrorBag class in the file ViewErrorBag.php provides an implementation of a view error bag. It allows you to manage multiple instances of MessageBag for different views.

Methods

hasBag($key = 'default')

This method checks if a named MessageBag exists in the ViewErrorBag instance.

  • Parameter:
    • $key (string): The key/name of the MessageBag to check. Default is 'default'.
  • Returns:
    • (bool): Returns true if the MessageBag exists, false otherwise.

getBag($key)

This method retrieves a specific named MessageBag instance from the ViewErrorBag.

  • Parameters:
    • $key (string): The key/name of the MessageBag to retrieve.
  • Returns:
    • (\Illuminate\Contracts\Support\MessageBag): The requested MessageBag instance.

getBags()

This method retrieves all the bags in the ViewErrorBag instance.

  • Returns:
    • (array): An array containing all the bags in the ViewErrorBag.

put($key, MessageBagContract $bag)

This method adds a new MessageBag instance to the ViewErrorBag instance.

  • Parameters:
    • $key (string): The key/name to associate with the MessageBag.
    • $bag (\Illuminate\Contracts\Support\MessageBag): The MessageBag instance to add.
  • Returns:
    • ($this): Returns the ViewErrorBag instance for method chaining.

any()

This method checks if the default message bag has any messages.

  • Returns:
    • (bool): Returns true if there are messages, false otherwise.

count(): int

This method gets the number of messages in the default bag.

  • Returns:
    • (int): The number of messages in the default bag.

__call($method, $parameters)

This method allows you to dynamically call methods on the default bag.

  • Parameters:
    • $method (string): The method name to call.
    • $parameters (array): The parameters to pass to the method.
  • Returns:
    • (mixed): The result of the method call.

__get($key)

This method allows you to dynamically access a view error bag by name.

  • Parameters:
    • $key (string): The key/name of the view error bag to access.
  • Returns:
    • (\Illuminate\Contracts\Support\MessageBag): The requested view error bag.

__set($key, $value)

This method allows you to dynamically set a view error bag.

  • Parameters:
    • $key (string): The key/name of the view error bag to set.
    • $value (\Illuminate\Contracts\Support\MessageBag): The view error bag instance to set.

__toString()

This method converts the default bag to its string representation.

  • Returns:
    • (string): The string representation of the default bag.
<?php

namespace Illuminate\Support;

use Countable;
use Illuminate\Contracts\Support\MessageBag as MessageBagContract;
use Stringable;

/**
 * @mixin \Illuminate\Contracts\Support\MessageBag
 */
class ViewErrorBag implements Countable, Stringable
{
    /**
     * The array of the view error bags.
     *
     * @var array
     */
    protected $bags = [];

    /**
     * Checks if a named MessageBag exists in the bags.
     *
     * @param  string  $key
     * @return bool
     */
    public function hasBag($key = 'default')
    {
        return isset($this->bags[$key]);
    }

    /**
     * Get a MessageBag instance from the bags.
     *
     * @param  string  $key
     * @return \Illuminate\Contracts\Support\MessageBag
     */
    public function getBag($key)
    {
        return Arr::get($this->bags, $key) ?: new MessageBag;
    }

    /**
     * Get all the bags.
     *
     * @return array
     */
    public function getBags()
    {
        return $this->bags;
    }

    /**
     * Add a new MessageBag instance to the bags.
     *
     * @param  string  $key
     * @param  \Illuminate\Contracts\Support\MessageBag  $bag
     * @return $this
     */
    public function put($key, MessageBagContract $bag)
    {
        $this->bags[$key] = $bag;

        return $this;
    }

    /**
     * Determine if the default message bag has any messages.
     *
     * @return bool
     */
    public function any()
    {
        return $this->count() > 0;
    }

    /**
     * Get the number of messages in the default bag.
     *
     * @return int
     */
    public function count(): int
    {
        return $this->getBag('default')->count();
    }

    /**
     * Dynamically call methods on the default bag.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return mixed
     */
    public function __call($method, $parameters)
    {
        return $this->getBag('default')->$method(...$parameters);
    }

    /**
     * Dynamically access a view error bag.
     *
     * @param  string  $key
     * @return \Illuminate\Contracts\Support\MessageBag
     */
    public function __get($key)
    {
        return $this->getBag($key);
    }

    /**
     * Dynamically set a view error bag.
     *
     * @param  string  $key
     * @param  \Illuminate\Contracts\Support\MessageBag  $value
     * @return void
     */
    public function __set($key, $value)
    {
        $this->put($key, $value);
    }

    /**
     * Convert the default bag to its string representation.
     *
     * @return string
     */
    public function __toString()
    {
        return (string) $this->getBag('default');
    }
}