message.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // Slice "file_" off of the error type.
  82. // ---------------------------------------------------------
  83. $error_type = substr($rule->error, 5);
  84. return (is_null($rule->$error_type)) ? Lang::line('validation.'.$rule->error)->get() : $rule->$error_type;
  85. }
  86. /**
  87. * Prepare an error message for display. All place-holders will be replaced
  88. * with their actual values.
  89. *
  90. * @param Rule $rule
  91. * @param string $attribute
  92. * @param string $message
  93. * @return string
  94. */
  95. private static function prepare($rule, $attribute, $message)
  96. {
  97. // ---------------------------------------------------------
  98. // The rangable rule messages have three place-holders that
  99. // must be replaced.
  100. //
  101. // :max = The maximum size of the attribute.
  102. // :min = The minimum size of the attribute.
  103. // :size = The exact size the attribute must be.
  104. // ---------------------------------------------------------
  105. if ($rule instanceof Rangable_Rule)
  106. {
  107. $message = str_replace(':max', $rule->maximum, $message);
  108. $message = str_replace(':min', $rule->minimum, $message);
  109. $message = str_replace(':size', $rule->size, $message);
  110. }
  111. // ---------------------------------------------------------
  112. // The Upload_Of rule message have two place-holders taht
  113. // must be replaced.
  114. //
  115. // :max = The maximum file size of the upload (kilobytes).
  116. // :types = The allowed file types for the upload.
  117. // ---------------------------------------------------------
  118. elseif ($rule instanceof Rules\Upload_Of)
  119. {
  120. $message = str_replace(':max', $rule->maximum, $message);
  121. if (is_array($rule->types))
  122. {
  123. $message = str_replace(':types', implode(', ', $rule->types), $message);
  124. }
  125. }
  126. return str_replace(':attribute', Lang::line('attributes.'.$attribute)->get(str_replace('_', ' ', $attribute)), $message);
  127. }
  128. }