PHP Classes

File: examples/example_array_script.php

Recommend this page to a friend!
  Classes of Chara Miteo   ApPHP Data Validator   examples/example_array_script.php   Download  
File: examples/example_array_script.php
Role: Example script
Content type: text/plain
Description: Example file
Class: ApPHP Data Validator
Validate sets of values in different ways
Author: By
Last change:
Date: 12 years ago
Size: 4,747 bytes
 

Contents

Class file image Download
<?php
require_once '../validator.class.php';
$validator = new Validator();

$dataArray = array(
   
'normal' => 5,
   
'less_then_min' => 1,
   
'null_can_be_null' => 0,
   
'float_when_int_needed' => 7.1,
   
'forth' => 0
);
$dataObj = new ValidatorDataContainer($dataArray);

//int 5 normal
$validatorType = new ValidatorTypeNumeric($dataObj, 'normal', ValidatorTypeNumeric::$subtypeInt);
$validator->AddType($validatorType);

// int less then minimum
$validatorType = new ValidatorTypeNumeric($dataObj, 'less_then_min', ValidatorTypeNumeric::$subtypeInt);
$validatorType->SetMin(16);
$validatorType->SetMax(90);
$validator->AddType($validatorType);

// int 0 and can be null with min
$validatorType = new ValidatorTypeNumeric($dataObj, 'null_can_be_null', ValidatorTypeNumeric::$subtypeInt);
$validatorType->SetCanBeNullFlag(1);
$validatorType->SetMin(5);
$validator->AddType($validatorType);

//float when int is needed
$validatorType = new ValidatorTypeNumeric($dataObj, 'float_when_int_needed', ValidatorTypeNumeric::$subtypeInt);
$validatorType->SetCanBeNullFlag(0);
$validatorType->SetMin(6);
$validator->AddType($validatorType);

//int 0 when can not be null
$validatorType = new ValidatorTypeNumeric($dataObj, 'forth', ValidatorTypeNumeric::$subtypeInt);
$validatorType->SetCanBeNullFlag(0);
$validator->AddType($validatorType);

//STRING
$dataArray = array(
   
'noraml_string' => 'a',
   
'smaller_then_min' => 'be',
   
'alphabetic_with_digits' => 'Henry8',
   
'no_spaces_with_pointing' => 'Jane:|',
   
'thirteenth' => 'Are you coming?',
   
'with_pointing_on_no_pointing_setting' => 'Come2me!',

);

$dataObj = new ValidatorDataContainer($dataArray);

// Normal string
$validatorType = new ValidatorTypeString($dataObj, 'noraml_string', ValidatorTypeString::$subtypeAlphabetic);
$validatorType->SetCanBeNullFlag(0);
$validator->AddType($validatorType);

// string smaller then minLen
$validatorType = new ValidatorTypeString($dataObj, 'smaller_then_min', ValidatorTypeString::$subtypeAlphabetic);
$validatorType->SetCanBeNullFlag(0);
$validatorType->SetMinLen(4);
$validator->AddType($validatorType);

// string alphabetic but with digits
$validatorType = new ValidatorTypeString($dataObj, 'alphabetic_with_digits', ValidatorTypeString::$subtypeAlphabetic);
$validatorType->SetCanBeNullFlag(0);
$validatorType->SetMinLen(4);
$validatorType->SetMaxLen(12);
$validatorType->SetPointingAllowedFlag(0);
$validator->AddType($validatorType);

// string alphabetic without spaces and with pointing on no-spaces setting
$validatorType = new ValidatorTypeString($dataObj, 'no_spaces_with_pointing', ValidatorTypeString::$subtypeAlphabetic);
$validatorType->SetCanBeNullFlag(0);
$validatorType->SetMinLen(4);
$validatorType->SetMaxLen(12);
$validatorType->SetSpacesAllowedFlag(0);
$validatorType->SetPointingAllowedFlag(1);
$validator->AddType($validatorType);

// string alphanumeric without spaces and with pointing on no spaces + no pointing setting
$validatorType = new ValidatorTypeString($dataObj, 'with_pointing_on_no_pointing_setting', ValidatorTypeString::$subtypeAlphanumeric);
$validatorType->SetCanBeNullFlag(0);
$validatorType->SetMinLen(4);
$validatorType->SetMaxLen(20);
$validatorType->SetSpacesAllowedFlag(0);
$validatorType->SetPointingAllowedFlag(0);
$validator->AddType($validatorType);

//EMAIL
$data = array(
   
'normal' => 'myemail@google.com',
   
'with_wrong_symbol' => 'my!email@google.com',
);

$dataObj = new ValidatorDataContainer($data);

// normal email
$validatorType = new ValidatorTypeEmail($dataObj, 'normal');
$validator->AddType($validatorType);

// wrong symbols
$validatorType = new ValidatorTypeEmail($dataObj, 'with_wrong_symbol');
$validator->AddType($validatorType);

//URL
$data = array(
   
'normal' => 'www.google.com',
   
'with_properties' => 'https://accounts.google
    .com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2'
,
   
'with_wrong_symbol' => 'www.g!ogle.com',
);

$dataObj = new ValidatorDataContainer($data);
// normal
$validatorType = new ValidatorTypeUrl($dataObj, 'normal');
$validator->AddType($validatorType);

// url with properties
$validatorType = new ValidatorTypeUrl($dataObj, 'with_properties');
$validator->AddType($validatorType);

// contains wrong symbol
$validatorType = new ValidatorTypeUrl($dataObj, 'with_wrong_symbol');
$validator->AddType($validatorType);
$validator->Validate();

if(
$validator->GetHasErrorStatus()){
   
$errors = $validator->GetErrorArray();
    foreach (
$errors as $error){
        echo
'- '.$error->ToString().'<br>';
    }
}