Identical.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ 7pAiTDukr7oq4vps+xXLbJkXlO+JpCai+XggHCwKJes=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2017 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.10 [rev.1.10.01]
  11. */
  12. /**
  13. * identical values validator class
  14. */
  15. namespace Cube\Validate;
  16. class Identical extends AbstractValidate
  17. {
  18. protected $_message = "'%s' and '%name%' do not match.";
  19. /**
  20. *
  21. * check for strict values
  22. *
  23. * @var bool
  24. */
  25. private $_strict = true;
  26. /**
  27. *
  28. * variable name
  29. *
  30. * @var string
  31. */
  32. private $_variableName;
  33. /**
  34. *
  35. * variable value
  36. *
  37. * @var mixed
  38. */
  39. private $_variableValue;
  40. /**
  41. *
  42. * class constructor
  43. *
  44. * initialize the variable name and value plus if the matching is strict or not
  45. *
  46. * @param array $data data[0] -> variable name;
  47. * data[1] -> variable value;
  48. * data[2] -> strict comparison
  49. */
  50. public function __construct(array $data = null)
  51. {
  52. if (isset($data[0])) {
  53. $this->setVariableName($data[0]);
  54. }
  55. if (isset($data[1])) {
  56. $this->setVariableValue($data[1]);
  57. }
  58. if (isset($data[2])) {
  59. $this->setStrict($data[2]);
  60. }
  61. }
  62. /**
  63. *
  64. * get strict value
  65. *
  66. * @return bool
  67. */
  68. public function getStrict()
  69. {
  70. return $this->_strict;
  71. }
  72. /**
  73. *
  74. * set strict value
  75. *
  76. * @param bool $strict
  77. *
  78. * @return $this
  79. */
  80. public function setStrict($strict = true)
  81. {
  82. $this->_strict = $strict;
  83. return $this;
  84. }
  85. /**
  86. *
  87. * get variable name
  88. *
  89. * @return string
  90. */
  91. public function getVariableName()
  92. {
  93. return $this->_variableName;
  94. }
  95. /**
  96. *
  97. * set variable name
  98. *
  99. * @param string $variableName
  100. *
  101. * @return $this
  102. */
  103. public function setVariableName($variableName)
  104. {
  105. $this->_variableName = $variableName;
  106. return $this;
  107. }
  108. /**
  109. *
  110. * get variable value
  111. *
  112. * @return mixed
  113. */
  114. public function getVariableValue()
  115. {
  116. return $this->_variableValue;
  117. }
  118. /**
  119. *
  120. * set variable value (can be a string, number, bool, array, object etc)
  121. *
  122. * @param mixed $variableValue
  123. *
  124. * @return $this
  125. */
  126. public function setVariableValue($variableValue)
  127. {
  128. $this->_variableValue = $variableValue;
  129. return $this;
  130. }
  131. /**
  132. *
  133. * returns true if the values are identical
  134. *
  135. * @return bool return true if the validation is successful
  136. */
  137. public function isValid()
  138. {
  139. $this->setMessage(
  140. str_replace('%name%', $this->_variableName, $this->getMessage()));
  141. if (($this->_strict && ($this->_value !== $this->_variableValue)) ||
  142. (!$this->_strict && ($this->_value != $this->_variableValue))
  143. ) {
  144. return false;
  145. }
  146. return true;
  147. }
  148. }