Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $data = array('input_string_array' => array('aaa', 'abaa', 'abaca'));
- $args = array(
- 'component' => array('filter' => FILTER_DEFAULT,
- 'flags' => FILTER_REQUIRE_ARRAY,
- 'options' => array('min_length' => 1, 'max_length' => 10)
- )
- );
- var_dump(filter_var_array($data, $args));
- echo filter_var($string, FILTER_CALLBACK, array("options"=> array("lengthChecker", "5")));
- $data = array('input_string_array' => array('', 'aaa', 'abaa', 'abaca'));
- $args = array(
- 'input_string_array' => array(
- 'filter' => FILTER_VALIDATE_REGEXP,
- 'flags' => FILTER_REQUIRE_ARRAY|FILTER_NULL_ON_FAILURE,
- 'options' => array('regexp'=>'/^.{1,3}$/')
- )
- );
- var_dump(filter_var_array($data, $args));
- array(1) {
- ["input_string_array"]=>
- array(4) {
- [0]=>
- NULL
- [1]=>
- string(3) "aaa"
- [2]=>
- NULL
- [3]=>
- NULL
- }
- }
- $filteredArray = filter_var_array($arrayToFilter, $filterInstuctionsArray);
- 'options' => [$this, 'callbackMethod']
- 'options' => [$this->object, 'callbackMethod']
- 'options' => [$object, 'callbackMethod']
- 'options' => 'callbackFunction'
- function ($value) use ($min, $max) {
- $length = mb_strlen($value, 'UTF-8');
- return ($length >= $min) && ($length <= $max);
- }
- $filterInstructionsArray[
- 'fName' = ['filter' => FILTER_CALLBACK,
- 'flags' => FILTER_REQUIRE_SCALAR,
- 'options' => function ($value) use ($min, $max) {
- $length = mb_strlen($value, 'UTF-8');
- return ($length >= $min) && ($length <= $max);}],
- 'lName' = ['filter' => FILTER_CALLBACK,
- 'flags' => FILTER_REQUIRE_SCALAR,
- 'options' => function ($value) use ($min, $max) {
- $length = mb_strlen($value, 'UTF-8');
- return ($length >= $min) && ($length <= $max);}]
- ];
- private $checkNameLength = function ($value) use ($this->nameMin, $this->nameMax) {
- $length = mb_strlen($value, 'UTF-8');
- return ($length >= $this->nameMin) && ($length <= $this->nameMax);
- };
- $checkNameLength = function ($value) use ($min, $max) {
- $length = mb_strlen($value, 'UTF-8');
- return ($length >= $min) && ($length <= $max);
- };
- $filterInstructionsArray[
- 'fName' = ['filter' => FILTER_CALLBACK,
- 'flags' => FILTER_REQUIRE_SCALAR,
- 'options' => [$this, 'checkNameLength']]
- ];
- $filterInstructionsArray[
- 'fName' = ['filter' => FILTER_CALLBACK,
- 'flags' => FILTER_REQUIRE_SCALAR,
- 'options' => 'checkNameLength']
- ];
- $checkName = function ($value) use ($min, $max) {
- function lengthTest($string, $min, $max){
- $length = mb_strlen($string, 'UTF-8');
- return ($length >= $min) && ($length <= $max);
- }
- return lengthTest($value, $min, $max);
- };
- $filterInstructionsArray[
- 'fName' = ['filter' => FILTER_CALLBACK,
- 'flags' => FILTER_REQUIRE_SCALAR,
- 'options' => [$checkName, 'lengthTest']]
- ];
- //If you just want to test only the lengths first, this is
- //very inefficient. Assume each $regex is only checking string length.
- $filterLengthInstructions = [
- 'fName' => ['filter' => FILTER_VALIDATE_REGEXP,
- 'flags' => FILTER_REQUIRE_SCALAR,
- 'options' => ['regexp' => $fNameRegex]],
- 'lName' => ['filter' => FILTER_VALIDATE_REGEXP,
- 'flags' => FILTER_REQUIRE_SCALAR,
- 'options' => ['regexp' => $lNameRegex]],
- 'company' => ['filter' => FILTER_VALIDATE_REGEXP,
- 'flags' => FILTER_REQUIRE_SCALAR,
- 'options' => ['regexp' => $comanyRegex]],
- 'address1' => ['filter' => FILTER_VALIDATE_REGEXP,
- 'flags' => FILTER_REQUIRE_SCALAR,
- 'options' => ['regexp' => $address1Regex]],
- 'address2' => ['filter' => FILTER_VALIDATE_REGEXP,
- 'flags' => FILTER_REQUIRE_SCALAR,
- 'options' => ['regexp' => $address2Regex]],
- 'zip' => ['filter' => FILTER_VALIDATE_REGEXP,
- 'flags' => FILTER_REQUIRE_SCALAR,
- 'options' => ['regexp' => $zipRegex]],
- 'website' => ['filter' => FILTER_VALIDATE_REGEXP,
- 'flags' => FILTER_REQUIRE_SCALAR,
- 'options' => ['regexp' => $urlRegex]],
- 'email' => ['filter' => FILTER_VALIDATE_REGEXP,
- 'flags' => FILTER_REQUIRE_SCALAR,
- 'options' => ['regexp' => $emailRegex]]
- ];
- $this->testString($string, $min, $max, $pattern, $errorMessage);
- $this->stringTester->testString($string, $min, $max, $pattern, $errorMessage);
- abstract Class Tester
- {
- }
- class StringTester extends Tester
- {
- private function testString($string, $min, $max, $pattern, &$errorMessage)
- {
- $length = mb_strlen($string, 'UTF-8');
- if($length < $min) //Test string against minimum length.
- {
- $errorMessage = 'Too small! ('.$min.' min, ' .$length. ' given.)';
- }
- elseif($length > $max) //Test string against maximum length.
- {
- $errorMessage = 'Too large! ('.$max.' max, ' .$length. ' given.)';
- }
- elseif(preg_match($pattern, $string) === 0) //Test string's pattern.
- {
- $errorMessage = 'Invalid string format!';
- }
- else
- {
- $errorMessage = ''; //The error message is the empty string.
- }
- return;
- }
- }
- abstract Class Validator
- {
- //Arrays
- protected $inputArray;
- protected $errorMessagesArray = [];
- protected $stringTestRulesArray; //I know. I know. :-)
- //Objects
- protected $stringTester;
- //Abstract functions
- abstract public function validate();
- public function __construct(Tester $stringTester, array $inputArray, array $stringTestRutlesArray)
- {
- $this->stringTester = $stringTester;
- $this->inputArray = $inputArray;
- $this->stringTestRulesArray = $stringTestRulesArray
- }
- public function getInput()
- {
- return $this->inputArray;
- }
- public function getErrorMessages()
- {
- return $this->errorMessagesArray();
- }
- protected function validateStrings()
- {
- //Notice how input values correspond to error message elements via $key.
- foreach($this->stringTestRulesArray as $key = $valuesArr)
- {
- $this->stringTester->testString($this->inputArray[$key], $valuesArr['min'], $valuesArr['max'], $valuesArr['pattern'], $this->errorMessagesArray[$key]);
- }
- return;
- }
- }
- class ContactValidator extends Validator
- {
- public function __construct(Tester $stringTester, Sanitizer $sanitizer)
- {
- $stringTestRulesArray = [
- 'fName' => ['min' => 1, 'max' => 25, 'pattern' => '/[A-Za-z' -]/'],
- 'lName' => ['min' => 1, 'max' => 40, 'pattern' => '/[A-Za-z' -]/']
- ];
- parent::__construct($stringTester, $sanitizer->getInput(), $stringTestRulesArray);
- }
- public function validate()
- {
- $this->validateStrings();
- //Other, contact form specific validation stuff.
- }
- }
- class RegisterValidator extends Validator
- {
- public function __construct(Tester $stringTester, Sanitizer $sanitizer)
- {
- $stringTestRulesArray = [
- 'fName' => ['min' => 1, 'max' => 30, 'pattern' => '/[A-Za-z' -]/'],
- 'lName' => ['min' => 1, 'max' => 45, 'pattern' => '/[A-Za-z' -]/']
- ];
- parent::__construct($stringTester, $sanitizer->getInput(), $stringTestRulesArray);
- }
- public function validate()
- {
- $this->validateStrings();
- //Other, register form specific validation stuff.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement