ViewErrorBag.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Illuminate\Support;
  3. use Countable;
  4. use Illuminate\Contracts\Support\MessageBag as MessageBagContract;
  5. /**
  6. * @mixin \Illuminate\Contracts\Support\MessageBag
  7. */
  8. class ViewErrorBag implements Countable
  9. {
  10. /**
  11. * The array of the view error bags.
  12. *
  13. * @var array
  14. */
  15. protected $bags = [];
  16. /**
  17. * Checks if a named MessageBag exists in the bags.
  18. *
  19. * @param string $key
  20. * @return bool
  21. */
  22. public function hasBag($key = 'default')
  23. {
  24. return isset($this->bags[$key]);
  25. }
  26. /**
  27. * Get a MessageBag instance from the bags.
  28. *
  29. * @param string $key
  30. * @return \Illuminate\Contracts\Support\MessageBag
  31. */
  32. public function getBag($key)
  33. {
  34. return Arr::get($this->bags, $key) ?: new MessageBag;
  35. }
  36. /**
  37. * Get all the bags.
  38. *
  39. * @return array
  40. */
  41. public function getBags()
  42. {
  43. return $this->bags;
  44. }
  45. /**
  46. * Add a new MessageBag instance to the bags.
  47. *
  48. * @param string $key
  49. * @param \Illuminate\Contracts\Support\MessageBag $bag
  50. * @return $this
  51. */
  52. public function put($key, MessageBagContract $bag)
  53. {
  54. $this->bags[$key] = $bag;
  55. return $this;
  56. }
  57. /**
  58. * Determine if the default message bag has any messages.
  59. *
  60. * @return bool
  61. */
  62. public function any()
  63. {
  64. return $this->count() > 0;
  65. }
  66. /**
  67. * Get the number of messages in the default bag.
  68. *
  69. * @return int
  70. */
  71. #[\ReturnTypeWillChange]
  72. public function count()
  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. }