ViewErrorBag.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Illuminate\Support;
  3. use Countable;
  4. use Illuminate\Contracts\Support\MessageBag as MessageBagContract;
  5. use Stringable;
  6. /**
  7. * @mixin \Illuminate\Contracts\Support\MessageBag
  8. */
  9. class ViewErrorBag implements Countable, Stringable
  10. {
  11. /**
  12. * The array of the view error bags.
  13. *
  14. * @var array<string, \Illuminate\Contracts\Support\MessageBag>
  15. */
  16. protected $bags = [];
  17. /**
  18. * Checks if a named MessageBag exists in the bags.
  19. *
  20. * @param string $key
  21. * @return bool
  22. */
  23. public function hasBag($key = 'default')
  24. {
  25. return isset($this->bags[$key]);
  26. }
  27. /**
  28. * Get a MessageBag instance from the bags.
  29. *
  30. * @param string $key
  31. * @return \Illuminate\Contracts\Support\MessageBag
  32. */
  33. public function getBag($key)
  34. {
  35. return Arr::get($this->bags, $key) ?: new MessageBag;
  36. }
  37. /**
  38. * Get all the bags.
  39. *
  40. * @return array<string, \Illuminate\Contracts\Support\MessageBag>
  41. */
  42. public function getBags()
  43. {
  44. return $this->bags;
  45. }
  46. /**
  47. * Add a new MessageBag instance to the bags.
  48. *
  49. * @param string $key
  50. * @param \Illuminate\Contracts\Support\MessageBag $bag
  51. * @return $this
  52. */
  53. public function put($key, MessageBagContract $bag)
  54. {
  55. $this->bags[$key] = $bag;
  56. return $this;
  57. }
  58. /**
  59. * Determine if the default message bag has any messages.
  60. *
  61. * @return bool
  62. */
  63. public function any()
  64. {
  65. return $this->count() > 0;
  66. }
  67. /**
  68. * Get the number of messages in the default bag.
  69. *
  70. * @return int
  71. */
  72. public function count(): int
  73. {
  74. return $this->getBag('default')->count();
  75. }
  76. /**
  77. * Dynamically call methods on the default bag.
  78. *
  79. * @param string $method
  80. * @param array $parameters
  81. * @return mixed
  82. */
  83. public function __call($method, $parameters)
  84. {
  85. return $this->getBag('default')->$method(...$parameters);
  86. }
  87. /**
  88. * Dynamically access a view error bag.
  89. *
  90. * @param string $key
  91. * @return \Illuminate\Contracts\Support\MessageBag
  92. */
  93. public function __get($key)
  94. {
  95. return $this->getBag($key);
  96. }
  97. /**
  98. * Dynamically set a view error bag.
  99. *
  100. * @param string $key
  101. * @param \Illuminate\Contracts\Support\MessageBag $value
  102. * @return void
  103. */
  104. public function __set($key, $value)
  105. {
  106. $this->put($key, $value);
  107. }
  108. /**
  109. * Convert the default bag to its string representation.
  110. *
  111. * @return string
  112. */
  113. public function __toString()
  114. {
  115. return (string) $this->getBag('default');
  116. }
  117. }