ReplacesAttributes.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. <?php
  2. namespace Illuminate\Validation\Concerns;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Support\Str;
  5. trait ReplacesAttributes
  6. {
  7. /**
  8. * Replace all place-holders for the accepted_if rule.
  9. *
  10. * @param string $message
  11. * @param string $attribute
  12. * @param string $rule
  13. * @param array<int,string> $parameters
  14. * @return string
  15. */
  16. protected function replaceAcceptedIf($message, $attribute, $rule, $parameters)
  17. {
  18. $parameters[1] = $this->getDisplayableValue($parameters[0], Arr::get($this->data, $parameters[0]));
  19. $parameters[0] = $this->getDisplayableAttribute($parameters[0]);
  20. return $this->replaceWhileKeepingCase($message, ['other' => $parameters[0], 'value' => $parameters[1]]);
  21. }
  22. /**
  23. * Replace all place-holders for the declined_if rule.
  24. *
  25. * @param string $message
  26. * @param string $attribute
  27. * @param string $rule
  28. * @param array<int,string> $parameters
  29. * @return string
  30. */
  31. protected function replaceDeclinedIf($message, $attribute, $rule, $parameters)
  32. {
  33. return $this->replaceAcceptedIf($message, $attribute, $rule, $parameters);
  34. }
  35. /**
  36. * Replace all place-holders for the between rule.
  37. *
  38. * @param string $message
  39. * @param string $attribute
  40. * @param string $rule
  41. * @param array<int,string> $parameters
  42. * @return string
  43. */
  44. protected function replaceBetween($message, $attribute, $rule, $parameters)
  45. {
  46. return str_replace([':min', ':max'], $parameters, $message);
  47. }
  48. /**
  49. * Replace all place-holders for the date_format rule.
  50. *
  51. * @param string $message
  52. * @param string $attribute
  53. * @param string $rule
  54. * @param array<int,string> $parameters
  55. * @return string
  56. */
  57. protected function replaceDateFormat($message, $attribute, $rule, $parameters)
  58. {
  59. return str_replace(':format', $parameters[0], $message);
  60. }
  61. /**
  62. * Replace all place-holders for the decimal rule.
  63. *
  64. * @param string $message
  65. * @param string $attribute
  66. * @param string $rule
  67. * @param array<int,int> $parameters
  68. * @return string
  69. */
  70. protected function replaceDecimal($message, $attribute, $rule, $parameters)
  71. {
  72. return str_replace(
  73. ':decimal',
  74. isset($parameters[1])
  75. ? $parameters[0].'-'.$parameters[1]
  76. : $parameters[0],
  77. $message
  78. );
  79. }
  80. /**
  81. * Replace all place-holders for the different rule.
  82. *
  83. * @param string $message
  84. * @param string $attribute
  85. * @param string $rule
  86. * @param array<int,string> $parameters
  87. * @return string
  88. */
  89. protected function replaceDifferent($message, $attribute, $rule, $parameters)
  90. {
  91. return $this->replaceSame($message, $attribute, $rule, $parameters);
  92. }
  93. /**
  94. * Replace all place-holders for the digits rule.
  95. *
  96. * @param string $message
  97. * @param string $attribute
  98. * @param string $rule
  99. * @param array<int,string> $parameters
  100. * @return string
  101. */
  102. protected function replaceDigits($message, $attribute, $rule, $parameters)
  103. {
  104. return str_replace(':digits', $parameters[0], $message);
  105. }
  106. /**
  107. * Replace all place-holders for the digits (between) rule.
  108. *
  109. * @param string $message
  110. * @param string $attribute
  111. * @param string $rule
  112. * @param array<int,string> $parameters
  113. * @return string
  114. */
  115. protected function replaceDigitsBetween($message, $attribute, $rule, $parameters)
  116. {
  117. return $this->replaceBetween($message, $attribute, $rule, $parameters);
  118. }
  119. /**
  120. * Replace all place-holders for the encoding rule.
  121. *
  122. * @param string $message
  123. * @param string $attribute
  124. * @param string $rule
  125. * @param array<int,string> $parameters
  126. * @return string
  127. */
  128. protected function replaceEncoding($message, $attribute, $rule, $parameters)
  129. {
  130. return str_replace(':encoding', $parameters[0], $message);
  131. }
  132. /**
  133. * Replace all place-holders for the extensions rule.
  134. *
  135. * @param string $message
  136. * @param string $attribute
  137. * @param string $rule
  138. * @param array<int,string> $parameters
  139. * @return string
  140. */
  141. protected function replaceExtensions($message, $attribute, $rule, $parameters)
  142. {
  143. return str_replace(':values', implode(', ', $parameters), $message);
  144. }
  145. /**
  146. * Replace all place-holders for the min rule.
  147. *
  148. * @param string $message
  149. * @param string $attribute
  150. * @param string $rule
  151. * @param array<int,string> $parameters
  152. * @return string
  153. */
  154. protected function replaceMin($message, $attribute, $rule, $parameters)
  155. {
  156. return str_replace(':min', $parameters[0], $message);
  157. }
  158. /**
  159. * Replace all place-holders for the min digits rule.
  160. *
  161. * @param string $message
  162. * @param string $attribute
  163. * @param string $rule
  164. * @param array<int,string> $parameters
  165. * @return string
  166. */
  167. protected function replaceMinDigits($message, $attribute, $rule, $parameters)
  168. {
  169. return str_replace(':min', $parameters[0], $message);
  170. }
  171. /**
  172. * Replace all place-holders for the max rule.
  173. *
  174. * @param string $message
  175. * @param string $attribute
  176. * @param string $rule
  177. * @param array<int,string> $parameters
  178. * @return string
  179. */
  180. protected function replaceMax($message, $attribute, $rule, $parameters)
  181. {
  182. return str_replace(':max', $parameters[0], $message);
  183. }
  184. /**
  185. * Replace all place-holders for the max digits rule.
  186. *
  187. * @param string $message
  188. * @param string $attribute
  189. * @param string $rule
  190. * @param array<int,string> $parameters
  191. * @return string
  192. */
  193. protected function replaceMaxDigits($message, $attribute, $rule, $parameters)
  194. {
  195. return str_replace(':max', $parameters[0], $message);
  196. }
  197. /**
  198. * Replace all place-holders for the missing_if rule.
  199. *
  200. * @param string $message
  201. * @param string $attribute
  202. * @param string $rule
  203. * @param array<int,string> $parameters
  204. * @return string
  205. */
  206. protected function replaceMissingIf($message, $attribute, $rule, $parameters)
  207. {
  208. return $this->replaceAcceptedIf($message, $attribute, $rule, $parameters);
  209. }
  210. /**
  211. * Replace all place-holders for the missing_unless rule.
  212. *
  213. * @param string $message
  214. * @param string $attribute
  215. * @param string $rule
  216. * @param array<int,string> $parameters
  217. * @return string
  218. */
  219. protected function replaceMissingUnless($message, $attribute, $rule, $parameters)
  220. {
  221. return str_replace([':other', ':value'], [
  222. $this->getDisplayableAttribute($parameters[0]),
  223. $this->getDisplayableValue($parameters[0], $parameters[1]),
  224. ], $message);
  225. }
  226. /**
  227. * Replace all place-holders for the missing_with rule.
  228. *
  229. * @param string $message
  230. * @param string $attribute
  231. * @param string $rule
  232. * @param array<int,string> $parameters
  233. * @return string
  234. */
  235. protected function replaceMissingWith($message, $attribute, $rule, $parameters)
  236. {
  237. return str_replace(
  238. [':values', ':VALUES', ':Values'],
  239. [
  240. implode(' / ', $this->getAttributeList($parameters)),
  241. Str::upper(implode(' / ', $this->getAttributeList($parameters))),
  242. implode(' / ', array_map(Str::ucfirst(...), $this->getAttributeList($parameters))),
  243. ],
  244. $message
  245. );
  246. }
  247. /**
  248. * Replace all place-holders for the missing_with_all rule.
  249. *
  250. * @param string $message
  251. * @param string $attribute
  252. * @param string $rule
  253. * @param array<int,string> $parameters
  254. * @return string
  255. */
  256. protected function replaceMissingWithAll($message, $attribute, $rule, $parameters)
  257. {
  258. return $this->replaceMissingWith($message, $attribute, $rule, $parameters);
  259. }
  260. /**
  261. * Replace all place-holders for the multiple_of rule.
  262. *
  263. * @param string $message
  264. * @param string $attribute
  265. * @param string $rule
  266. * @param array<int,string> $parameters
  267. * @return string
  268. */
  269. protected function replaceMultipleOf($message, $attribute, $rule, $parameters)
  270. {
  271. return str_replace(':value', $parameters[0] ?? '', $message);
  272. }
  273. /**
  274. * Replace all place-holders for the in rule.
  275. *
  276. * @param string $message
  277. * @param string $attribute
  278. * @param string $rule
  279. * @param array<int,string> $parameters
  280. * @return string
  281. */
  282. protected function replaceIn($message, $attribute, $rule, $parameters)
  283. {
  284. foreach ($parameters as &$parameter) {
  285. $parameter = $this->getDisplayableValue($attribute, $parameter);
  286. }
  287. return str_replace(
  288. [':values', ':VALUES', ':Values'],
  289. [
  290. implode(', ', $parameters),
  291. Str::upper(implode(', ', $parameters)),
  292. implode(', ', array_map(Str::ucfirst(...), $parameters)),
  293. ],
  294. $message,
  295. );
  296. }
  297. /**
  298. * Replace all place-holders for the not_in rule.
  299. *
  300. * @param string $message
  301. * @param string $attribute
  302. * @param string $rule
  303. * @param array<int,string> $parameters
  304. * @return string
  305. */
  306. protected function replaceNotIn($message, $attribute, $rule, $parameters)
  307. {
  308. return $this->replaceIn($message, $attribute, $rule, $parameters);
  309. }
  310. /**
  311. * Replace all place-holders for the in_array rule.
  312. *
  313. * @param string $message
  314. * @param string $attribute
  315. * @param string $rule
  316. * @param array<int,string> $parameters
  317. * @return string
  318. */
  319. protected function replaceInArray($message, $attribute, $rule, $parameters)
  320. {
  321. $value = $this->getDisplayableAttribute($parameters[0]);
  322. return $this->replaceWhileKeepingCase($message, ['other' => $value]);
  323. }
  324. /**
  325. * Replace all place-holders for the in_array_keys rule.
  326. *
  327. * @param string $message
  328. * @param string $attribute
  329. * @param string $rule
  330. * @param array<int,string> $parameters
  331. * @return string
  332. */
  333. protected function replaceInArrayKeys($message, $attribute, $rule, $parameters)
  334. {
  335. return $this->replaceIn($message, $attribute, $rule, $parameters);
  336. }
  337. /**
  338. * Replace all place-holders for the required_array_keys rule.
  339. *
  340. * @param string $message
  341. * @param string $attribute
  342. * @param string $rule
  343. * @param array<int,string> $parameters
  344. * @return string
  345. */
  346. protected function replaceRequiredArrayKeys($message, $attribute, $rule, $parameters)
  347. {
  348. return $this->replaceIn($message, $attribute, $rule, $parameters);
  349. }
  350. /**
  351. * Replace all place-holders for the mimetypes rule.
  352. *
  353. * @param string $message
  354. * @param string $attribute
  355. * @param string $rule
  356. * @param array<int,string> $parameters
  357. * @return string
  358. */
  359. protected function replaceMimetypes($message, $attribute, $rule, $parameters)
  360. {
  361. return str_replace(':values', implode(', ', $parameters), $message);
  362. }
  363. /**
  364. * Replace all place-holders for the mimes rule.
  365. *
  366. * @param string $message
  367. * @param string $attribute
  368. * @param string $rule
  369. * @param array<int,string> $parameters
  370. * @return string
  371. */
  372. protected function replaceMimes($message, $attribute, $rule, $parameters)
  373. {
  374. return str_replace(':values', implode(', ', $parameters), $message);
  375. }
  376. /**
  377. * Replace all place-holders for the present_if rule.
  378. *
  379. * @param string $message
  380. * @param string $attribute
  381. * @param string $rule
  382. * @param array<int,string> $parameters
  383. * @return string
  384. */
  385. protected function replacePresentIf($message, $attribute, $rule, $parameters)
  386. {
  387. return $this->replaceAcceptedIf($message, $attribute, $rule, $parameters);
  388. }
  389. /**
  390. * Replace all place-holders for the present_unless rule.
  391. *
  392. * @param string $message
  393. * @param string $attribute
  394. * @param string $rule
  395. * @param array<int,string> $parameters
  396. * @return string
  397. */
  398. protected function replacePresentUnless($message, $attribute, $rule, $parameters)
  399. {
  400. return $this->replaceMissingUnless($message, $attribute, $rule, $parameters);
  401. }
  402. /**
  403. * Replace all place-holders for the present_with rule.
  404. *
  405. * @param string $message
  406. * @param string $attribute
  407. * @param string $rule
  408. * @param array<int,string> $parameters
  409. * @return string
  410. */
  411. protected function replacePresentWith($message, $attribute, $rule, $parameters)
  412. {
  413. return str_replace(
  414. [':values', ':VALUES', ':Values'],
  415. [
  416. implode(' / ', $this->getAttributeList($parameters)),
  417. Str::upper(implode(' / ', $this->getAttributeList($parameters))),
  418. implode(' / ', array_map(Str::ucfirst(...), $this->getAttributeList($parameters))),
  419. ],
  420. $message,
  421. );
  422. }
  423. /**
  424. * Replace all place-holders for the present_with_all rule.
  425. *
  426. * @param string $message
  427. * @param string $attribute
  428. * @param string $rule
  429. * @param array<int,string> $parameters
  430. * @return string
  431. */
  432. protected function replacePresentWithAll($message, $attribute, $rule, $parameters)
  433. {
  434. return $this->replacePresentWith($message, $attribute, $rule, $parameters);
  435. }
  436. /**
  437. * Replace all place-holders for the required_with rule.
  438. *
  439. * @param string $message
  440. * @param string $attribute
  441. * @param string $rule
  442. * @param array<int,string> $parameters
  443. * @return string
  444. */
  445. protected function replaceRequiredWith($message, $attribute, $rule, $parameters)
  446. {
  447. return str_replace(
  448. [':values', ':VALUES', ':Values'],
  449. [
  450. implode(' / ', $this->getAttributeList($parameters)),
  451. Str::upper(implode(' / ', $this->getAttributeList($parameters))),
  452. implode(' / ', array_map(Str::ucfirst(...), $this->getAttributeList($parameters))),
  453. ],
  454. $message,
  455. );
  456. }
  457. /**
  458. * Replace all place-holders for the required_with_all rule.
  459. *
  460. * @param string $message
  461. * @param string $attribute
  462. * @param string $rule
  463. * @param array<int,string> $parameters
  464. * @return string
  465. */
  466. protected function replaceRequiredWithAll($message, $attribute, $rule, $parameters)
  467. {
  468. return $this->replaceRequiredWith($message, $attribute, $rule, $parameters);
  469. }
  470. /**
  471. * Replace all place-holders for the required_without rule.
  472. *
  473. * @param string $message
  474. * @param string $attribute
  475. * @param string $rule
  476. * @param array<int,string> $parameters
  477. * @return string
  478. */
  479. protected function replaceRequiredWithout($message, $attribute, $rule, $parameters)
  480. {
  481. return $this->replaceRequiredWith($message, $attribute, $rule, $parameters);
  482. }
  483. /**
  484. * Replace all place-holders for the required_without_all rule.
  485. *
  486. * @param string $message
  487. * @param string $attribute
  488. * @param string $rule
  489. * @param array<int,string> $parameters
  490. * @return string
  491. */
  492. protected function replaceRequiredWithoutAll($message, $attribute, $rule, $parameters)
  493. {
  494. return $this->replaceRequiredWith($message, $attribute, $rule, $parameters);
  495. }
  496. /**
  497. * Replace all place-holders for the size rule.
  498. *
  499. * @param string $message
  500. * @param string $attribute
  501. * @param string $rule
  502. * @param array<int,string> $parameters
  503. * @return string
  504. */
  505. protected function replaceSize($message, $attribute, $rule, $parameters)
  506. {
  507. return str_replace(':size', $parameters[0], $message);
  508. }
  509. /**
  510. * Replace all place-holders for the gt rule.
  511. *
  512. * @param string $message
  513. * @param string $attribute
  514. * @param string $rule
  515. * @param array<int,string> $parameters
  516. * @return string
  517. */
  518. protected function replaceGt($message, $attribute, $rule, $parameters)
  519. {
  520. if (is_null($value = $this->getValue($parameters[0]))) {
  521. return str_replace(':value', $this->getDisplayableAttribute($parameters[0]), $message);
  522. }
  523. return str_replace(':value', $this->getSize($attribute, $value), $message);
  524. }
  525. /**
  526. * Replace all place-holders for the lt rule.
  527. *
  528. * @param string $message
  529. * @param string $attribute
  530. * @param string $rule
  531. * @param array<int,string> $parameters
  532. * @return string
  533. */
  534. protected function replaceLt($message, $attribute, $rule, $parameters)
  535. {
  536. return $this->replaceGt($message, $attribute, $rule, $parameters);
  537. }
  538. /**
  539. * Replace all place-holders for the gte rule.
  540. *
  541. * @param string $message
  542. * @param string $attribute
  543. * @param string $rule
  544. * @param array<int,string> $parameters
  545. * @return string
  546. */
  547. protected function replaceGte($message, $attribute, $rule, $parameters)
  548. {
  549. return $this->replaceGt($message, $attribute, $rule, $parameters);
  550. }
  551. /**
  552. * Replace all place-holders for the lte rule.
  553. *
  554. * @param string $message
  555. * @param string $attribute
  556. * @param string $rule
  557. * @param array<int,string> $parameters
  558. * @return string
  559. */
  560. protected function replaceLte($message, $attribute, $rule, $parameters)
  561. {
  562. return $this->replaceGt($message, $attribute, $rule, $parameters);
  563. }
  564. /**
  565. * Replace all place-holders for the required_if rule.
  566. *
  567. * @param string $message
  568. * @param string $attribute
  569. * @param string $rule
  570. * @param array<int,string> $parameters
  571. * @return string
  572. */
  573. protected function replaceRequiredIf($message, $attribute, $rule, $parameters)
  574. {
  575. return $this->replaceAcceptedIf($message, $attribute, $rule, $parameters);
  576. }
  577. /**
  578. * Replace all place-holders for the required_if_accepted rule.
  579. *
  580. * @param string $message
  581. * @param string $attribute
  582. * @param string $rule
  583. * @param array<int,string> $parameters
  584. * @return string
  585. */
  586. protected function replaceRequiredIfAccepted($message, $attribute, $rule, $parameters)
  587. {
  588. $value = $this->getDisplayableAttribute($parameters[0]);
  589. return $this->replaceWhileKeepingCase($message, ['other' => $value]);
  590. }
  591. /**
  592. * Replace all place-holders for the required_if_declined rule.
  593. *
  594. * @param string $message
  595. * @param string $attribute
  596. * @param string $rule
  597. * @param array<int,string> $parameters
  598. * @return string
  599. */
  600. protected function replaceRequiredIfDeclined($message, $attribute, $rule, $parameters)
  601. {
  602. return $this->replaceRequiredIfAccepted($message, $attribute, $rule, $parameters);
  603. }
  604. /**
  605. * Replace all place-holders for the required_unless rule.
  606. *
  607. * @param string $message
  608. * @param string $attribute
  609. * @param string $rule
  610. * @param array<int,string> $parameters
  611. * @return string
  612. */
  613. protected function replaceRequiredUnless($message, $attribute, $rule, $parameters)
  614. {
  615. $other = $this->getDisplayableAttribute($parameters[0]);
  616. $values = [];
  617. foreach (array_slice($parameters, 1) as $value) {
  618. $values[] = $this->getDisplayableValue($parameters[0], $value);
  619. }
  620. return str_replace(
  621. [':other', ':OTHER', ':Other', ':values', ':VALUES', ':Values'],
  622. [
  623. $other,
  624. Str::upper($other),
  625. Str::ucfirst($other),
  626. implode(', ', $values),
  627. Str::upper(implode(', ', $values)),
  628. implode(', ', array_map(Str::ucfirst(...), $values)),
  629. ],
  630. $message
  631. );
  632. }
  633. /**
  634. * Replace all place-holders for the prohibited_if rule.
  635. *
  636. * @param string $message
  637. * @param string $attribute
  638. * @param string $rule
  639. * @param array<int,string> $parameters
  640. * @return string
  641. */
  642. protected function replaceProhibitedIf($message, $attribute, $rule, $parameters)
  643. {
  644. return $this->replaceAcceptedIf($message, $attribute, $rule, $parameters);
  645. }
  646. /**
  647. * Replace all place-holders for the prohibited_if_accepted rule.
  648. *
  649. * @param string $message
  650. * @param string $attribute
  651. * @param string $rule
  652. * @param array<int,string> $parameters
  653. * @return string
  654. */
  655. protected function replaceProhibitedIfAccepted($message, $attribute, $rule, $parameters)
  656. {
  657. return $this->replaceRequiredIfAccepted($message, $attribute, $rule, $parameters);
  658. }
  659. /**
  660. * Replace all place-holders for the prohibited_if_declined rule.
  661. *
  662. * @param string $message
  663. * @param string $attribute
  664. * @param string $rule
  665. * @param array<int,string> $parameters
  666. * @return string
  667. */
  668. protected function replaceProhibitedIfDeclined($message, $attribute, $rule, $parameters)
  669. {
  670. return $this->replaceRequiredIfAccepted($message, $attribute, $rule, $parameters);
  671. }
  672. /**
  673. * Replace all place-holders for the prohibited_unless rule.
  674. *
  675. * @param string $message
  676. * @param string $attribute
  677. * @param string $rule
  678. * @param array<int,string> $parameters
  679. * @return string
  680. */
  681. protected function replaceProhibitedUnless($message, $attribute, $rule, $parameters)
  682. {
  683. return $this->replaceRequiredUnless($message, $attribute, $rule, $parameters);
  684. }
  685. /**
  686. * Replace all place-holders for the prohibited_with rule.
  687. *
  688. * @param string $message
  689. * @param string $attribute
  690. * @param string $rule
  691. * @param array<int,string> $parameters
  692. * @return string
  693. */
  694. protected function replaceProhibits($message, $attribute, $rule, $parameters)
  695. {
  696. return str_replace(
  697. [':other', ':OTHER', ':Other'],
  698. [
  699. implode(' / ', $this->getAttributeList($parameters)),
  700. Str::upper(implode(' / ', $this->getAttributeList($parameters))),
  701. implode(' / ', array_map(Str::ucfirst(...), $this->getAttributeList($parameters))),
  702. ],
  703. $message
  704. );
  705. }
  706. /**
  707. * Replace all place-holders for the same rule.
  708. *
  709. * @param string $message
  710. * @param string $attribute
  711. * @param string $rule
  712. * @param array<int,string> $parameters
  713. * @return string
  714. */
  715. protected function replaceSame($message, $attribute, $rule, $parameters)
  716. {
  717. $value = $this->getDisplayableAttribute($parameters[0]);
  718. return $this->replaceWhileKeepingCase($message, ['other' => $value]);
  719. }
  720. /**
  721. * Replace all place-holders for the before rule.
  722. *
  723. * @param string $message
  724. * @param string $attribute
  725. * @param string $rule
  726. * @param array<int,string> $parameters
  727. * @return string
  728. */
  729. protected function replaceBefore($message, $attribute, $rule, $parameters)
  730. {
  731. if (! strtotime($parameters[0])) {
  732. return str_replace(':date', $this->getDisplayableAttribute($parameters[0]), $message);
  733. }
  734. return str_replace(':date', $this->getDisplayableValue($attribute, $parameters[0]), $message);
  735. }
  736. /**
  737. * Replace all place-holders for the before_or_equal rule.
  738. *
  739. * @param string $message
  740. * @param string $attribute
  741. * @param string $rule
  742. * @param array<int,string> $parameters
  743. * @return string
  744. */
  745. protected function replaceBeforeOrEqual($message, $attribute, $rule, $parameters)
  746. {
  747. return $this->replaceBefore($message, $attribute, $rule, $parameters);
  748. }
  749. /**
  750. * Replace all place-holders for the after rule.
  751. *
  752. * @param string $message
  753. * @param string $attribute
  754. * @param string $rule
  755. * @param array<int,string> $parameters
  756. * @return string
  757. */
  758. protected function replaceAfter($message, $attribute, $rule, $parameters)
  759. {
  760. return $this->replaceBefore($message, $attribute, $rule, $parameters);
  761. }
  762. /**
  763. * Replace all place-holders for the after_or_equal rule.
  764. *
  765. * @param string $message
  766. * @param string $attribute
  767. * @param string $rule
  768. * @param array<int,string> $parameters
  769. * @return string
  770. */
  771. protected function replaceAfterOrEqual($message, $attribute, $rule, $parameters)
  772. {
  773. return $this->replaceBefore($message, $attribute, $rule, $parameters);
  774. }
  775. /**
  776. * Replace all place-holders for the date_equals rule.
  777. *
  778. * @param string $message
  779. * @param string $attribute
  780. * @param string $rule
  781. * @param array<int,string> $parameters
  782. * @return string
  783. */
  784. protected function replaceDateEquals($message, $attribute, $rule, $parameters)
  785. {
  786. return $this->replaceBefore($message, $attribute, $rule, $parameters);
  787. }
  788. /**
  789. * Replace all place-holders for the dimensions rule.
  790. *
  791. * @param string $message
  792. * @param string $attribute
  793. * @param string $rule
  794. * @param array<int,string> $parameters
  795. * @return string
  796. */
  797. protected function replaceDimensions($message, $attribute, $rule, $parameters)
  798. {
  799. $parameters = $this->parseNamedParameters($parameters);
  800. if (is_array($parameters)) {
  801. foreach ($parameters as $key => $value) {
  802. $message = str_replace(':'.$key, $value, $message);
  803. }
  804. }
  805. return $message;
  806. }
  807. /**
  808. * Replace all place-holders for the ends_with rule.
  809. *
  810. * @param string $message
  811. * @param string $attribute
  812. * @param string $rule
  813. * @param array<int,string> $parameters
  814. * @return string
  815. */
  816. protected function replaceEndsWith($message, $attribute, $rule, $parameters)
  817. {
  818. return $this->replaceIn($message, $attribute, $rule, $parameters);
  819. }
  820. /**
  821. * Replace all place-holders for the doesnt_end_with rule.
  822. *
  823. * @param string $message
  824. * @param string $attribute
  825. * @param string $rule
  826. * @param array<int,string> $parameters
  827. * @return string
  828. */
  829. protected function replaceDoesntEndWith($message, $attribute, $rule, $parameters)
  830. {
  831. return $this->replaceIn($message, $attribute, $rule, $parameters);
  832. }
  833. /**
  834. * Replace all place-holders for the starts_with rule.
  835. *
  836. * @param string $message
  837. * @param string $attribute
  838. * @param string $rule
  839. * @param array<int,string> $parameters
  840. * @return string
  841. */
  842. protected function replaceStartsWith($message, $attribute, $rule, $parameters)
  843. {
  844. return $this->replaceIn($message, $attribute, $rule, $parameters);
  845. }
  846. /**
  847. * Replace all place-holders for the doesnt_start_with rule.
  848. *
  849. * @param string $message
  850. * @param string $attribute
  851. * @param string $rule
  852. * @param array<int,string> $parameters
  853. * @return string
  854. */
  855. protected function replaceDoesntStartWith($message, $attribute, $rule, $parameters)
  856. {
  857. return $this->replaceIn($message, $attribute, $rule, $parameters);
  858. }
  859. /**
  860. * Replace all place-holders for the doesnt_contain rule.
  861. *
  862. * @param string $message
  863. * @param string $attribute
  864. * @param string $rule
  865. * @param array<int,string> $parameters
  866. * @return string
  867. */
  868. protected function replaceDoesntContain($message, $attribute, $rule, $parameters)
  869. {
  870. return $this->replaceIn($message, $attribute, $rule, $parameters);
  871. }
  872. /**
  873. * Replace the given string while maintaining different casing variants.
  874. *
  875. * @param array<string, string> $mapping
  876. */
  877. private function replaceWhileKeepingCase(string $message, array $mapping): string
  878. {
  879. $fn = [fn ($v) => $v, Str::upper(...), Str::ucfirst(...)];
  880. $cases = array_reduce(
  881. array_keys($mapping),
  882. fn (array $carry, string $placeholder) => [...$carry, ...array_map(fn (callable $fn) => ':'.$fn($placeholder), $fn)],
  883. [],
  884. );
  885. $replacements = array_reduce(
  886. array_values($mapping),
  887. fn (array $carry, string $parameter) => [...$carry, ...array_map(fn (callable $fn) => $fn($parameter), $fn)],
  888. [],
  889. );
  890. return str_replace($cases, $replacements, $message);
  891. }
  892. }