Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.78 KB | None | 0 0
  1. $data = array('input_string_array' => array('aaa', 'abaa', 'abaca'));
  2. $args = array(
  3. 'component' => array('filter' => FILTER_DEFAULT,
  4. 'flags' => FILTER_REQUIRE_ARRAY,
  5. 'options' => array('min_length' => 1, 'max_length' => 10)
  6. )
  7. );
  8.  
  9. var_dump(filter_var_array($data, $args));
  10.  
  11. echo filter_var($string, FILTER_CALLBACK, array("options"=> array("lengthChecker", "5")));
  12.  
  13. $data = array('input_string_array' => array('', 'aaa', 'abaa', 'abaca'));
  14. $args = array(
  15. 'input_string_array' => array(
  16. 'filter' => FILTER_VALIDATE_REGEXP,
  17. 'flags' => FILTER_REQUIRE_ARRAY|FILTER_NULL_ON_FAILURE,
  18. 'options' => array('regexp'=>'/^.{1,3}$/')
  19. )
  20. );
  21. var_dump(filter_var_array($data, $args));
  22.  
  23. array(1) {
  24. ["input_string_array"]=>
  25. array(4) {
  26. [0]=>
  27. NULL
  28. [1]=>
  29. string(3) "aaa"
  30. [2]=>
  31. NULL
  32. [3]=>
  33. NULL
  34. }
  35. }
  36.  
  37. $filteredArray = filter_var_array($arrayToFilter, $filterInstuctionsArray);
  38.  
  39. 'options' => [$this, 'callbackMethod']
  40.  
  41. 'options' => [$this->object, 'callbackMethod']
  42.  
  43. 'options' => [$object, 'callbackMethod']
  44.  
  45. 'options' => 'callbackFunction'
  46.  
  47. function ($value) use ($min, $max) {
  48. $length = mb_strlen($value, 'UTF-8');
  49. return ($length >= $min) && ($length <= $max);
  50. }
  51.  
  52. $filterInstructionsArray[
  53. 'fName' = ['filter' => FILTER_CALLBACK,
  54. 'flags' => FILTER_REQUIRE_SCALAR,
  55. 'options' => function ($value) use ($min, $max) {
  56. $length = mb_strlen($value, 'UTF-8');
  57. return ($length >= $min) && ($length <= $max);}],
  58. 'lName' = ['filter' => FILTER_CALLBACK,
  59. 'flags' => FILTER_REQUIRE_SCALAR,
  60. 'options' => function ($value) use ($min, $max) {
  61. $length = mb_strlen($value, 'UTF-8');
  62. return ($length >= $min) && ($length <= $max);}]
  63. ];
  64.  
  65. private $checkNameLength = function ($value) use ($this->nameMin, $this->nameMax) {
  66. $length = mb_strlen($value, 'UTF-8');
  67. return ($length >= $this->nameMin) && ($length <= $this->nameMax);
  68. };
  69.  
  70. $checkNameLength = function ($value) use ($min, $max) {
  71. $length = mb_strlen($value, 'UTF-8');
  72. return ($length >= $min) && ($length <= $max);
  73. };
  74.  
  75. $filterInstructionsArray[
  76. 'fName' = ['filter' => FILTER_CALLBACK,
  77. 'flags' => FILTER_REQUIRE_SCALAR,
  78. 'options' => [$this, 'checkNameLength']]
  79. ];
  80.  
  81. $filterInstructionsArray[
  82. 'fName' = ['filter' => FILTER_CALLBACK,
  83. 'flags' => FILTER_REQUIRE_SCALAR,
  84. 'options' => 'checkNameLength']
  85. ];
  86.  
  87. $checkName = function ($value) use ($min, $max) {
  88.  
  89. function lengthTest($string, $min, $max){
  90. $length = mb_strlen($string, 'UTF-8');
  91. return ($length >= $min) && ($length <= $max);
  92. }
  93.  
  94. return lengthTest($value, $min, $max);
  95. };
  96.  
  97. $filterInstructionsArray[
  98. 'fName' = ['filter' => FILTER_CALLBACK,
  99. 'flags' => FILTER_REQUIRE_SCALAR,
  100. 'options' => [$checkName, 'lengthTest']]
  101. ];
  102.  
  103. //If you just want to test only the lengths first, this is
  104. //very inefficient. Assume each $regex is only checking string length.
  105.  
  106. $filterLengthInstructions = [
  107. 'fName' => ['filter' => FILTER_VALIDATE_REGEXP,
  108. 'flags' => FILTER_REQUIRE_SCALAR,
  109. 'options' => ['regexp' => $fNameRegex]],
  110. 'lName' => ['filter' => FILTER_VALIDATE_REGEXP,
  111. 'flags' => FILTER_REQUIRE_SCALAR,
  112. 'options' => ['regexp' => $lNameRegex]],
  113. 'company' => ['filter' => FILTER_VALIDATE_REGEXP,
  114. 'flags' => FILTER_REQUIRE_SCALAR,
  115. 'options' => ['regexp' => $comanyRegex]],
  116. 'address1' => ['filter' => FILTER_VALIDATE_REGEXP,
  117. 'flags' => FILTER_REQUIRE_SCALAR,
  118. 'options' => ['regexp' => $address1Regex]],
  119. 'address2' => ['filter' => FILTER_VALIDATE_REGEXP,
  120. 'flags' => FILTER_REQUIRE_SCALAR,
  121. 'options' => ['regexp' => $address2Regex]],
  122. 'zip' => ['filter' => FILTER_VALIDATE_REGEXP,
  123. 'flags' => FILTER_REQUIRE_SCALAR,
  124. 'options' => ['regexp' => $zipRegex]],
  125. 'website' => ['filter' => FILTER_VALIDATE_REGEXP,
  126. 'flags' => FILTER_REQUIRE_SCALAR,
  127. 'options' => ['regexp' => $urlRegex]],
  128. 'email' => ['filter' => FILTER_VALIDATE_REGEXP,
  129. 'flags' => FILTER_REQUIRE_SCALAR,
  130. 'options' => ['regexp' => $emailRegex]]
  131. ];
  132.  
  133. $this->testString($string, $min, $max, $pattern, $errorMessage);
  134.  
  135. $this->stringTester->testString($string, $min, $max, $pattern, $errorMessage);
  136.  
  137. abstract Class Tester
  138. {
  139.  
  140. }
  141.  
  142. class StringTester extends Tester
  143. {
  144. private function testString($string, $min, $max, $pattern, &$errorMessage)
  145. {
  146. $length = mb_strlen($string, 'UTF-8');
  147.  
  148. if($length < $min) //Test string against minimum length.
  149. {
  150. $errorMessage = 'Too small! ('.$min.' min, ' .$length. ' given.)';
  151. }
  152. elseif($length > $max) //Test string against maximum length.
  153. {
  154. $errorMessage = 'Too large! ('.$max.' max, ' .$length. ' given.)';
  155. }
  156. elseif(preg_match($pattern, $string) === 0) //Test string's pattern.
  157. {
  158. $errorMessage = 'Invalid string format!';
  159. }
  160. else
  161. {
  162. $errorMessage = ''; //The error message is the empty string.
  163. }
  164.  
  165. return;
  166. }
  167. }
  168.  
  169. abstract Class Validator
  170. {
  171. //Arrays
  172. protected $inputArray;
  173. protected $errorMessagesArray = [];
  174. protected $stringTestRulesArray; //I know. I know. :-)
  175.  
  176. //Objects
  177. protected $stringTester;
  178.  
  179. //Abstract functions
  180. abstract public function validate();
  181.  
  182. public function __construct(Tester $stringTester, array $inputArray, array $stringTestRutlesArray)
  183. {
  184. $this->stringTester = $stringTester;
  185. $this->inputArray = $inputArray;
  186. $this->stringTestRulesArray = $stringTestRulesArray
  187. }
  188.  
  189. public function getInput()
  190. {
  191. return $this->inputArray;
  192. }
  193.  
  194. public function getErrorMessages()
  195. {
  196. return $this->errorMessagesArray();
  197. }
  198.  
  199. protected function validateStrings()
  200. {
  201. //Notice how input values correspond to error message elements via $key.
  202. foreach($this->stringTestRulesArray as $key = $valuesArr)
  203. {
  204. $this->stringTester->testString($this->inputArray[$key], $valuesArr['min'], $valuesArr['max'], $valuesArr['pattern'], $this->errorMessagesArray[$key]);
  205. }
  206.  
  207. return;
  208. }
  209.  
  210. }
  211.  
  212. class ContactValidator extends Validator
  213. {
  214. public function __construct(Tester $stringTester, Sanitizer $sanitizer)
  215. {
  216. $stringTestRulesArray = [
  217. 'fName' => ['min' => 1, 'max' => 25, 'pattern' => '/[A-Za-z' -]/'],
  218. 'lName' => ['min' => 1, 'max' => 40, 'pattern' => '/[A-Za-z' -]/']
  219. ];
  220.  
  221. parent::__construct($stringTester, $sanitizer->getInput(), $stringTestRulesArray);
  222. }
  223.  
  224. public function validate()
  225. {
  226. $this->validateStrings();
  227. //Other, contact form specific validation stuff.
  228. }
  229. }
  230.  
  231. class RegisterValidator extends Validator
  232. {
  233. public function __construct(Tester $stringTester, Sanitizer $sanitizer)
  234. {
  235. $stringTestRulesArray = [
  236. 'fName' => ['min' => 1, 'max' => 30, 'pattern' => '/[A-Za-z' -]/'],
  237. 'lName' => ['min' => 1, 'max' => 45, 'pattern' => '/[A-Za-z' -]/']
  238. ];
  239.  
  240. parent::__construct($stringTester, $sanitizer->getInput(), $stringTestRulesArray);
  241. }
  242.  
  243. public function validate()
  244. {
  245. $this->validateStrings();
  246. //Other, register form specific validation stuff.
  247. }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement