validator.php 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. <?php namespace Laravel; use Closure;
  2. class Validator {
  3. /**
  4. * The array being validated.
  5. *
  6. * @var array
  7. */
  8. public $attributes;
  9. /**
  10. * The post-validation error messages.
  11. *
  12. * @var Messages
  13. */
  14. public $errors;
  15. /**
  16. * The validation rules.
  17. *
  18. * @var array
  19. */
  20. protected $rules = array();
  21. /**
  22. * The validation messages.
  23. *
  24. * @var array
  25. */
  26. protected $messages = array();
  27. /**
  28. * The database connection that should be used by the validator.
  29. *
  30. * @var Database\Connection
  31. */
  32. protected $db;
  33. /**
  34. * The bundle for which the validation is being run.
  35. *
  36. * @var string
  37. */
  38. protected $bundle = DEFAULT_BUNDLE;
  39. /**
  40. * The language that should be used when retrieving error messages.
  41. *
  42. * @var string
  43. */
  44. protected $language;
  45. /**
  46. * The size related validation rules.
  47. *
  48. * @var array
  49. */
  50. protected $size_rules = array('size', 'between', 'min', 'max');
  51. /**
  52. * The numeric related validation rules.
  53. *
  54. * @var array
  55. */
  56. protected $numeric_rules = array('numeric', 'integer');
  57. /**
  58. * The registered custom validators.
  59. *
  60. * @var array
  61. */
  62. protected static $validators = array();
  63. /**
  64. * Create a new validator instance.
  65. *
  66. * @param array $attributes
  67. * @param array $rules
  68. * @param array $messages
  69. * @return void
  70. */
  71. public function __construct($attributes, $rules, $messages = array())
  72. {
  73. foreach ($rules as $key => &$rule)
  74. {
  75. $rule = (is_string($rule)) ? explode('|', $rule) : $rule;
  76. }
  77. $this->rules = $rules;
  78. $this->messages = $messages;
  79. $this->attributes = $attributes;
  80. }
  81. /**
  82. * Create a new validator instance.
  83. *
  84. * @param array $attributes
  85. * @param array $rules
  86. * @param array $messages
  87. * @return Validator
  88. */
  89. public static function make($attributes, $rules, $messages = array())
  90. {
  91. return new static($attributes, $rules, $messages);
  92. }
  93. /**
  94. * Register a custom validator.
  95. *
  96. * @param string $name
  97. * @param Closure $validator
  98. * @return void
  99. */
  100. public static function register($name, $validator)
  101. {
  102. static::$validators[$name] = $validator;
  103. }
  104. /**
  105. * Validate the target array using the specified validation rules.
  106. *
  107. * @return bool
  108. */
  109. public function passes()
  110. {
  111. return $this->valid();
  112. }
  113. /**
  114. * Validate the target array using the specified validation rules.
  115. *
  116. * @return bool
  117. */
  118. public function fails()
  119. {
  120. return $this->invalid();
  121. }
  122. /**
  123. * Validate the target array using the specified validation rules.
  124. *
  125. * @return bool
  126. */
  127. public function invalid()
  128. {
  129. return ! $this->valid();
  130. }
  131. /**
  132. * Validate the target array using the specified validation rules.
  133. *
  134. * @return bool
  135. */
  136. public function valid()
  137. {
  138. $this->errors = new Messages;
  139. foreach ($this->rules as $attribute => $rules)
  140. {
  141. foreach ($rules as $rule) $this->check($attribute, $rule);
  142. }
  143. return count($this->errors->messages) == 0;
  144. }
  145. /**
  146. * Evaluate an attribute against a validation rule.
  147. *
  148. * @param string $attribute
  149. * @param string $rule
  150. * @return void
  151. */
  152. protected function check($attribute, $rule)
  153. {
  154. list($rule, $parameters) = $this->parse($rule);
  155. $value = array_get($this->attributes, $attribute);
  156. // Before running the validator, we need to verify that the attribute and rule
  157. // combination is actually validatable. Only the "accepted" rule implies that
  158. // the attribute is "required", so if the attribute does not exist, the other
  159. // rules will not be run for the attribute.
  160. $validatable = $this->validatable($rule, $attribute, $value);
  161. if ($validatable and ! $this->{'validate_'.$rule}($attribute, $value, $parameters, $this))
  162. {
  163. $this->error($attribute, $rule, $parameters);
  164. }
  165. }
  166. /**
  167. * Determine if an attribute is validatable.
  168. *
  169. * To be considered validatable, the attribute must either exist, or the rule
  170. * being checked must implicitly validate "required", such as the "required"
  171. * rule or the "accepted" rule.
  172. *
  173. * @param string $rule
  174. * @param string $attribute
  175. * @param mixed $value
  176. * @return bool
  177. */
  178. protected function validatable($rule, $attribute, $value)
  179. {
  180. return $this->validate_required($attribute, $value) or $this->implicit($rule);
  181. }
  182. /**
  183. * Determine if a given rule implies that the attribute is required.
  184. *
  185. * @param string $rule
  186. * @return bool
  187. */
  188. protected function implicit($rule)
  189. {
  190. return $rule == 'required' or $rule == 'accepted' or $rule == 'required_with';
  191. }
  192. /**
  193. * Add an error message to the validator's collection of messages.
  194. *
  195. * @param string $attribute
  196. * @param string $rule
  197. * @param array $parameters
  198. * @return void
  199. */
  200. protected function error($attribute, $rule, $parameters)
  201. {
  202. $message = $this->replace($this->message($attribute, $rule), $attribute, $rule, $parameters);
  203. $this->errors->add($attribute, $message);
  204. }
  205. /**
  206. * Validate that a required attribute exists in the attributes array.
  207. *
  208. * @param string $attribute
  209. * @param mixed $value
  210. * @return bool
  211. */
  212. protected function validate_required($attribute, $value)
  213. {
  214. if (is_null($value))
  215. {
  216. return false;
  217. }
  218. elseif (is_string($value) and trim($value) === '')
  219. {
  220. return false;
  221. }
  222. elseif ( ! is_null(Input::file($attribute)) and is_array($value) and $value['tmp_name'] == '')
  223. {
  224. return false;
  225. }
  226. return true;
  227. }
  228. /**
  229. * Validate that an attribute exists in the attributes array, if another
  230. * attribute exists in the attributes array.
  231. *
  232. * @param string $attribute
  233. * @param mixed $value
  234. * @param array $parameters
  235. * @return bool
  236. */
  237. protected function validate_required_with($attribute, $value, $parameters)
  238. {
  239. $other = $parameters[0];
  240. if ($this->validate_required($other, $this->attributes[$other]))
  241. {
  242. return $this->validate_required($attribute, $value);
  243. }
  244. return true;
  245. }
  246. /**
  247. * Validate that an attribute has a matching confirmation attribute.
  248. *
  249. * @param string $attribute
  250. * @param mixed $value
  251. * @return bool
  252. */
  253. protected function validate_confirmed($attribute, $value)
  254. {
  255. return $this->validate_same($attribute, $value, array($attribute.'_confirmation'));
  256. }
  257. /**
  258. * Validate that an attribute was "accepted".
  259. *
  260. * This validation rule implies the attribute is "required".
  261. *
  262. * @param string $attribute
  263. * @param mixed $value
  264. * @return bool
  265. */
  266. protected function validate_accepted($attribute, $value)
  267. {
  268. return $this->validate_required($attribute, $value) and ($value == 'yes' or $value == '1');
  269. }
  270. /**
  271. * Validate that an attribute is the same as another attribute.
  272. *
  273. * @param string $attribute
  274. * @param mixed $value
  275. * @param array $parameters
  276. * @return bool
  277. */
  278. protected function validate_same($attribute, $value, $parameters)
  279. {
  280. $other = $parameters[0];
  281. return isset($this->attributes[$other]) and $value == $this->attributes[$other];
  282. }
  283. /**
  284. * Validate that an attribute is different from another attribute.
  285. *
  286. * @param string $attribute
  287. * @param mixed $value
  288. * @param array $parameters
  289. * @return bool
  290. */
  291. protected function validate_different($attribute, $value, $parameters)
  292. {
  293. $other = $parameters[0];
  294. return isset($this->attributes[$other]) and $value != $this->attributes[$other];
  295. }
  296. /**
  297. * Validate that an attribute is numeric.
  298. *
  299. * @param string $attribute
  300. * @param mixed $value
  301. * @return bool
  302. */
  303. protected function validate_numeric($attribute, $value)
  304. {
  305. return is_numeric($value);
  306. }
  307. /**
  308. * Validate that an attribute is an integer.
  309. *
  310. * @param string $attribute
  311. * @param mixed $value
  312. * @return bool
  313. */
  314. protected function validate_integer($attribute, $value)
  315. {
  316. return filter_var($value, FILTER_VALIDATE_INT) !== false;
  317. }
  318. /**
  319. * Validate the size of an attribute.
  320. *
  321. * @param string $attribute
  322. * @param mixed $value
  323. * @param array $parameters
  324. * @return bool
  325. */
  326. protected function validate_size($attribute, $value, $parameters)
  327. {
  328. return $this->size($attribute, $value) == $parameters[0];
  329. }
  330. /**
  331. * Validate the size of an attribute is between a set of values.
  332. *
  333. * @param string $attribute
  334. * @param mixed $value
  335. * @param array $parameters
  336. * @return bool
  337. */
  338. protected function validate_between($attribute, $value, $parameters)
  339. {
  340. $size = $this->size($attribute, $value);
  341. return $size >= $parameters[0] and $size <= $parameters[1];
  342. }
  343. /**
  344. * Validate the size of an attribute is greater than a minimum value.
  345. *
  346. * @param string $attribute
  347. * @param mixed $value
  348. * @param array $parameters
  349. * @return bool
  350. */
  351. protected function validate_min($attribute, $value, $parameters)
  352. {
  353. return $this->size($attribute, $value) >= $parameters[0];
  354. }
  355. /**
  356. * Validate the size of an attribute is less than a maximum value.
  357. *
  358. * @param string $attribute
  359. * @param mixed $value
  360. * @param array $parameters
  361. * @return bool
  362. */
  363. protected function validate_max($attribute, $value, $parameters)
  364. {
  365. return $this->size($attribute, $value) <= $parameters[0];
  366. }
  367. /**
  368. * Get the size of an attribute.
  369. *
  370. * @param string $attribute
  371. * @param mixed $value
  372. * @return mixed
  373. */
  374. protected function size($attribute, $value)
  375. {
  376. // This method will determine if the attribute is a number, string, or file and
  377. // return the proper size accordingly. If it is a number, the number itself is
  378. // the size; if it is a file, the kilobytes is the size; if it is a
  379. // string, the length is the size.
  380. if (is_numeric($value) and $this->has_rule($attribute, $this->numeric_rules))
  381. {
  382. return $this->attributes[$attribute];
  383. }
  384. elseif (array_key_exists($attribute, Input::file()))
  385. {
  386. return $value['size'] / 1024;
  387. }
  388. else
  389. {
  390. return Str::length(trim($value));
  391. }
  392. }
  393. /**
  394. * Validate an attribute is contained within a list of values.
  395. *
  396. * @param string $attribute
  397. * @param mixed $value
  398. * @param array $parameters
  399. * @return bool
  400. */
  401. protected function validate_in($attribute, $value, $parameters)
  402. {
  403. return in_array($value, $parameters);
  404. }
  405. /**
  406. * Validate an attribute is not contained within a list of values.
  407. *
  408. * @param string $attribute
  409. * @param mixed $value
  410. * @param array $parameters
  411. * @return bool
  412. */
  413. protected function validate_not_in($attribute, $value, $parameters)
  414. {
  415. return ! in_array($value, $parameters);
  416. }
  417. /**
  418. * Validate the uniqueness of an attribute value on a given database table.
  419. *
  420. * If a database column is not specified, the attribute will be used.
  421. *
  422. * @param string $attribute
  423. * @param mixed $value
  424. * @param array $parameters
  425. * @return bool
  426. */
  427. protected function validate_unique($attribute, $value, $parameters)
  428. {
  429. // We allow the table column to be specified just in case the column does
  430. // not have the same name as the attribute. It must be within the second
  431. // parameter position, right after the database table name.
  432. if (isset($parameters[1]))
  433. {
  434. $attribute = $parameters[1];
  435. }
  436. $query = $this->db()->table($parameters[0])->where($attribute, '=', $value);
  437. // We also allow an ID to be specified that will not be included in the
  438. // uniqueness check. This makes updating columns easier since it is
  439. // fine for the given ID to exist in the table.
  440. if (isset($parameters[2]))
  441. {
  442. $id = (isset($parameters[3])) ? $parameters[3] : 'id';
  443. $query->where($id, '<>', $parameters[2]);
  444. }
  445. return $query->count() == 0;
  446. }
  447. /**
  448. * Validate the existence of an attribute value in a database table.
  449. *
  450. * @param string $attribute
  451. * @param mixed $value
  452. * @param array $parameters
  453. * @return bool
  454. */
  455. protected function validate_exists($attribute, $value, $parameters)
  456. {
  457. if (isset($parameters[1])) $attribute = $parameters[1];
  458. // Grab the number of elements we are looking for. If the given value is
  459. // in array, we'll count all of the values in the array, otherwise we
  460. // can just make sure the count is greater or equal to one.
  461. $count = (is_array($value)) ? count($value) : 1;
  462. $query = $this->db()->table($parameters[0]);
  463. // If the given value is an array, we will check for the existence of
  464. // all the values in the database, otherwise we'll check for the
  465. // presence of the single given value in the database.
  466. if (is_array($value))
  467. {
  468. $query = $query->where_in($attribute, $value);
  469. }
  470. else
  471. {
  472. $query = $query->where($attribute, '=', $value);
  473. }
  474. return $query->count() >= $count;
  475. }
  476. /**
  477. * Validate that an attribute is a valid IP.
  478. *
  479. * @param string $attribute
  480. * @param mixed $value
  481. * @return bool
  482. */
  483. protected function validate_ip($attribute, $value)
  484. {
  485. return filter_var($value, FILTER_VALIDATE_IP) !== false;
  486. }
  487. /**
  488. * Validate that an attribute is a valid e-mail address.
  489. *
  490. * @param string $attribute
  491. * @param mixed $value
  492. * @return bool
  493. */
  494. protected function validate_email($attribute, $value)
  495. {
  496. return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
  497. }
  498. /**
  499. * Validate that an attribute is a valid URL.
  500. *
  501. * @param string $attribute
  502. * @param mixed $value
  503. * @return bool
  504. */
  505. protected function validate_url($attribute, $value)
  506. {
  507. return filter_var($value, FILTER_VALIDATE_URL) !== false;
  508. }
  509. /**
  510. * Validate that an attribute is an active URL.
  511. *
  512. * @param string $attribute
  513. * @param mixed $value
  514. * @return bool
  515. */
  516. protected function validate_active_url($attribute, $value)
  517. {
  518. $url = str_replace(array('http://', 'https://', 'ftp://'), '', Str::lower($value));
  519. return (trim($url) !== '') ? checkdnsrr($url) : false;
  520. }
  521. /**
  522. * Validate the MIME type of a file is an image MIME type.
  523. *
  524. * @param string $attribute
  525. * @param mixed $value
  526. * @return bool
  527. */
  528. protected function validate_image($attribute, $value)
  529. {
  530. return $this->validate_mimes($attribute, $value, array('jpg', 'png', 'gif', 'bmp'));
  531. }
  532. /**
  533. * Validate that an attribute contains only alphabetic characters.
  534. *
  535. * @param string $attribute
  536. * @param mixed $value
  537. * @return bool
  538. */
  539. protected function validate_alpha($attribute, $value)
  540. {
  541. return preg_match('/^([a-z])+$/i', $value);
  542. }
  543. /**
  544. * Validate that an attribute contains only alpha-numeric characters.
  545. *
  546. * @param string $attribute
  547. * @param mixed $value
  548. * @return bool
  549. */
  550. protected function validate_alpha_num($attribute, $value)
  551. {
  552. return preg_match('/^([a-z0-9])+$/i', $value);
  553. }
  554. /**
  555. * Validate that an attribute contains only alpha-numeric characters, dashes, and underscores.
  556. *
  557. * @param string $attribute
  558. * @param mixed $value
  559. * @return bool
  560. */
  561. protected function validate_alpha_dash($attribute, $value)
  562. {
  563. return preg_match('/^([-a-z0-9_-])+$/i', $value);
  564. }
  565. /**
  566. * Validate that an attribute passes a regular expression check.
  567. *
  568. * @param string $attribute
  569. * @param mixed $value
  570. * @param array $parameters
  571. * @return bool
  572. */
  573. protected function validate_match($attribute, $value, $parameters)
  574. {
  575. return preg_match($parameters[0], $value);
  576. }
  577. /**
  578. * Validate the MIME type of a file upload attribute is in a set of MIME types.
  579. *
  580. * @param string $attribute
  581. * @param array $value
  582. * @param array $parameters
  583. * @return bool
  584. */
  585. protected function validate_mimes($attribute, $value, $parameters)
  586. {
  587. if ( ! is_array($value) or array_get($value, 'tmp_name', '') == '') return true;
  588. foreach ($parameters as $extension)
  589. {
  590. if (File::is($extension, $value['tmp_name']))
  591. {
  592. return true;
  593. }
  594. }
  595. return false;
  596. }
  597. /**
  598. * Validate that an attribute is an array
  599. *
  600. * @param string $attribute
  601. * @param mixed $value
  602. * @return bool
  603. */
  604. protected function validate_array($attribute, $value)
  605. {
  606. return is_array($value);
  607. }
  608. /**
  609. * Validate that an attribute of type array has a specific count
  610. *
  611. * @param string $attribute
  612. * @param mixed $value
  613. * @param array $parameters
  614. * @return bool
  615. */
  616. protected function validate_count($attribute, $value, $parameters)
  617. {
  618. return (is_array($value) && count($value) == $parameters[0]);
  619. }
  620. /**
  621. * Validate that an attribute of type array has a minimum of elements.
  622. *
  623. * @param string $attribute
  624. * @param mixed $value
  625. * @param array $parameters
  626. * @return bool
  627. */
  628. protected function validate_countmin($attribute, $value, $parameters)
  629. {
  630. return (is_array($value) && count($value) >= $parameters[0]);
  631. }
  632. /**
  633. * Validate that an attribute of type array has a maximum of elements.
  634. *
  635. * @param string $attribute
  636. * @param mixed $value
  637. * @param array $parameters
  638. * @return bool
  639. */
  640. protected function validate_countmax($attribute, $value, $parameters)
  641. {
  642. return (is_array($value) && count($value) <= $parameters[0]);
  643. }
  644. /**
  645. * Validate that an attribute of type array has elements between max and min.
  646. *
  647. * @param string $attribute
  648. * @param mixed $value
  649. * @param array $parameters
  650. * @return bool
  651. */
  652. protected function validate_countbetween($attribute, $value, $parameters)
  653. {
  654. return (is_array($value) && count($value) >= $parameters[0] && count($value) <= $parameters[1] );
  655. }
  656. /**
  657. * Validate the date is before a given date.
  658. *
  659. * @param string $attribute
  660. * @param mixed $value
  661. * @param array $parameters
  662. * @return bool
  663. */
  664. protected function validate_before($attribute, $value, $parameters)
  665. {
  666. return (strtotime($value) < strtotime($parameters[0]));
  667. }
  668. /**
  669. * Validate the date is after a given date.
  670. *
  671. * @param string $attribute
  672. * @param mixed $value
  673. * @param array $parameters
  674. * @return bool
  675. */
  676. protected function validate_after($attribute, $value, $parameters)
  677. {
  678. return (strtotime($value) > strtotime($parameters[0]));
  679. }
  680. /**
  681. * Get the proper error message for an attribute and rule.
  682. *
  683. * @param string $attribute
  684. * @param string $rule
  685. * @return string
  686. */
  687. protected function message($attribute, $rule)
  688. {
  689. $bundle = Bundle::prefix($this->bundle);
  690. // First we'll check for developer specified, attribute specific messages.
  691. // These messages take first priority. They allow the fine-grained tuning
  692. // of error messages for each rule.
  693. $custom = $attribute.'_'.$rule;
  694. if (array_key_exists($custom, $this->messages))
  695. {
  696. return $this->messages[$custom];
  697. }
  698. elseif (Lang::has($custom = "{$bundle}validation.custom.{$custom}", $this->language))
  699. {
  700. return Lang::line($custom)->get($this->language);
  701. }
  702. // Next we'll check for developer specified, rule specific error messages.
  703. // These allow the developer to override the error message for an entire
  704. // rule, regardless of the attribute being validated by that rule.
  705. elseif (array_key_exists($rule, $this->messages))
  706. {
  707. return $this->messages[$rule];
  708. }
  709. // If the rule being validated is a "size" rule, we will need to gather
  710. // the specific size message for the type of attribute being validated,
  711. // either a number, file, or string.
  712. elseif (in_array($rule, $this->size_rules))
  713. {
  714. return $this->size_message($bundle, $attribute, $rule);
  715. }
  716. // If no developer specified messages have been set, and no other special
  717. // messages apply to the rule, we will just pull the default validation
  718. // message from the validation language file.
  719. else
  720. {
  721. $line = "{$bundle}validation.{$rule}";
  722. return Lang::line($line)->get($this->language);
  723. }
  724. }
  725. /**
  726. * Get the proper error message for an attribute and size rule.
  727. *
  728. * @param string $bundle
  729. * @param string $attribute
  730. * @param string $rule
  731. * @return string
  732. */
  733. protected function size_message($bundle, $attribute, $rule)
  734. {
  735. // There are three different types of size validations. The attribute
  736. // may be either a number, file, or a string, so we'll check a few
  737. // things to figure out which one it is.
  738. if ($this->has_rule($attribute, $this->numeric_rules))
  739. {
  740. $line = 'numeric';
  741. }
  742. // We assume that attributes present in the $_FILES array are files,
  743. // which makes sense. If the attribute doesn't have numeric rules
  744. // and isn't a file, it's a string.
  745. elseif (array_key_exists($attribute, Input::file()))
  746. {
  747. $line = 'file';
  748. }
  749. else
  750. {
  751. $line = 'string';
  752. }
  753. return Lang::line("{$bundle}validation.{$rule}.{$line}")->get($this->language);
  754. }
  755. /**
  756. * Replace all error message place-holders with actual values.
  757. *
  758. * @param string $message
  759. * @param string $attribute
  760. * @param string $rule
  761. * @param array $parameters
  762. * @return string
  763. */
  764. protected function replace($message, $attribute, $rule, $parameters)
  765. {
  766. $message = str_replace(':attribute', $this->attribute($attribute), $message);
  767. if (method_exists($this, $replacer = 'replace_'.$rule))
  768. {
  769. $message = $this->$replacer($message, $attribute, $rule, $parameters);
  770. }
  771. return $message;
  772. }
  773. /**
  774. * Replace all place-holders for the between rule.
  775. *
  776. * @param string $message
  777. * @param string $attribute
  778. * @param string $rule
  779. * @param array $parameters
  780. * @return string
  781. */
  782. protected function replace_between($message, $attribute, $rule, $parameters)
  783. {
  784. return str_replace(array(':min', ':max'), $parameters, $message);
  785. }
  786. /**
  787. * Replace all place-holders for the size rule.
  788. *
  789. * @param string $message
  790. * @param string $attribute
  791. * @param string $rule
  792. * @param array $parameters
  793. * @return string
  794. */
  795. protected function replace_size($message, $attribute, $rule, $parameters)
  796. {
  797. return str_replace(':size', $parameters[0], $message);
  798. }
  799. /**
  800. * Replace all place-holders for the min rule.
  801. *
  802. * @param string $message
  803. * @param string $attribute
  804. * @param string $rule
  805. * @param array $parameters
  806. * @return string
  807. */
  808. protected function replace_min($message, $attribute, $rule, $parameters)
  809. {
  810. return str_replace(':min', $parameters[0], $message);
  811. }
  812. /**
  813. * Replace all place-holders for the max rule.
  814. *
  815. * @param string $message
  816. * @param string $attribute
  817. * @param string $rule
  818. * @param array $parameters
  819. * @return string
  820. */
  821. protected function replace_max($message, $attribute, $rule, $parameters)
  822. {
  823. return str_replace(':max', $parameters[0], $message);
  824. }
  825. /**
  826. * Replace all place-holders for the in rule.
  827. *
  828. * @param string $message
  829. * @param string $attribute
  830. * @param string $rule
  831. * @param array $parameters
  832. * @return string
  833. */
  834. protected function replace_in($message, $attribute, $rule, $parameters)
  835. {
  836. return str_replace(':values', implode(', ', $parameters), $message);
  837. }
  838. /**
  839. * Replace all place-holders for the not_in rule.
  840. *
  841. * @param string $message
  842. * @param string $attribute
  843. * @param string $rule
  844. * @param array $parameters
  845. * @return string
  846. */
  847. protected function replace_not_in($message, $attribute, $rule, $parameters)
  848. {
  849. return str_replace(':values', implode(', ', $parameters), $message);
  850. }
  851. /**
  852. * Replace all place-holders for the mimes rule.
  853. *
  854. * @param string $message
  855. * @param string $attribute
  856. * @param string $rule
  857. * @param array $parameters
  858. * @return string
  859. */
  860. protected function replace_mimes($message, $attribute, $rule, $parameters)
  861. {
  862. return str_replace(':values', implode(', ', $parameters), $message);
  863. }
  864. /**
  865. * Replace all place-holders for the same rule.
  866. *
  867. * @param string $message
  868. * @param string $attribute
  869. * @param string $rule
  870. * @param array $parameters
  871. * @return string
  872. */
  873. protected function replace_same($message, $attribute, $rule, $parameters)
  874. {
  875. return str_replace(':other', $parameters[0], $message);
  876. }
  877. /**
  878. * Replace all place-holders for the different rule.
  879. *
  880. * @param string $message
  881. * @param string $attribute
  882. * @param string $rule
  883. * @param array $parameters
  884. * @return string
  885. */
  886. protected function replace_different($message, $attribute, $rule, $parameters)
  887. {
  888. return str_replace(':other', $parameters[0], $message);
  889. }
  890. /**
  891. * Replace all place-holders for the before rule.
  892. *
  893. * @param string $message
  894. * @param string $attribute
  895. * @param string $rule
  896. * @param array $parameters
  897. * @return string
  898. */
  899. protected function replace_before($message, $attribute, $rule, $parameters)
  900. {
  901. return str_replace(':date', $parameters[0], $message);
  902. }
  903. /**
  904. * Replace all place-holders for the after rule.
  905. *
  906. * @param string $message
  907. * @param string $attribute
  908. * @param string $rule
  909. * @param array $parameters
  910. * @return string
  911. */
  912. protected function replace_after($message, $attribute, $rule, $parameters)
  913. {
  914. return str_replace(':date', $parameters[0], $message);
  915. }
  916. /**
  917. * Replace all place-holders for the count rule.
  918. *
  919. * @param string $message
  920. * @param string $attribute
  921. * @param string $rule
  922. * @param array $parameters
  923. * @return string
  924. */
  925. protected function replace_count($message, $attribute, $rule, $parameters)
  926. {
  927. return str_replace(':count', $parameters[0], $message);
  928. }
  929. /**
  930. * Replace all place-holders for the countmin rule.
  931. *
  932. * @param string $message
  933. * @param string $attribute
  934. * @param string $rule
  935. * @param array $parameters
  936. * @return string
  937. */
  938. protected function replace_countmin($message, $attribute, $rule, $parameters)
  939. {
  940. return str_replace(':min', $parameters[0], $message);
  941. }
  942. /**
  943. * Replace all place-holders for the countmax rule.
  944. *
  945. * @param string $message
  946. * @param string $attribute
  947. * @param string $rule
  948. * @param array $parameters
  949. * @return string
  950. */
  951. protected function replace_countmax($message, $attribute, $rule, $parameters)
  952. {
  953. return str_replace(':max', $parameters[0], $message);
  954. }
  955. /**
  956. * Replace all place-holders for the between rule.
  957. *
  958. * @param string $message
  959. * @param string $attribute
  960. * @param string $rule
  961. * @param array $parameters
  962. * @return string
  963. */
  964. protected function replace_countbetween($message, $attribute, $rule, $parameters)
  965. {
  966. return str_replace(array(':min', ':max'), $parameters, $message);
  967. }
  968. /**
  969. * Get the displayable name for a given attribute.
  970. *
  971. * @param string $attribute
  972. * @return string
  973. */
  974. protected function attribute($attribute)
  975. {
  976. $bundle = Bundle::prefix($this->bundle);
  977. // More reader friendly versions of the attribute names may be stored
  978. // in the validation language file, allowing a more readable version
  979. // of the attribute name in the message.
  980. $line = "{$bundle}validation.attributes.{$attribute}";
  981. if (Lang::has($line, $this->language))
  982. {
  983. return Lang::line($line)->get($this->language);
  984. }
  985. // If no language line has been specified for the attribute, all of
  986. // the underscores are removed from the attribute name and that
  987. // will be used as the attribute name.
  988. else
  989. {
  990. return str_replace('_', ' ', $attribute);
  991. }
  992. }
  993. /**
  994. * Determine if an attribute has a rule assigned to it.
  995. *
  996. * @param string $attribute
  997. * @param array $rules
  998. * @return bool
  999. */
  1000. protected function has_rule($attribute, $rules)
  1001. {
  1002. foreach ($this->rules[$attribute] as $rule)
  1003. {
  1004. list($rule, $parameters) = $this->parse($rule);
  1005. if (in_array($rule, $rules)) return true;
  1006. }
  1007. return false;
  1008. }
  1009. /**
  1010. * Extract the rule name and parameters from a rule.
  1011. *
  1012. * @param string $rule
  1013. * @return array
  1014. */
  1015. protected function parse($rule)
  1016. {
  1017. $parameters = array();
  1018. // The format for specifying validation rules and parameters follows a
  1019. // {rule}:{parameters} formatting convention. For instance, the rule
  1020. // "max:3" specifies that the value may only be 3 characters long.
  1021. if (($colon = strpos($rule, ':')) !== false)
  1022. {
  1023. $parameters = str_getcsv(substr($rule, $colon + 1));
  1024. }
  1025. return array(is_numeric($colon) ? substr($rule, 0, $colon) : $rule, $parameters);
  1026. }
  1027. /**
  1028. * Set the bundle that the validator is running for.
  1029. *
  1030. * The bundle determines which bundle the language lines will be loaded from.
  1031. *
  1032. * @param string $bundle
  1033. * @return Validator
  1034. */
  1035. public function bundle($bundle)
  1036. {
  1037. $this->bundle = $bundle;
  1038. return $this;
  1039. }
  1040. /**
  1041. * Set the language that should be used when retrieving error messages.
  1042. *
  1043. * @param string $language
  1044. * @return Validator
  1045. */
  1046. public function speaks($language)
  1047. {
  1048. $this->language = $language;
  1049. return $this;
  1050. }
  1051. /**
  1052. * Set the database connection that should be used by the validator.
  1053. *
  1054. * @param Database\Connection $connection
  1055. * @return Validator
  1056. */
  1057. public function connection(Database\Connection $connection)
  1058. {
  1059. $this->db = $connection;
  1060. return $this;
  1061. }
  1062. /**
  1063. * Get the database connection for the Validator.
  1064. *
  1065. * @return Database\Connection
  1066. */
  1067. protected function db()
  1068. {
  1069. if ( ! is_null($this->db)) return $this->db;
  1070. return $this->db = Database::connection();
  1071. }
  1072. /**
  1073. * Dynamically handle calls to custom registered validators.
  1074. */
  1075. public function __call($method, $parameters)
  1076. {
  1077. // First we will slice the "validate_" prefix off of the validator since
  1078. // custom validators aren't registered with such a prefix, then we can
  1079. // just call the method with the given parameters.
  1080. if (isset(static::$validators[$method = substr($method, 9)]))
  1081. {
  1082. return call_user_func_array(static::$validators[$method], $parameters);
  1083. }
  1084. throw new \Exception("Method [$method] does not exist.");
  1085. }
  1086. }