set method.
The following are methods available in the input class.
input class can be easily initialized as shown below.
$input = new Input;
set method is used to set parameters to be validated by
the input class. It's alias method is the test() method which takes the same number
of arguments as parameters.
$input->set($value, $config, $bool); $input->test($value, $config, $bool); // same as above where: $value : value to be tested $config: configuration parameters or options that define action to be performed $bool : a boolean of true tells input class to disallow spaces when validating input value set.
$text1 = 'foo'; $text2 = 'foo bar'; $text1 = $input->set($text1, ['type' => 'string']); // returns foo $text2 = $input->set($text2, ['type' => 'string']); // returns bar //check spaces $text1 = $input->set($text1, ['type' => 'string'], true); // returns foo $text2 = $input->set($text2, ['type' => 'string'], true); // returns null because test string contains spaces
type - defines the type of validation. Options are [string | text | email | integer | number | phone | url | range | pregmatch]
length - defines the minimum and maximum number of characters to be allowed where maximum is default if one value is supplied.
range - defines an array list which a value must be a member of.
$input->set('site@gmail.com', ['type' => 'email']); // returns site@gmail.com
$input->set('site.com', ['type' => 'email']); // returns null
$input->set('10', ['type' => 'number']); // returns 10
$input->set('hi', ['type' => 'number']); // returns null
$input->set('0701323323', ['type' => 'phone']); // returns 0701323323
$input->set('07812', ['type' => 'phone']); // returns null : This uses a regex pattern
$input->set('20', ['type' => 'number', range => ['5', '15', '20']]); // returns 20
$input->set('20', ['type' => 'number', range => ['5', '15', '25']]); // returns null
$input->set('foo', ['type' => 'string', 'length' => 10]); // returns 10, character is less than 10
$input->set('foobar123', ['type' => 'string', 'length' => ['0', '5']]); // returns null, character is greater than 5
$input->set('foo', ['type' => 'text', 'length' => 10]); // returns foo
$input->set('foobar123', ['type' => 'text']); // returns null, value contains number
$input->set('http://site.com', ['type' => 'url']); // returns http://site.com
$input->set('site', ['type' => 'url']); // returns null, value is not a valid url
$input->set('foobar', ['type' => 'string', 'pattern' => 'a-zA-z']); // match data type using pattern
$input->set('400', ['type' => 'number', 'range' => [100, 700]]); // match data type using specific range of values
$input->set('foobar', ['type' => 'string', 'length' => [3, 7]]); // matches a minimum and maximum length of character a data must contain.
strict() method is a directive that prevents the input class from proceeding with
subsequent validations if an error is encountered in previous validations
$input->strict(bool);
where:
bool: set the strict type to true or false. Default is true.
$input->strict();
$input->set('foo', ['type'=>'number']); // returns null
$input->set('foo', ['type'=>'text']); // returns null because a previous validation returned null
strict() method when set to true, is used to to stop a validation process if any of the set() method
cannot validate the data supplied. Once any error occurs in any earlier validation, other subsequent validations after will fail
by returning an empty value.
$input->default_type($type); // set base path where : $type: type of validation
$input->default_type('text'); // set default type
$input->set('foo'); // returns foo
$input->set('foo123'); // returns null
$input->default_length(length); // set base path where : length: array or string of acceptable lengths
$input->default_length(5); // set default length $input->set('foo12'); // returns foo12 $input->set('foobar123'); // returns null
$input->default_range($range); // set default range where : $range: array of acceptable ranges
$input->default_range(['ball', 'cat', 'dog']); // set default range $input->test('cat'); // returns cat $input->test('bird'); // returns null
arrGetsVoid checks if a supplied array has any index key having an empty value within it.
Input::arrGetsVoid($array);
where:
$array : array lists to be tested
Input::arrGetsVoid(['name'=>'foo','age'=>'']); // returns true Input::arrGetsVoid(['name'=>'foo','age'=>'30']); // returns false
voidKey method returns the corresponding keys that have
an empty value in a previously tested array.
This method is useful when handling zip files.
Input::arrGetsVoid(['name'=>'foo', 'age'=>'']);
Input::voidKey(); // returns ['age']