ReplacesAttributes.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <?php
  2. namespace Illuminate\Validation\Concerns;
  3. use Illuminate\Support\Arr;
  4. trait ReplacesAttributes
  5. {
  6. /**
  7. * Replace all place-holders for the between rule.
  8. *
  9. * @param string $message
  10. * @param string $attribute
  11. * @param string $rule
  12. * @param array $parameters
  13. * @return string
  14. */
  15. protected function replaceBetween($message, $attribute, $rule, $parameters)
  16. {
  17. return str_replace([':min', ':max'], $parameters, $message);
  18. }
  19. /**
  20. * Replace all place-holders for the date_format rule.
  21. *
  22. * @param string $message
  23. * @param string $attribute
  24. * @param string $rule
  25. * @param array $parameters
  26. * @return string
  27. */
  28. protected function replaceDateFormat($message, $attribute, $rule, $parameters)
  29. {
  30. return str_replace(':format', $parameters[0], $message);
  31. }
  32. /**
  33. * Replace all place-holders for the different rule.
  34. *
  35. * @param string $message
  36. * @param string $attribute
  37. * @param string $rule
  38. * @param array $parameters
  39. * @return string
  40. */
  41. protected function replaceDifferent($message, $attribute, $rule, $parameters)
  42. {
  43. return $this->replaceSame($message, $attribute, $rule, $parameters);
  44. }
  45. /**
  46. * Replace all place-holders for the digits rule.
  47. *
  48. * @param string $message
  49. * @param string $attribute
  50. * @param string $rule
  51. * @param array $parameters
  52. * @return string
  53. */
  54. protected function replaceDigits($message, $attribute, $rule, $parameters)
  55. {
  56. return str_replace(':digits', $parameters[0], $message);
  57. }
  58. /**
  59. * Replace all place-holders for the digits (between) rule.
  60. *
  61. * @param string $message
  62. * @param string $attribute
  63. * @param string $rule
  64. * @param array $parameters
  65. * @return string
  66. */
  67. protected function replaceDigitsBetween($message, $attribute, $rule, $parameters)
  68. {
  69. return $this->replaceBetween($message, $attribute, $rule, $parameters);
  70. }
  71. /**
  72. * Replace all place-holders for the min rule.
  73. *
  74. * @param string $message
  75. * @param string $attribute
  76. * @param string $rule
  77. * @param array $parameters
  78. * @return string
  79. */
  80. protected function replaceMin($message, $attribute, $rule, $parameters)
  81. {
  82. return str_replace(':min', $parameters[0], $message);
  83. }
  84. /**
  85. * Replace all place-holders for the max rule.
  86. *
  87. * @param string $message
  88. * @param string $attribute
  89. * @param string $rule
  90. * @param array $parameters
  91. * @return string
  92. */
  93. protected function replaceMax($message, $attribute, $rule, $parameters)
  94. {
  95. return str_replace(':max', $parameters[0], $message);
  96. }
  97. /**
  98. * Replace all place-holders for the in rule.
  99. *
  100. * @param string $message
  101. * @param string $attribute
  102. * @param string $rule
  103. * @param array $parameters
  104. * @return string
  105. */
  106. protected function replaceIn($message, $attribute, $rule, $parameters)
  107. {
  108. foreach ($parameters as &$parameter) {
  109. $parameter = $this->getDisplayableValue($attribute, $parameter);
  110. }
  111. return str_replace(':values', implode(', ', $parameters), $message);
  112. }
  113. /**
  114. * Replace all place-holders for the not_in rule.
  115. *
  116. * @param string $message
  117. * @param string $attribute
  118. * @param string $rule
  119. * @param array $parameters
  120. * @return string
  121. */
  122. protected function replaceNotIn($message, $attribute, $rule, $parameters)
  123. {
  124. return $this->replaceIn($message, $attribute, $rule, $parameters);
  125. }
  126. /**
  127. * Replace all place-holders for the in_array rule.
  128. *
  129. * @param string $message
  130. * @param string $attribute
  131. * @param string $rule
  132. * @param array $parameters
  133. * @return string
  134. */
  135. protected function replaceInArray($message, $attribute, $rule, $parameters)
  136. {
  137. return str_replace(':other', $this->getDisplayableAttribute($parameters[0]), $message);
  138. }
  139. /**
  140. * Replace all place-holders for the mimetypes rule.
  141. *
  142. * @param string $message
  143. * @param string $attribute
  144. * @param string $rule
  145. * @param array $parameters
  146. * @return string
  147. */
  148. protected function replaceMimetypes($message, $attribute, $rule, $parameters)
  149. {
  150. return str_replace(':values', implode(', ', $parameters), $message);
  151. }
  152. /**
  153. * Replace all place-holders for the mimes rule.
  154. *
  155. * @param string $message
  156. * @param string $attribute
  157. * @param string $rule
  158. * @param array $parameters
  159. * @return string
  160. */
  161. protected function replaceMimes($message, $attribute, $rule, $parameters)
  162. {
  163. return str_replace(':values', implode(', ', $parameters), $message);
  164. }
  165. /**
  166. * Replace all place-holders for the required_with rule.
  167. *
  168. * @param string $message
  169. * @param string $attribute
  170. * @param string $rule
  171. * @param array $parameters
  172. * @return string
  173. */
  174. protected function replaceRequiredWith($message, $attribute, $rule, $parameters)
  175. {
  176. return str_replace(':values', implode(' / ', $this->getAttributeList($parameters)), $message);
  177. }
  178. /**
  179. * Replace all place-holders for the required_with_all rule.
  180. *
  181. * @param string $message
  182. * @param string $attribute
  183. * @param string $rule
  184. * @param array $parameters
  185. * @return string
  186. */
  187. protected function replaceRequiredWithAll($message, $attribute, $rule, $parameters)
  188. {
  189. return $this->replaceRequiredWith($message, $attribute, $rule, $parameters);
  190. }
  191. /**
  192. * Replace all place-holders for the required_without rule.
  193. *
  194. * @param string $message
  195. * @param string $attribute
  196. * @param string $rule
  197. * @param array $parameters
  198. * @return string
  199. */
  200. protected function replaceRequiredWithout($message, $attribute, $rule, $parameters)
  201. {
  202. return $this->replaceRequiredWith($message, $attribute, $rule, $parameters);
  203. }
  204. /**
  205. * Replace all place-holders for the required_without_all rule.
  206. *
  207. * @param string $message
  208. * @param string $attribute
  209. * @param string $rule
  210. * @param array $parameters
  211. * @return string
  212. */
  213. protected function replaceRequiredWithoutAll($message, $attribute, $rule, $parameters)
  214. {
  215. return $this->replaceRequiredWith($message, $attribute, $rule, $parameters);
  216. }
  217. /**
  218. * Replace all place-holders for the size rule.
  219. *
  220. * @param string $message
  221. * @param string $attribute
  222. * @param string $rule
  223. * @param array $parameters
  224. * @return string
  225. */
  226. protected function replaceSize($message, $attribute, $rule, $parameters)
  227. {
  228. return str_replace(':size', $parameters[0], $message);
  229. }
  230. /**
  231. * Replace all place-holders for the gt rule.
  232. *
  233. * @param string $message
  234. * @param string $attribute
  235. * @param string $rule
  236. * @param array $parameters
  237. * @return string
  238. */
  239. protected function replaceGt($message, $attribute, $rule, $parameters)
  240. {
  241. return str_replace(':value', $this->getSize($parameters[0], $this->getValue($parameters[0])), $message);
  242. }
  243. /**
  244. * Replace all place-holders for the lt rule.
  245. *
  246. * @param string $message
  247. * @param string $attribute
  248. * @param string $rule
  249. * @param array $parameters
  250. * @return string
  251. */
  252. protected function replaceLt($message, $attribute, $rule, $parameters)
  253. {
  254. return str_replace(':value', $this->getSize($parameters[0], $this->getValue($parameters[0])), $message);
  255. }
  256. /**
  257. * Replace all place-holders for the gte rule.
  258. *
  259. * @param string $message
  260. * @param string $attribute
  261. * @param string $rule
  262. * @param array $parameters
  263. * @return string
  264. */
  265. protected function replaceGte($message, $attribute, $rule, $parameters)
  266. {
  267. return str_replace(':value', $this->getSize($parameters[0], $this->getValue($parameters[0])), $message);
  268. }
  269. /**
  270. * Replace all place-holders for the lte rule.
  271. *
  272. * @param string $message
  273. * @param string $attribute
  274. * @param string $rule
  275. * @param array $parameters
  276. * @return string
  277. */
  278. protected function replaceLte($message, $attribute, $rule, $parameters)
  279. {
  280. return str_replace(':value', $this->getSize($parameters[0], $this->getValue($parameters[0])), $message);
  281. }
  282. /**
  283. * Replace all place-holders for the required_if rule.
  284. *
  285. * @param string $message
  286. * @param string $attribute
  287. * @param string $rule
  288. * @param array $parameters
  289. * @return string
  290. */
  291. protected function replaceRequiredIf($message, $attribute, $rule, $parameters)
  292. {
  293. $parameters[1] = $this->getDisplayableValue($parameters[0], Arr::get($this->data, $parameters[0]));
  294. $parameters[0] = $this->getDisplayableAttribute($parameters[0]);
  295. return str_replace([':other', ':value'], $parameters, $message);
  296. }
  297. /**
  298. * Replace all place-holders for the required_unless rule.
  299. *
  300. * @param string $message
  301. * @param string $attribute
  302. * @param string $rule
  303. * @param array $parameters
  304. * @return string
  305. */
  306. protected function replaceRequiredUnless($message, $attribute, $rule, $parameters)
  307. {
  308. $other = $this->getDisplayableAttribute($parameters[0]);
  309. $values = [];
  310. foreach (array_slice($parameters, 1) as $value) {
  311. $values[] = $this->getDisplayableValue($parameters[0], $value);
  312. }
  313. return str_replace([':other', ':values'], [$other, implode(', ', $values)], $message);
  314. }
  315. /**
  316. * Replace all place-holders for the same rule.
  317. *
  318. * @param string $message
  319. * @param string $attribute
  320. * @param string $rule
  321. * @param array $parameters
  322. * @return string
  323. */
  324. protected function replaceSame($message, $attribute, $rule, $parameters)
  325. {
  326. return str_replace(':other', $this->getDisplayableAttribute($parameters[0]), $message);
  327. }
  328. /**
  329. * Replace all place-holders for the before rule.
  330. *
  331. * @param string $message
  332. * @param string $attribute
  333. * @param string $rule
  334. * @param array $parameters
  335. * @return string
  336. */
  337. protected function replaceBefore($message, $attribute, $rule, $parameters)
  338. {
  339. if (! (strtotime($parameters[0]))) {
  340. return str_replace(':date', $this->getDisplayableAttribute($parameters[0]), $message);
  341. }
  342. return str_replace(':date', $parameters[0], $message);
  343. }
  344. /**
  345. * Replace all place-holders for the before_or_equal rule.
  346. *
  347. * @param string $message
  348. * @param string $attribute
  349. * @param string $rule
  350. * @param array $parameters
  351. * @return string
  352. */
  353. protected function replaceBeforeOrEqual($message, $attribute, $rule, $parameters)
  354. {
  355. return $this->replaceBefore($message, $attribute, $rule, $parameters);
  356. }
  357. /**
  358. * Replace all place-holders for the after rule.
  359. *
  360. * @param string $message
  361. * @param string $attribute
  362. * @param string $rule
  363. * @param array $parameters
  364. * @return string
  365. */
  366. protected function replaceAfter($message, $attribute, $rule, $parameters)
  367. {
  368. return $this->replaceBefore($message, $attribute, $rule, $parameters);
  369. }
  370. /**
  371. * Replace all place-holders for the after_or_equal rule.
  372. *
  373. * @param string $message
  374. * @param string $attribute
  375. * @param string $rule
  376. * @param array $parameters
  377. * @return string
  378. */
  379. protected function replaceAfterOrEqual($message, $attribute, $rule, $parameters)
  380. {
  381. return $this->replaceBefore($message, $attribute, $rule, $parameters);
  382. }
  383. /**
  384. * Replace all place-holders for the dimensions rule.
  385. *
  386. * @param string $message
  387. * @param string $attribute
  388. * @param string $rule
  389. * @param array $parameters
  390. * @return string
  391. */
  392. protected function replaceDimensions($message, $attribute, $rule, $parameters)
  393. {
  394. $parameters = $this->parseNamedParameters($parameters);
  395. if (is_array($parameters)) {
  396. foreach ($parameters as $key => $value) {
  397. $message = str_replace(':'.$key, $value, $message);
  398. }
  399. }
  400. return $message;
  401. }
  402. }