Input

The input class is a special tool that helps to validate input. It is mostly used when validating form inputs. All validation are directly processed and returned through the set method. The following are methods available in the input class.

  1. set
  2. strict
  3. default_type
  4. default_length
  5. default_range
  6. arrGetsVoid
  7. voidKey
initializing class
The input class can be easily initialized as shown below.

Sample: Initializing Input
  $input  = new Input;
                                    


set
The 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.

Syntax
  $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.                            
    
                                        
We shall be looking at a series of examples below.
Example: validating strings
  $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                              
                                
The following are list of available options and their descriptions:

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.

The following examples best describe how these options can be applied for validation
Example: Input Types
  $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
The strict() method is a directive that prevents the input class from proceeding with subsequent validations if an error is encountered in previous validations

Syntax: strict validation
  $input->strict(bool);
    
    where:
    
      bool: set the strict type to true or false. Default is true.
                                
Example: strict validation
  $input->strict();

  $input->set('foo', ['type'=>'number']);  // returns null
    
  $input->set('foo', ['type'=>'text']);  // returns null because a previous validation returned null                                  
                                
The 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.

default_type
Sets the default type of inputs to be validated. This can be overidden by setting options.

Syntax
  $input->default_type($type);  // set base path
     
    where :

      $type: type of validation
    
                    
Example
  $input->default_type('text');  // set default type

  $input->set('foo'); // returns foo
  $input->set('foo123'); // returns null
                                        

default_length
Sets the default length of inputs to be validated. This can be overidden by setting options.

Syntax
  $input->default_length(length);  // set base path
     
    where :

      length: array or string of acceptable lengths
    
                                        
Example
  $input->default_length(5);  // set default length
    
  $input->set('foo12'); // returns foo12
  $input->set('foobar123'); // returns null
                                        

default_range
Sets the default ranges for inputs to be validated. This can be overidden by setting options.

Syntax
  $input->default_range($range);  // set default range
     
    where :

      $range: array of acceptable ranges
    
                                        
Example
  $input->default_range(['ball', 'cat', 'dog']);  // set default range
    
  $input->test('cat'); // returns cat
  $input->test('bird'); // returns null
                                        

arrGetsVoid
The arrGetsVoid checks if a supplied array has any index key having an empty value within it.

Syntax
  Input::arrGetsVoid($array); 
     
    where:
        
       $array : array lists to be tested
    
                                        
Example
  Input::arrGetsVoid(['name'=>'foo','age'=>'']); // returns true
  Input::arrGetsVoid(['name'=>'foo','age'=>'30']); // returns false
                                        

voidKey
The voidKey method returns the corresponding keys that have an empty value in a previously tested array. This method is useful when handling zip files.

Example: fetch last directory
  Input::arrGetsVoid(['name'=>'foo', 'age'=>'']);
  Input::voidKey();  // returns ['age']