PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/system/classes/kohana/validate.php

https://bitbucket.org/emolina/asesores
PHP | 1191 lines | 596 code | 155 blank | 440 comment | 62 complexity | b1bbc1b2fa8ed524e18b76f86806e171 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Array and variable validation.
  4. *
  5. * @package Kohana
  6. * @category Security
  7. * @author Kohana Team
  8. * @copyright (c) 2008-2010 Kohana Team
  9. * @license http://kohanaframework.org/license
  10. */
  11. class Kohana_Validate extends ArrayObject {
  12. /**
  13. * Creates a new Validation instance.
  14. *
  15. * @param array array to use for validation
  16. * @return Validate
  17. */
  18. public static function factory(array $array)
  19. {
  20. return new Validate($array);
  21. }
  22. /**
  23. * Checks if a field is not empty.
  24. *
  25. * @return boolean
  26. */
  27. public static function not_empty($value)
  28. {
  29. if (is_object($value) AND $value instanceof ArrayObject)
  30. {
  31. // Get the array from the ArrayObject
  32. $value = $value->getArrayCopy();
  33. }
  34. // Value cannot be NULL, FALSE, '', or an empty array
  35. return ! in_array($value, array(NULL, FALSE, '', array()), TRUE);
  36. }
  37. /**
  38. * Checks a field against a regular expression.
  39. *
  40. * @param string value
  41. * @param string regular expression to match (including delimiters)
  42. * @return boolean
  43. */
  44. public static function regex($value, $expression)
  45. {
  46. return (bool) preg_match($expression, (string) $value);
  47. }
  48. /**
  49. * Checks that a field is long enough.
  50. *
  51. * @param string value
  52. * @param integer minimum length required
  53. * @return boolean
  54. */
  55. public static function min_length($value, $length)
  56. {
  57. return UTF8::strlen($value) >= $length;
  58. }
  59. /**
  60. * Checks that a field is short enough.
  61. *
  62. * @param string value
  63. * @param integer maximum length required
  64. * @return boolean
  65. */
  66. public static function max_length($value, $length)
  67. {
  68. return UTF8::strlen($value) <= $length;
  69. }
  70. /**
  71. * Checks that a field is exactly the right length.
  72. *
  73. * @param string value
  74. * @param integer exact length required
  75. * @return boolean
  76. */
  77. public static function exact_length($value, $length)
  78. {
  79. return UTF8::strlen($value) === $length;
  80. }
  81. /**
  82. * Checks that a field is exactly the value required.
  83. *
  84. * @param string value
  85. * @param string required value
  86. * @return boolean
  87. */
  88. public static function equals($value, $required)
  89. {
  90. return ($value === $required);
  91. }
  92. /**
  93. * Check an email address for correct format.
  94. *
  95. * @link http://www.iamcal.com/publish/articles/php/parsing_email/
  96. * @link http://www.w3.org/Protocols/rfc822/
  97. *
  98. * @param string email address
  99. * @param boolean strict RFC compatibility
  100. * @return boolean
  101. */
  102. public static function email($email, $strict = FALSE)
  103. {
  104. if ($strict === TRUE)
  105. {
  106. $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
  107. $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
  108. $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
  109. $pair = '\\x5c[\\x00-\\x7f]';
  110. $domain_literal = "\\x5b($dtext|$pair)*\\x5d";
  111. $quoted_string = "\\x22($qtext|$pair)*\\x22";
  112. $sub_domain = "($atom|$domain_literal)";
  113. $word = "($atom|$quoted_string)";
  114. $domain = "$sub_domain(\\x2e$sub_domain)*";
  115. $local_part = "$word(\\x2e$word)*";
  116. $expression = "/^$local_part\\x40$domain$/D";
  117. }
  118. else
  119. {
  120. $expression = '/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD';
  121. }
  122. return (bool) preg_match($expression, (string) $email);
  123. }
  124. /**
  125. * Validate the domain of an email address by checking if the domain has a
  126. * valid MX record.
  127. *
  128. * @link http://php.net/checkdnsrr not added to Windows until PHP 5.3.0
  129. *
  130. * @param string email address
  131. * @return boolean
  132. */
  133. public static function email_domain($email)
  134. {
  135. // Check if the email domain has a valid MX record
  136. return (bool) checkdnsrr(preg_replace('/^[^@]++@/', '', $email), 'MX');
  137. }
  138. /**
  139. * Validate a URL.
  140. *
  141. * @param string URL
  142. * @return boolean
  143. */
  144. public static function url($url)
  145. {
  146. // Based on http://www.apps.ietf.org/rfc/rfc1738.html#sec-5
  147. if ( ! preg_match(
  148. '~^
  149. # scheme
  150. [-a-z0-9+.]++://
  151. # username:password (optional)
  152. (?:
  153. [-a-z0-9$_.+!*\'(),;?&=%]++ # username
  154. (?::[-a-z0-9$_.+!*\'(),;?&=%]++)? # password (optional)
  155. @
  156. )?
  157. (?:
  158. # ip address
  159. \d{1,3}+(?:\.\d{1,3}+){3}+
  160. | # or
  161. # hostname (captured)
  162. (
  163. (?!-)[-a-z0-9]{1,63}+(?<!-)
  164. (?:\.(?!-)[-a-z0-9]{1,63}+(?<!-)){0,126}+
  165. )
  166. )
  167. # port (optional)
  168. (?::\d{1,5}+)?
  169. # path (optional)
  170. (?:/.*)?
  171. $~iDx', $url, $matches))
  172. return FALSE;
  173. // We matched an IP address
  174. if ( ! isset($matches[1]))
  175. return TRUE;
  176. // Check maximum length of the whole hostname
  177. // http://en.wikipedia.org/wiki/Domain_name#cite_note-0
  178. if (strlen($matches[1]) > 253)
  179. return FALSE;
  180. // An extra check for the top level domain
  181. // It must start with a letter
  182. $tld = ltrim(substr($matches[1], (int) strrpos($matches[1], '.')), '.');
  183. return ctype_alpha($tld[0]);
  184. }
  185. /**
  186. * Validate an IP.
  187. *
  188. * @param string IP address
  189. * @param boolean allow private IP networks
  190. * @return boolean
  191. */
  192. public static function ip($ip, $allow_private = TRUE)
  193. {
  194. // Do not allow reserved addresses
  195. $flags = FILTER_FLAG_NO_RES_RANGE;
  196. if ($allow_private === FALSE)
  197. {
  198. // Do not allow private or reserved addresses
  199. $flags = $flags | FILTER_FLAG_NO_PRIV_RANGE;
  200. }
  201. return (bool) filter_var($ip, FILTER_VALIDATE_IP, $flags);
  202. }
  203. /**
  204. * Validates a credit card number, with a Luhn check if possible.
  205. *
  206. * @param integer credit card number
  207. * @param string|array card type, or an array of card types
  208. * @return boolean
  209. * @uses Validate::luhn
  210. */
  211. public static function credit_card($number, $type = NULL)
  212. {
  213. // Remove all non-digit characters from the number
  214. if (($number = preg_replace('/\D+/', '', $number)) === '')
  215. return FALSE;
  216. if ($type == NULL)
  217. {
  218. // Use the default type
  219. $type = 'default';
  220. }
  221. elseif (is_array($type))
  222. {
  223. foreach ($type as $t)
  224. {
  225. // Test each type for validity
  226. if (Validate::credit_card($number, $t))
  227. return TRUE;
  228. }
  229. return FALSE;
  230. }
  231. $cards = Kohana::config('credit_cards');
  232. // Check card type
  233. $type = strtolower($type);
  234. if ( ! isset($cards[$type]))
  235. return FALSE;
  236. // Check card number length
  237. $length = strlen($number);
  238. // Validate the card length by the card type
  239. if ( ! in_array($length, preg_split('/\D+/', $cards[$type]['length'])))
  240. return FALSE;
  241. // Check card number prefix
  242. if ( ! preg_match('/^'.$cards[$type]['prefix'].'/', $number))
  243. return FALSE;
  244. // No Luhn check required
  245. if ($cards[$type]['luhn'] == FALSE)
  246. return TRUE;
  247. return Validate::luhn($number);
  248. }
  249. /**
  250. * Validate a number against the [Luhn](http://en.wikipedia.org/wiki/Luhn_algorithm)
  251. * (mod10) formula.
  252. *
  253. * @param string number to check
  254. * @return boolean
  255. */
  256. public static function luhn($number)
  257. {
  258. // Force the value to be a string as this method uses string functions.
  259. // Converting to an integer may pass PHP_INT_MAX and result in an error!
  260. $number = (string) $number;
  261. if ( ! ctype_digit($number))
  262. {
  263. // Luhn can only be used on numbers!
  264. return FALSE;
  265. }
  266. // Check number length
  267. $length = strlen($number);
  268. // Checksum of the card number
  269. $checksum = 0;
  270. for ($i = $length - 1; $i >= 0; $i -= 2)
  271. {
  272. // Add up every 2nd digit, starting from the right
  273. $checksum += substr($number, $i, 1);
  274. }
  275. for ($i = $length - 2; $i >= 0; $i -= 2)
  276. {
  277. // Add up every 2nd digit doubled, starting from the right
  278. $double = substr($number, $i, 1) * 2;
  279. // Subtract 9 from the double where value is greater than 10
  280. $checksum += ($double >= 10) ? ($double - 9) : $double;
  281. }
  282. // If the checksum is a multiple of 10, the number is valid
  283. return ($checksum % 10 === 0);
  284. }
  285. /**
  286. * Checks if a phone number is valid.
  287. *
  288. * @param string phone number to check
  289. * @return boolean
  290. */
  291. public static function phone($number, $lengths = NULL)
  292. {
  293. if ( ! is_array($lengths))
  294. {
  295. $lengths = array(7,10,11);
  296. }
  297. // Remove all non-digit characters from the number
  298. $number = preg_replace('/\D+/', '', $number);
  299. // Check if the number is within range
  300. return in_array(strlen($number), $lengths);
  301. }
  302. /**
  303. * Tests if a string is a valid date string.
  304. *
  305. * @param string date to check
  306. * @return boolean
  307. */
  308. public static function date($str)
  309. {
  310. return (strtotime($str) !== FALSE);
  311. }
  312. /**
  313. * Checks whether a string consists of alphabetical characters only.
  314. *
  315. * @param string input string
  316. * @param boolean trigger UTF-8 compatibility
  317. * @return boolean
  318. */
  319. public static function alpha($str, $utf8 = FALSE)
  320. {
  321. $str = (string) $str;
  322. if ($utf8 === TRUE)
  323. {
  324. return (bool) preg_match('/^\pL++$/uD', $str);
  325. }
  326. else
  327. {
  328. return ctype_alpha($str);
  329. }
  330. }
  331. /**
  332. * Checks whether a string consists of alphabetical characters and numbers only.
  333. *
  334. * @param string input string
  335. * @param boolean trigger UTF-8 compatibility
  336. * @return boolean
  337. */
  338. public static function alpha_numeric($str, $utf8 = FALSE)
  339. {
  340. if ($utf8 === TRUE)
  341. {
  342. return (bool) preg_match('/^[\pL\pN]++$/uD', $str);
  343. }
  344. else
  345. {
  346. return ctype_alnum($str);
  347. }
  348. }
  349. /**
  350. * Checks whether a string consists of alphabetical characters, numbers, underscores and dashes only.
  351. *
  352. * @param string input string
  353. * @param boolean trigger UTF-8 compatibility
  354. * @return boolean
  355. */
  356. public static function alpha_dash($str, $utf8 = FALSE)
  357. {
  358. if ($utf8 === TRUE)
  359. {
  360. $regex = '/^[-\pL\pN_]++$/uD';
  361. }
  362. else
  363. {
  364. $regex = '/^[-a-z0-9_]++$/iD';
  365. }
  366. return (bool) preg_match($regex, $str);
  367. }
  368. /**
  369. * Checks whether a string consists of digits only (no dots or dashes).
  370. *
  371. * @param string input string
  372. * @param boolean trigger UTF-8 compatibility
  373. * @return boolean
  374. */
  375. public static function digit($str, $utf8 = FALSE)
  376. {
  377. if ($utf8 === TRUE)
  378. {
  379. return (bool) preg_match('/^\pN++$/uD', $str);
  380. }
  381. else
  382. {
  383. return (is_int($str) AND $str >= 0) OR ctype_digit($str);
  384. }
  385. }
  386. /**
  387. * Checks whether a string is a valid number (negative and decimal numbers allowed).
  388. *
  389. * Uses {@link http://www.php.net/manual/en/function.localeconv.php locale conversion}
  390. * to allow decimal point to be locale specific.
  391. *
  392. * @param string input string
  393. * @return boolean
  394. */
  395. public static function numeric($str)
  396. {
  397. // Get the decimal point for the current locale
  398. list($decimal) = array_values(localeconv());
  399. // A lookahead is used to make sure the string contains at least one digit (before or after the decimal point)
  400. return (bool) preg_match('/^-?+(?=.*[0-9])[0-9]*+'.preg_quote($decimal).'?+[0-9]*+$/D', (string) $str);
  401. }
  402. /**
  403. * Tests if a number is within a range.
  404. *
  405. * @param string number to check
  406. * @param integer minimum value
  407. * @param integer maximum value
  408. * @return boolean
  409. */
  410. public static function range($number, $min, $max)
  411. {
  412. return ($number >= $min AND $number <= $max);
  413. }
  414. /**
  415. * Checks if a string is a proper decimal format. Optionally, a specific
  416. * number of digits can be checked too.
  417. *
  418. * @param string number to check
  419. * @param integer number of decimal places
  420. * @param integer number of digits
  421. * @return boolean
  422. */
  423. public static function decimal($str, $places = 2, $digits = NULL)
  424. {
  425. if ($digits > 0)
  426. {
  427. // Specific number of digits
  428. $digits = '{'. (int) $digits.'}';
  429. }
  430. else
  431. {
  432. // Any number of digits
  433. $digits = '+';
  434. }
  435. // Get the decimal point for the current locale
  436. list($decimal) = array_values(localeconv());
  437. return (bool) preg_match('/^[0-9]'.$digits.preg_quote($decimal).'[0-9]{'. (int) $places.'}$/D', $str);
  438. }
  439. /**
  440. * Checks if a string is a proper hexadecimal HTML color value. The validation
  441. * is quite flexible as it does not require an initial "#" and also allows for
  442. * the short notation using only three instead of six hexadecimal characters.
  443. *
  444. * @param string input string
  445. * @return boolean
  446. */
  447. public static function color($str)
  448. {
  449. return (bool) preg_match('/^#?+[0-9a-f]{3}(?:[0-9a-f]{3})?$/iD', $str);
  450. }
  451. // Field filters
  452. protected $_filters = array();
  453. // Field rules
  454. protected $_rules = array();
  455. // Field callbacks
  456. protected $_callbacks = array();
  457. // Field labels
  458. protected $_labels = array();
  459. // Rules that are executed even when the value is empty
  460. protected $_empty_rules = array('not_empty', 'matches');
  461. // Error list, field => rule
  462. protected $_errors = array();
  463. /**
  464. * Sets the unique "any field" key and creates an ArrayObject from the
  465. * passed array.
  466. *
  467. * @param array array to validate
  468. * @return void
  469. */
  470. public function __construct(array $array)
  471. {
  472. parent::__construct($array, ArrayObject::STD_PROP_LIST);
  473. }
  474. /**
  475. * Copies the current filter/rule/callback to a new array.
  476. *
  477. * $copy = $array->copy($new_data);
  478. *
  479. * @param array new data set
  480. * @return Validation
  481. * @since 3.0.5
  482. */
  483. public function copy(array $array)
  484. {
  485. // Create a copy of the current validation set
  486. $copy = clone $this;
  487. // Replace the data set
  488. $copy->exchangeArray($array);
  489. return $copy;
  490. }
  491. /**
  492. * Returns the array representation of the current object.
  493. *
  494. * @return array
  495. */
  496. public function as_array()
  497. {
  498. return $this->getArrayCopy();
  499. }
  500. /**
  501. * Sets or overwrites the label name for a field.
  502. *
  503. * @param string field name
  504. * @param string label
  505. * @return $this
  506. */
  507. public function label($field, $label)
  508. {
  509. // Set the label for this field
  510. $this->_labels[$field] = $label;
  511. return $this;
  512. }
  513. /**
  514. * Sets labels using an array.
  515. *
  516. * @param array list of field => label names
  517. * @return $this
  518. */
  519. public function labels(array $labels)
  520. {
  521. $this->_labels = $labels + $this->_labels;
  522. return $this;
  523. }
  524. /**
  525. * Overwrites or appends filters to a field. Each filter will be executed once.
  526. * All rules must be valid PHP callbacks.
  527. *
  528. * // Run trim() on all fields
  529. * $validation->filter(TRUE, 'trim');
  530. *
  531. * @param string field name
  532. * @param mixed valid PHP callback
  533. * @param array extra parameters for the filter
  534. * @return $this
  535. */
  536. public function filter($field, $filter, array $params = NULL)
  537. {
  538. if ($field !== TRUE AND ! isset($this->_labels[$field]))
  539. {
  540. // Set the field label to the field name
  541. $this->_labels[$field] = preg_replace('/[^\pL]+/u', ' ', $field);
  542. }
  543. // Store the filter and params for this rule
  544. $this->_filters[$field][$filter] = (array) $params;
  545. return $this;
  546. }
  547. /**
  548. * Add filters using an array.
  549. *
  550. * @param string field name
  551. * @param array list of functions or static method name
  552. * @return $this
  553. */
  554. public function filters($field, array $filters)
  555. {
  556. foreach ($filters as $filter => $params)
  557. {
  558. $this->filter($field, $filter, $params);
  559. }
  560. return $this;
  561. }
  562. /**
  563. * Overwrites or appends rules to a field. Each rule will be executed once.
  564. * All rules must be string names of functions method names.
  565. *
  566. * // The "username" must not be empty and have a minimum length of 4
  567. * $validation->rule('username', 'not_empty')
  568. * ->rule('username', 'min_length', array(4));
  569. *
  570. * @param string field name
  571. * @param string function or static method name
  572. * @param array extra parameters for the rule
  573. * @return $this
  574. */
  575. public function rule($field, $rule, array $params = NULL)
  576. {
  577. if ($field !== TRUE AND ! isset($this->_labels[$field]))
  578. {
  579. // Set the field label to the field name
  580. $this->_labels[$field] = trim(preg_replace('/[^\pL]+/u', ' ', $field));
  581. }
  582. if ('matches' === $rule AND ! isset($this->_labels[$params[0]]))
  583. {
  584. $match_field = $params[0];
  585. $this->_labels[$match_field] = trim(preg_replace('/[^\pL]+/u', ' ', $match_field));
  586. }
  587. // Store the rule and params for this rule
  588. $this->_rules[$field][$rule] = (array) $params;
  589. return $this;
  590. }
  591. /**
  592. * Add rules using an array.
  593. *
  594. * @param string field name
  595. * @param array list of functions or static method name
  596. * @return $this
  597. */
  598. public function rules($field, array $rules)
  599. {
  600. foreach ($rules as $rule => $params)
  601. {
  602. $this->rule($field, $rule, $params);
  603. }
  604. return $this;
  605. }
  606. /**
  607. * Adds a callback to a field. Each callback will be executed only once.
  608. *
  609. * // The "username" must be checked with a custom method
  610. * $validation->callback('username', array($this, 'check_username'));
  611. *
  612. * To add a callback to every field already set, use TRUE for the field name.
  613. *
  614. * @param string field name
  615. * @param mixed callback to add
  616. * @param array extra parameters for the callback
  617. * @return $this
  618. */
  619. public function callback($field, $callback, array $params = array())
  620. {
  621. if ( ! isset($this->_callbacks[$field]))
  622. {
  623. // Create the list for this field
  624. $this->_callbacks[$field] = array();
  625. }
  626. if ($field !== TRUE AND ! isset($this->_labels[$field]))
  627. {
  628. // Set the field label to the field name
  629. $this->_labels[$field] = preg_replace('/[^\pL]+/u', ' ', $field);
  630. }
  631. if ( ! in_array($callback, $this->_callbacks[$field], TRUE))
  632. {
  633. // Store the callback
  634. $this->_callbacks[$field][] = array($callback, $params);
  635. }
  636. return $this;
  637. }
  638. /**
  639. * Add callbacks using an array.
  640. *
  641. * @param string field name
  642. * @param array list of callbacks
  643. * @return $this
  644. */
  645. public function callbacks($field, array $callbacks)
  646. {
  647. foreach ($callbacks as $callback)
  648. {
  649. $this->callback($field, $callback);
  650. }
  651. return $this;
  652. }
  653. /**
  654. * Executes all validation filters, rules, and callbacks. This should
  655. * typically be called within an if/else block.
  656. *
  657. * if ($validation->check())
  658. * {
  659. * // The data is valid, do something here
  660. * }
  661. *
  662. * @param boolean allow empty array?
  663. * @return boolean
  664. */
  665. public function check($allow_empty = FALSE)
  666. {
  667. if (Kohana::$profiling === TRUE)
  668. {
  669. // Start a new benchmark
  670. $benchmark = Profiler::start('Validation', __FUNCTION__);
  671. }
  672. // New data set
  673. $data = $this->_errors = array();
  674. // Assume nothing has been submitted
  675. $submitted = FALSE;
  676. // Get a list of the expected fields
  677. $expected = array_keys($this->_labels);
  678. // Import the filters, rules, and callbacks locally
  679. $filters = $this->_filters;
  680. $rules = $this->_rules;
  681. $callbacks = $this->_callbacks;
  682. foreach ($expected as $field)
  683. {
  684. if (isset($this[$field]))
  685. {
  686. // Some data has been submitted, continue validation
  687. $submitted = TRUE;
  688. // Use the submitted value
  689. $data[$field] = $this[$field];
  690. }
  691. else
  692. {
  693. // No data exists for this field
  694. $data[$field] = NULL;
  695. }
  696. if (isset($filters[TRUE]))
  697. {
  698. if ( ! isset($filters[$field]))
  699. {
  700. // Initialize the filters for this field
  701. $filters[$field] = array();
  702. }
  703. // Append the filters
  704. $filters[$field] = array_merge($filters[$field], $filters[TRUE]);
  705. }
  706. if (isset($rules[TRUE]))
  707. {
  708. if ( ! isset($rules[$field]))
  709. {
  710. // Initialize the rules for this field
  711. $rules[$field] = array();
  712. }
  713. // Append the rules
  714. $rules[$field] = array_merge($rules[$field], $rules[TRUE]);
  715. }
  716. if (isset($callbacks[TRUE]))
  717. {
  718. if ( ! isset($callbacks[$field]))
  719. {
  720. // Initialize the callbacks for this field
  721. $callbacks[$field] = array();
  722. }
  723. // Append the callbacks
  724. $callbacks[$field] = array_merge($callbacks[$field], $callbacks[TRUE]);
  725. }
  726. }
  727. // Overload the current array with the new one
  728. $this->exchangeArray($data);
  729. if ($submitted === FALSE AND ! $allow_empty)
  730. {
  731. // Because no data was submitted, validation will not be forced
  732. return FALSE;
  733. }
  734. // Remove the filters, rules, and callbacks that apply to every field
  735. unset($filters[TRUE], $rules[TRUE], $callbacks[TRUE]);
  736. // Execute the filters
  737. foreach ($filters as $field => $set)
  738. {
  739. // Get the field value
  740. $value = $this[$field];
  741. foreach ($set as $filter => $params)
  742. {
  743. // Add the field value to the parameters
  744. array_unshift($params, $value);
  745. if (strpos($filter, '::') === FALSE)
  746. {
  747. // Use a function call
  748. $function = new ReflectionFunction($filter);
  749. // Call $function($this[$field], $param, ...) with Reflection
  750. $value = $function->invokeArgs($params);
  751. }
  752. else
  753. {
  754. // Split the class and method of the rule
  755. list($class, $method) = explode('::', $filter, 2);
  756. // Use a static method call
  757. $method = new ReflectionMethod($class, $method);
  758. // Call $Class::$method($this[$field], $param, ...) with Reflection
  759. $value = $method->invokeArgs(NULL, $params);
  760. }
  761. }
  762. // Set the filtered value
  763. $this[$field] = $value;
  764. }
  765. // Execute the rules
  766. foreach ($rules as $field => $set)
  767. {
  768. // Get the field value
  769. $value = $this[$field];
  770. foreach ($set as $rule => $params)
  771. {
  772. if ( ! in_array($rule, $this->_empty_rules) AND ! Validate::not_empty($value))
  773. {
  774. // Skip this rule for empty fields
  775. continue;
  776. }
  777. // Add the field value to the parameters
  778. array_unshift($params, $value);
  779. if (method_exists($this, $rule))
  780. {
  781. // Use a method in this object
  782. $method = new ReflectionMethod($this, $rule);
  783. if ($method->isStatic())
  784. {
  785. // Call static::$rule($this[$field], $param, ...) with Reflection
  786. $passed = $method->invokeArgs(NULL, $params);
  787. }
  788. else
  789. {
  790. // Do not use Reflection here, the method may be protected
  791. $passed = call_user_func_array(array($this, $rule), $params);
  792. }
  793. }
  794. elseif (strpos($rule, '::') === FALSE)
  795. {
  796. // Use a function call
  797. $function = new ReflectionFunction($rule);
  798. // Call $function($this[$field], $param, ...) with Reflection
  799. $passed = $function->invokeArgs($params);
  800. }
  801. else
  802. {
  803. // Split the class and method of the rule
  804. list($class, $method) = explode('::', $rule, 2);
  805. // Use a static method call
  806. $method = new ReflectionMethod($class, $method);
  807. // Call $Class::$method($this[$field], $param, ...) with Reflection
  808. $passed = $method->invokeArgs(NULL, $params);
  809. }
  810. if ($passed === FALSE)
  811. {
  812. // Remove the field value from the parameters
  813. array_shift($params);
  814. // Add the rule to the errors
  815. $this->error($field, $rule, $params);
  816. // This field has an error, stop executing rules
  817. break;
  818. }
  819. }
  820. }
  821. // Execute the callbacks
  822. foreach ($callbacks as $field => $set)
  823. {
  824. if (isset($this->_errors[$field]))
  825. {
  826. // Skip any field that already has an error
  827. continue;
  828. }
  829. foreach ($set as $callback_array)
  830. {
  831. list($callback, $params) = $callback_array;
  832. if (is_string($callback) AND strpos($callback, '::') !== FALSE)
  833. {
  834. // Make the static callback into an array
  835. $callback = explode('::', $callback, 2);
  836. }
  837. if (is_array($callback))
  838. {
  839. // Separate the object and method
  840. list ($object, $method) = $callback;
  841. // Use a method in the given object
  842. $method = new ReflectionMethod($object, $method);
  843. if ( ! is_object($object))
  844. {
  845. // The object must be NULL for static calls
  846. $object = NULL;
  847. }
  848. // Call $object->$method($this, $field, $errors) with Reflection
  849. $method->invoke($object, $this, $field, $params);
  850. }
  851. else
  852. {
  853. // Use a function call
  854. $function = new ReflectionFunction($callback);
  855. // Call $function($this, $field, $errors) with Reflection
  856. $function->invoke($this, $field, $params);
  857. }
  858. if (isset($this->_errors[$field]))
  859. {
  860. // An error was added, stop processing callbacks
  861. break;
  862. }
  863. }
  864. }
  865. if (isset($benchmark))
  866. {
  867. // Stop benchmarking
  868. Profiler::stop($benchmark);
  869. }
  870. return empty($this->_errors);
  871. }
  872. /**
  873. * Add an error to a field.
  874. *
  875. * @param string field name
  876. * @param string error message
  877. * @return $this
  878. */
  879. public function error($field, $error, array $params = NULL)
  880. {
  881. $this->_errors[$field] = array($error, $params);
  882. return $this;
  883. }
  884. /**
  885. * Returns the error messages. If no file is specified, the error message
  886. * will be the name of the rule that failed. When a file is specified, the
  887. * message will be loaded from `$field.$rule`, or if no rule-specific message
  888. * exists, `$field.default` will be used. If neither is set, the returned
  889. * message will be `validate.$rule`. If `validate.$rule` is empty,
  890. * then `$file.$field.$rule` will be returned.
  891. *
  892. * By default all messages are translated using the default language.
  893. * A string can be used as the second parameter to specified the language
  894. * that the message was written in.
  895. *
  896. * // Get errors from messages/forms/login.php
  897. * $errors = $validate->errors('forms/login');
  898. *
  899. * @uses Kohana::message
  900. * @param string file to load error messages from
  901. * @param mixed translate the message
  902. * @return array
  903. */
  904. public function errors($file = NULL, $translate = TRUE)
  905. {
  906. if ($file === NULL)
  907. {
  908. // Return the error list
  909. return $this->_errors;
  910. }
  911. // Create a new message list
  912. $messages = array();
  913. foreach ($this->_errors as $field => $set)
  914. {
  915. list($error, $params) = $set;
  916. // Get the label for this field
  917. $label = $this->_labels[$field];
  918. if ($translate)
  919. {
  920. if (is_string($translate))
  921. {
  922. // Translate the label using the specified language
  923. $label = __($label, NULL, $translate);
  924. }
  925. else
  926. {
  927. // Translate the label
  928. $label = __($label);
  929. }
  930. }
  931. // Start the translation values list
  932. $values = array(
  933. ':field' => $label,
  934. ':value' => $this[$field],
  935. );
  936. if (is_array($values[':value']))
  937. {
  938. // All values must be strings
  939. $values[':value'] = implode(', ', Arr::flatten($values[':value']));
  940. }
  941. if ($params)
  942. {
  943. foreach ($params as $key => $value)
  944. {
  945. if (is_array($value))
  946. {
  947. // All values must be strings
  948. $value = implode(', ', Arr::flatten($value));
  949. }
  950. // Check if a label for this parameter exists
  951. if (isset($this->_labels[$value]))
  952. {
  953. // Use the label as the value, eg: related field name for "matches"
  954. $value = $this->_labels[$value];
  955. if ($translate)
  956. {
  957. if (is_string($translate))
  958. {
  959. // Translate the value using the specified language
  960. $value = __($value, NULL, $translate);
  961. }
  962. else
  963. {
  964. // Translate the value
  965. $value = __($value);
  966. }
  967. }
  968. }
  969. // Add each parameter as a numbered value, starting from 1
  970. $values[':param'.($key + 1)] = $value;
  971. }
  972. }
  973. if ($message = Kohana::message($file, "{$field}.{$error}") AND is_string($message))
  974. {
  975. // Found a message for this field and error
  976. }
  977. elseif ($message = Kohana::message($file, "{$field}.default") AND is_string($message))
  978. {
  979. // Found a default message for this field
  980. }
  981. elseif ($message = Kohana::message($file, $error) AND is_string($message))
  982. {
  983. // Found a default message for this error
  984. }
  985. elseif ($message = Kohana::message('validate', $error))
  986. {
  987. // Found a default message for this error
  988. }
  989. else
  990. {
  991. // No message exists, display the path expected
  992. $message = "{$file}.{$field}.{$error}";
  993. }
  994. if ($translate)
  995. {
  996. if (is_string($translate))
  997. {
  998. // Translate the message using specified language
  999. $message = __($message, $values, $translate);
  1000. }
  1001. else
  1002. {
  1003. // Translate the message using the default language
  1004. $message = __($message, $values);
  1005. }
  1006. }
  1007. else
  1008. {
  1009. // Do not translate, just replace the values
  1010. $message = strtr($message, $values);
  1011. }
  1012. // Set the message for this field
  1013. $messages[$field] = $message;
  1014. }
  1015. return $messages;
  1016. }
  1017. /**
  1018. * Checks if a field matches the value of another field.
  1019. *
  1020. * @param string field value
  1021. * @param string field name to match
  1022. * @return boolean
  1023. */
  1024. protected function matches($value, $match)
  1025. {
  1026. return ($value === $this[$match]);
  1027. }
  1028. } // End Validation