InArray.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ gmd9ouS4xubtNXtVQIQIKYC/O5Fd1vw7CrUNuUkKDh4=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2014 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.0
  11. */
  12. /**
  13. * in array validator class
  14. */
  15. namespace Cube\Validate;
  16. class InArray extends AbstractValidate
  17. {
  18. protected $_message = "'%s' was not found in the haystack.";
  19. /**
  20. *
  21. * array to compare the needle against
  22. *
  23. * @var array
  24. */
  25. protected $_haystack = array();
  26. /**
  27. *
  28. * class constructor
  29. *
  30. * initialize the haystack
  31. *
  32. * @param array $haystack
  33. */
  34. public function __construct(array $haystack = null)
  35. {
  36. if ($haystack !== null) {
  37. $this->setHaystack($haystack);
  38. }
  39. }
  40. /**
  41. *
  42. * get haystack
  43. *
  44. * @return bool
  45. */
  46. public function getHaystack()
  47. {
  48. return $this->_haystack;
  49. }
  50. /**
  51. *
  52. * set haystack
  53. *
  54. * @param array $haystack
  55. * @return \Cube\Validate\InArray
  56. */
  57. public function setHaystack(array $haystack)
  58. {
  59. $this->_haystack = $haystack;
  60. return $this;
  61. }
  62. /**
  63. *
  64. * checks if the variable is contained in the haystack submitted
  65. *
  66. * @return bool return true if the validation is successful
  67. */
  68. public function isValid()
  69. {
  70. if (!in_array($this->_value, $this->_haystack)) {
  71. return false;
  72. }
  73. return true;
  74. }
  75. }