PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/particle/validator/src/MessageStack.php

https://gitlab.com/donyradjah/cvglobal
PHP | 182 lines | 77 code | 19 blank | 86 comment | 6 complexity | 068a7de46e4dc17537e6e29d0fd43968 MD5 | raw file
  1. <?php
  2. /**
  3. * Particle.
  4. *
  5. * @link http://github.com/particle-php for the canonical source repository
  6. * @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com)
  7. * @license https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
  8. */
  9. namespace Particle\Validator;
  10. /**
  11. * The MessageStack is responsible for keeping track of all validation messages, including their overwrites.
  12. *
  13. * @package Particle\Validator
  14. */
  15. class MessageStack
  16. {
  17. /**
  18. * Contains a list of all validation messages.
  19. *
  20. * @var array
  21. */
  22. protected $messages = [];
  23. /**
  24. * Contains an array of field and reason specific message overwrites.
  25. *
  26. * @var array
  27. */
  28. protected $overwrites = [];
  29. /**
  30. * Contains an array of global message overwrites.
  31. *
  32. * @var array
  33. */
  34. protected $defaultMessages = [];
  35. /**
  36. * @var Failure[]
  37. */
  38. protected $failures = [];
  39. /**
  40. * Will append an error message for the target $key with $reason to the stack.
  41. *
  42. * @param Failure $failure
  43. */
  44. public function append(Failure $failure)
  45. {
  46. $key = $failure->getKey();
  47. $reason = $failure->getReason();
  48. if (isset($this->defaultMessages[$reason])) {
  49. $failure->overwriteMessageTemplate($this->defaultMessages[$reason]);
  50. }
  51. if (isset($this->overwrites[$key][$reason])) {
  52. $failure->overwriteMessageTemplate($this->overwrites[$key][$reason]);
  53. }
  54. $this->failures[] = $failure;
  55. }
  56. /**
  57. * Returns an overwrite (either default or specific message) for the reason and key, or false.
  58. *
  59. * @param string $reason
  60. * @param string $key
  61. * @return string|bool
  62. */
  63. public function getOverwrite($reason, $key)
  64. {
  65. if ($this->hasOverwrite($key, $reason)) {
  66. return $this->overwrites[$key][$reason];
  67. }
  68. if (array_key_exists($reason, $this->defaultMessages)) {
  69. return $this->defaultMessages[$reason];
  70. }
  71. return false;
  72. }
  73. /**
  74. * Overwrite key-validator specific messages (so [first_name => [Length::TOO_SHORT => 'Message']]).
  75. *
  76. * @param array $messages
  77. * @return $this
  78. */
  79. public function overwriteMessages(array $messages)
  80. {
  81. $this->overwrites = $messages;
  82. return $this;
  83. }
  84. /**
  85. * Overwrite the default validator-specific messages (so [Length::TOO_SHORT => 'Generic message'].
  86. *
  87. * @param array $messages
  88. * @return $this
  89. */
  90. public function overwriteDefaultMessages(array $messages)
  91. {
  92. $this->defaultMessages = $messages;
  93. return $this;
  94. }
  95. /**
  96. * Merges an existing MessageStack into this one by taking over it's overwrites and defaults.
  97. *
  98. * @param MessageStack $messageStack
  99. */
  100. public function merge(MessageStack $messageStack)
  101. {
  102. $this->mergeDefaultMessages($messageStack);
  103. $this->mergeOverwrites($messageStack);
  104. }
  105. /**
  106. * Reset the messages to an empty array.
  107. *
  108. * @return $this
  109. */
  110. public function reset()
  111. {
  112. $this->failures = [];
  113. return $this;
  114. }
  115. /**
  116. * Merges the default messages from $messageStack to this MessageStack.
  117. *
  118. * @param MessageStack $messageStack
  119. */
  120. protected function mergeDefaultMessages(MessageStack $messageStack)
  121. {
  122. foreach ($messageStack->defaultMessages as $key => $message) {
  123. if (!array_key_exists($key, $this->defaultMessages)) {
  124. $this->defaultMessages[$key] = $message;
  125. }
  126. }
  127. }
  128. /**
  129. * Merges the message overwrites from $messageStack to this MessageStack.
  130. *
  131. * @param MessageStack $messageStack
  132. */
  133. protected function mergeOverwrites(MessageStack $messageStack)
  134. {
  135. foreach ($messageStack->overwrites as $key => $reasons) {
  136. foreach ($reasons as $reason => $message) {
  137. if (!$this->hasOverwrite($key, $reason)) {
  138. $this->overwrites[$key][$reason] = $message;
  139. }
  140. }
  141. }
  142. }
  143. /**
  144. * Returns whether an overwrite exists for the key $key with reason $reason.
  145. *
  146. * @param string $key
  147. * @param string $reason
  148. * @return bool
  149. */
  150. protected function hasOverwrite($key, $reason)
  151. {
  152. return isset($this->overwrites[$key][$reason]);
  153. }
  154. /**
  155. * Returns an array of all failures of the last validation run.
  156. *
  157. * @return Failure[]
  158. */
  159. public function getFailures()
  160. {
  161. return $this->failures;
  162. }
  163. }