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

/vendor/laravel/framework/src/Illuminate/Validation/Concerns/ReplacesAttributes.php

https://gitlab.com/madwanz64/laravel
PHP | 508 lines | 171 code | 49 blank | 288 comment | 6 complexity | 25c0956edd2bceb121aa1982490d86d4 MD5 | raw file
  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. if (is_null($value = $this->getValue($parameters[0]))) {
  242. return str_replace(':value', $this->getDisplayableAttribute($parameters[0]), $message);
  243. }
  244. return str_replace(':value', $this->getSize($attribute, $value), $message);
  245. }
  246. /**
  247. * Replace all place-holders for the lt rule.
  248. *
  249. * @param string $message
  250. * @param string $attribute
  251. * @param string $rule
  252. * @param array $parameters
  253. * @return string
  254. */
  255. protected function replaceLt($message, $attribute, $rule, $parameters)
  256. {
  257. if (is_null($value = $this->getValue($parameters[0]))) {
  258. return str_replace(':value', $this->getDisplayableAttribute($parameters[0]), $message);
  259. }
  260. return str_replace(':value', $this->getSize($attribute, $value), $message);
  261. }
  262. /**
  263. * Replace all place-holders for the gte rule.
  264. *
  265. * @param string $message
  266. * @param string $attribute
  267. * @param string $rule
  268. * @param array $parameters
  269. * @return string
  270. */
  271. protected function replaceGte($message, $attribute, $rule, $parameters)
  272. {
  273. if (is_null($value = $this->getValue($parameters[0]))) {
  274. return str_replace(':value', $this->getDisplayableAttribute($parameters[0]), $message);
  275. }
  276. return str_replace(':value', $this->getSize($attribute, $value), $message);
  277. }
  278. /**
  279. * Replace all place-holders for the lte rule.
  280. *
  281. * @param string $message
  282. * @param string $attribute
  283. * @param string $rule
  284. * @param array $parameters
  285. * @return string
  286. */
  287. protected function replaceLte($message, $attribute, $rule, $parameters)
  288. {
  289. if (is_null($value = $this->getValue($parameters[0]))) {
  290. return str_replace(':value', $this->getDisplayableAttribute($parameters[0]), $message);
  291. }
  292. return str_replace(':value', $this->getSize($attribute, $value), $message);
  293. }
  294. /**
  295. * Replace all place-holders for the required_if rule.
  296. *
  297. * @param string $message
  298. * @param string $attribute
  299. * @param string $rule
  300. * @param array $parameters
  301. * @return string
  302. */
  303. protected function replaceRequiredIf($message, $attribute, $rule, $parameters)
  304. {
  305. $parameters[1] = $this->getDisplayableValue($parameters[0], Arr::get($this->data, $parameters[0]));
  306. $parameters[0] = $this->getDisplayableAttribute($parameters[0]);
  307. return str_replace([':other', ':value'], $parameters, $message);
  308. }
  309. /**
  310. * Replace all place-holders for the required_unless rule.
  311. *
  312. * @param string $message
  313. * @param string $attribute
  314. * @param string $rule
  315. * @param array $parameters
  316. * @return string
  317. */
  318. protected function replaceRequiredUnless($message, $attribute, $rule, $parameters)
  319. {
  320. $other = $this->getDisplayableAttribute($parameters[0]);
  321. $values = [];
  322. foreach (array_slice($parameters, 1) as $value) {
  323. $values[] = $this->getDisplayableValue($parameters[0], $value);
  324. }
  325. return str_replace([':other', ':values'], [$other, implode(', ', $values)], $message);
  326. }
  327. /**
  328. * Replace all place-holders for the same rule.
  329. *
  330. * @param string $message
  331. * @param string $attribute
  332. * @param string $rule
  333. * @param array $parameters
  334. * @return string
  335. */
  336. protected function replaceSame($message, $attribute, $rule, $parameters)
  337. {
  338. return str_replace(':other', $this->getDisplayableAttribute($parameters[0]), $message);
  339. }
  340. /**
  341. * Replace all place-holders for the before rule.
  342. *
  343. * @param string $message
  344. * @param string $attribute
  345. * @param string $rule
  346. * @param array $parameters
  347. * @return string
  348. */
  349. protected function replaceBefore($message, $attribute, $rule, $parameters)
  350. {
  351. if (! strtotime($parameters[0])) {
  352. return str_replace(':date', $this->getDisplayableAttribute($parameters[0]), $message);
  353. }
  354. return str_replace(':date', $this->getDisplayableValue($attribute, $parameters[0]), $message);
  355. }
  356. /**
  357. * Replace all place-holders for the before_or_equal rule.
  358. *
  359. * @param string $message
  360. * @param string $attribute
  361. * @param string $rule
  362. * @param array $parameters
  363. * @return string
  364. */
  365. protected function replaceBeforeOrEqual($message, $attribute, $rule, $parameters)
  366. {
  367. return $this->replaceBefore($message, $attribute, $rule, $parameters);
  368. }
  369. /**
  370. * Replace all place-holders for the after rule.
  371. *
  372. * @param string $message
  373. * @param string $attribute
  374. * @param string $rule
  375. * @param array $parameters
  376. * @return string
  377. */
  378. protected function replaceAfter($message, $attribute, $rule, $parameters)
  379. {
  380. return $this->replaceBefore($message, $attribute, $rule, $parameters);
  381. }
  382. /**
  383. * Replace all place-holders for the after_or_equal rule.
  384. *
  385. * @param string $message
  386. * @param string $attribute
  387. * @param string $rule
  388. * @param array $parameters
  389. * @return string
  390. */
  391. protected function replaceAfterOrEqual($message, $attribute, $rule, $parameters)
  392. {
  393. return $this->replaceBefore($message, $attribute, $rule, $parameters);
  394. }
  395. /**
  396. * Replace all place-holders for the date_equals rule.
  397. *
  398. * @param string $message
  399. * @param string $attribute
  400. * @param string $rule
  401. * @param array $parameters
  402. * @return string
  403. */
  404. protected function replaceDateEquals($message, $attribute, $rule, $parameters)
  405. {
  406. return $this->replaceBefore($message, $attribute, $rule, $parameters);
  407. }
  408. /**
  409. * Replace all place-holders for the dimensions rule.
  410. *
  411. * @param string $message
  412. * @param string $attribute
  413. * @param string $rule
  414. * @param array $parameters
  415. * @return string
  416. */
  417. protected function replaceDimensions($message, $attribute, $rule, $parameters)
  418. {
  419. $parameters = $this->parseNamedParameters($parameters);
  420. if (is_array($parameters)) {
  421. foreach ($parameters as $key => $value) {
  422. $message = str_replace(':'.$key, $value, $message);
  423. }
  424. }
  425. return $message;
  426. }
  427. /**
  428. * Replace all place-holders for the ends_with rule.
  429. *
  430. * @param string $message
  431. * @param string $attribute
  432. * @param string $rule
  433. * @param array $parameters
  434. * @return string
  435. */
  436. protected function replaceEndsWith($message, $attribute, $rule, $parameters)
  437. {
  438. foreach ($parameters as &$parameter) {
  439. $parameter = $this->getDisplayableValue($attribute, $parameter);
  440. }
  441. return str_replace(':values', implode(', ', $parameters), $message);
  442. }
  443. /**
  444. * Replace all place-holders for the starts_with rule.
  445. *
  446. * @param string $message
  447. * @param string $attribute
  448. * @param string $rule
  449. * @param array $parameters
  450. * @return string
  451. */
  452. protected function replaceStartsWith($message, $attribute, $rule, $parameters)
  453. {
  454. foreach ($parameters as &$parameter) {
  455. $parameter = $this->getDisplayableValue($attribute, $parameter);
  456. }
  457. return str_replace(':values', implode(', ', $parameters), $message);
  458. }
  459. }