message.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php namespace System\Validation;
  2. use System\Str;
  3. use System\Lang;
  4. class Message {
  5. /**
  6. * Get the appropriate validation message for a rule attribute.
  7. *
  8. * @param Rule $rule
  9. * @param string $attribute
  10. * @return string
  11. */
  12. public static function get($rule, $attribute)
  13. {
  14. if ($rule instanceof Rangable_Rule)
  15. {
  16. $message = static::get_rangable_message($rule);
  17. }
  18. elseif ($rule instanceof Rules\Upload_of)
  19. {
  20. $message = static::get_upload_of_message($rule);
  21. }
  22. else
  23. {
  24. $message = static::get_message($rule);
  25. }
  26. return static::prepare($rule, $attribute, $message);
  27. }
  28. /**
  29. * Get the error message for a typical validation rule.
  30. *
  31. * @param Rule $rule
  32. * @return string
  33. */
  34. private static function get_message($rule)
  35. {
  36. // ---------------------------------------------------------
  37. // The built-in error messages are stored in the language
  38. // directory and are keyed by the class name of the rule
  39. // they are associated with.
  40. // ---------------------------------------------------------
  41. if (is_null($rule->error))
  42. {
  43. $class = explode('\\', get_class($rule));
  44. $rule->error = strtolower(end($class));
  45. }
  46. return (is_null($rule->message)) ? Lang::line('validation.'.$rule->error)->get() : $rule->message;
  47. }
  48. /**
  49. * Get the error message for a Rangable rule.
  50. *
  51. * @param Rule $rule
  52. * @return string
  53. */
  54. private static function get_rangable_message($rule)
  55. {
  56. // ---------------------------------------------------------
  57. // Rangable rules sometimes set a "presence_of" error.
  58. //
  59. // This occurs when an attribute is null and the option to
  60. // allow null values has not been set.
  61. // ---------------------------------------------------------
  62. if ($rule->error == 'presence_of')
  63. {
  64. return static::get_message($rule);
  65. }
  66. // ---------------------------------------------------------
  67. // Slice "number_" or "string_" off of the error type.
  68. // ---------------------------------------------------------
  69. $error_type = substr($rule->error, 7);
  70. return (is_null($rule->$error_type)) ? Lang::line('validation.'.$rule->error)->get() : $rule->$error_type;
  71. }
  72. /**
  73. * Get the error message for an Upload_Of rule.
  74. *
  75. * @param Rule $rule
  76. * @return string
  77. */
  78. private static function get_upload_of_message($rule)
  79. {
  80. // ---------------------------------------------------------
  81. // Upload_Of rules sometimes set a "presence_of" error.
  82. //
  83. // This occurs when the uploaded file didn't exist and the
  84. // "not_required" method was not called.
  85. // ---------------------------------------------------------
  86. if ($rule->error == 'presence_of')
  87. {
  88. return static::get_message($rule);
  89. }
  90. // ---------------------------------------------------------
  91. // Slice "file_" off of the error type.
  92. // ---------------------------------------------------------
  93. $error_type = substr($rule->error, 5);
  94. return (is_null($rule->$error_type)) ? Lang::line('validation.'.$rule->error)->get() : $rule->$error_type;
  95. }
  96. /**
  97. * Prepare an error message for display. All place-holders will be replaced
  98. * with their actual values.
  99. *
  100. * @param Rule $rule
  101. * @param string $attribute
  102. * @param string $message
  103. * @return string
  104. */
  105. private static function prepare($rule, $attribute, $message)
  106. {
  107. // ---------------------------------------------------------
  108. // The rangable rule messages have three place-holders that
  109. // must be replaced.
  110. //
  111. // :max = The maximum size of the attribute.
  112. // :min = The minimum size of the attribute.
  113. // :size = The exact size the attribute must be.
  114. // ---------------------------------------------------------
  115. if ($rule instanceof Rangable_Rule)
  116. {
  117. $message = str_replace(':max', $rule->maximum, $message);
  118. $message = str_replace(':min', $rule->minimum, $message);
  119. $message = str_replace(':size', $rule->size, $message);
  120. }
  121. // ---------------------------------------------------------
  122. // The Upload_Of rule message have two place-holders taht
  123. // must be replaced.
  124. //
  125. // :max = The maximum file size of the upload (kilobytes).
  126. // :types = The allowed file types for the upload.
  127. // ---------------------------------------------------------
  128. elseif ($rule instanceof Rules\Upload_Of)
  129. {
  130. $message = str_replace(':max', $rule->maximum, $message);
  131. if (is_array($rule->types))
  132. {
  133. $message = str_replace(':types', implode(', ', $rule->types), $message);
  134. }
  135. }
  136. return str_replace(':attribute', Lang::line('attributes.'.$attribute)->get(str_replace('_', ' ', $attribute)), $message);
  137. }
  138. }