error_collector.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php namespace System\Validation;
  2. class Error_Collector {
  3. /**
  4. * All of the error messages.
  5. *
  6. * @var array
  7. */
  8. public $messages;
  9. /**
  10. * Create a new Error Collector instance.
  11. *
  12. * @return void
  13. */
  14. public function __construct($messages = array())
  15. {
  16. $this->messages = $messages;
  17. }
  18. /**
  19. * Add an error message to the collector.
  20. *
  21. * Duplicate messages will not be added.
  22. *
  23. * @param string $attribute
  24. * @param string $message
  25. * @return void
  26. */
  27. public function add($attribute, $message)
  28. {
  29. // -------------------------------------------------------------
  30. // Make sure the error message is not duplicated.
  31. //
  32. // For example, the Nullable rules can add a "required" message.
  33. // If the same message has already been added we don't want to
  34. // add it again.
  35. // -------------------------------------------------------------
  36. if ( ! array_key_exists($attribute, $this->messages) or ! is_array($this->messages[$attribute]) or ! in_array($message, $this->messages[$attribute]))
  37. {
  38. $this->messages[$attribute][] = $message;
  39. }
  40. }
  41. /**
  42. * Determine if errors exist for an attribute.
  43. *
  44. * @param string $attribute
  45. * @return bool
  46. */
  47. public function has($attribute)
  48. {
  49. return $this->first($attribute) !== '';
  50. }
  51. /**
  52. * Get the first error message for an attribute.
  53. *
  54. * @param string $attribute
  55. * @return string
  56. */
  57. public function first($attribute)
  58. {
  59. return (count($messages = $this->get($attribute)) > 0) ? $messages[0] : '';
  60. }
  61. /**
  62. * Get all of the error messages for an attribute.
  63. *
  64. * If no attribute is specified, all of the error messages will be returned.
  65. *
  66. * @param string $attribute
  67. * @param string $format
  68. * @return array
  69. */
  70. public function get($attribute = null, $format = ':message')
  71. {
  72. if (is_null($attribute))
  73. {
  74. return $this->all($format);
  75. }
  76. return (array_key_exists($attribute, $this->messages)) ? $this->format($this->messages[$attribute], $format) : array();
  77. }
  78. /**
  79. * Get all of the error messages.
  80. *
  81. * @param string $format
  82. * @return array
  83. */
  84. public function all($format = ':message')
  85. {
  86. $all = array();
  87. // ---------------------------------------------------------
  88. // Add each error message to the array of messages. Each
  89. // messages will have the specified format applied to it.
  90. // ---------------------------------------------------------
  91. foreach ($this->messages as $messages)
  92. {
  93. $all = array_merge($all, $this->format($messages, $format));
  94. }
  95. return $all;
  96. }
  97. /**
  98. * Format an array of messages.
  99. *
  100. * @param array $messages
  101. * @param string $format
  102. * @return array
  103. */
  104. private function format($messages, $format)
  105. {
  106. array_walk($messages, function(&$message, $key) use ($format) { $message = str_replace(':message', $message, $format); });
  107. return $messages;
  108. }
  109. }