ViewErrorBag.php 2.6KB

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. public function count()
  72. {
  73. return $this->getBag('default')->count();
  74. }
  75. /**
  76. * Dynamically call methods on the default bag.
  77. *
  78. * @param string $method
  79. * @param array $parameters
  80. * @return mixed
  81. */
  82. public function __call($method, $parameters)
  83. {
  84. return $this->getBag('default')->$method(...$parameters);
  85. }
  86. /**
  87. * Dynamically access a view error bag.
  88. *
  89. * @param string $key
  90. * @return \Illuminate\Contracts\Support\MessageBag
  91. */
  92. public function __get($key)
  93. {
  94. return $this->getBag($key);
  95. }
  96. /**
  97. * Dynamically set a view error bag.
  98. *
  99. * @param string $key
  100. * @param \Illuminate\Contracts\Support\MessageBag $value
  101. * @return void
  102. */
  103. public function __set($key, $value)
  104. {
  105. $this->put($key, $value);
  106. }
  107. /**
  108. * Convert the default bag to its string representation.
  109. *
  110. * @return string
  111. */
  112. public function __toString()
  113. {
  114. return (string) $this->getBag('default');
  115. }
  116. }