<?php
// import from same directory as this file
require_once(dirname(__FILE__) . "/KVerifiableData.php");
/////////////////////////////////////////////////////////////////////
// DEMO
// This shows an example that specifies some data to be verified
// As is, array $info below contains 4 errors;
// update $info to fix these errors, or to introduce errors that should be 'caught'
// STEP 1: specify attributes of data to verify
$verifiableData =& new KVerifiableData();
$verifiableData->addRequiredItem('name', KVDATA_STRING);
$verifiableData->addOptionalItem('dob', KVDATA_DATE);
$verifiableData->addOptionalItem('isMale', KVDATA_BOOLEAN);
$verifiableData->addOptionalItem('date', KVDATA_DATE);
$verifiableData->addRequiredItem('numItems', KVDATA_INTEGER, array('moreThan'=>1));
$verifiableData->addRequiredItem('summary', KVDATA_STRING, array('minLength'=>5,'maxLength'=>9));
$verifiableData->addOptionalItem('price', KVDATA_FLOAT, array('minValue'=>24.5));
$verifiableData->addRequiredItem('countryId', KVDATA_INTEGER);
$verifiableData->addOptionalItem('times', KVDATA_TIME + KVDATA_ARRAY, array('minValue'=>1200,'minCollectionLength'=>3));
$verifiableData->addOptionalItem('prices', KVDATA_FLOAT + KVDATA_COMMA_LIST, array('minValue'=>3.5,'minCollectionLength'=>2,'maxCollectionLength'=>2));
$verifiableData->addDependency('dob', KVDATA_SAME_OR_COMES_BEFORE, 'date');
$verifiableData->addDependency('times', KVDATA_SIZE_IS_GREATER_THAN, 'prices');
$verifiableData->addDependency('prices', KVDATA_SIZE_IS_SAME_AS, 'numItems');
// STEP 2: specify data to be verified
// Alternatively, this data can come from elsewhere, e.g. $_GET
$info = array(
'name' => 'Templar' ,
'numItems' => 3 ,
'summary' => 'cars' ,
'isMale' => 'no' , // WARNING: will result in isMale=1 as isMale is boolean above
// 'countryId' => 19 ,
'price' => 2.34 ,
'dob' => "2/6/1989" ,
'date' => "1/6/1991" ,
'prices' => "23.3,86" ,
'times' => array(2349,"12:34","19.20") ,
);
// STEP 3: verify data
$verifiableData->loadValues($info);
$errorMessageBlocks = $verifiableData->verify();
if ($errorMessageBlocks !== null) {
// error(s) occurred - print them all
echo "Error(s):<pre>";
foreach ($errorMessageBlocks as $errorMessageBlock) {
echo $errorMessageBlock['message']."<br>";
}
print "</pre>";
} else {
// no errors found - in this case, show resulting values
printArrayToHtml($verifiableData->getValues());
}
function printArrayToHtml($arr)
{
print "<pre>";
print_r($arr);
print "</pre>";
}
?>
|