public function testValidate() { $specs = array('name' => array('type' => 'string', 'max_length' => 2, 'max_length' => 30), 'birthdate' => array('type' => 'string', 'regex' => '/^\\d{4}-[01]\\d-[0-3]\\d$/', 'before' => function (&$value) { // expect dd/mm/yyyy if (is_string($value) && preg_match('#^([0-3]\\d)/([01]\\d)/(\\d{4})$#', $value, $matches)) { $value = $matches[3] . '-' . $matches[2] . '-' . $matches[1]; } }, 'optional' => true), 'score' => array('types' => array('float', 'integer'), 'max_value' => 10, 'min_value' => 0)); $validator = new Validate\Validator(array('allow_extra' => false, 'empty_delete' => false, 'empty_null' => true, 'remove_extra' => true, 'specs' => $specs)); $tests = array(array('input' => array('name' => 'Jane', 'birthdate' => '31/01/1984', 'score' => 7), 'expect' => array('name' => 'Jane', 'birthdate' => '1984-01-31', 'score' => 7), 'expect_exception' => null), array('input' => array('name' => 'Curt', 'birthdate' => '22/03/1983', 'score' => 9), 'expect' => array('name' => 'Curt', 'birthdate' => '1983-03-22', 'score' => 9), 'expect_exception' => null), array('input' => array('name' => 'Mike', 'birthdate' => '01/01/2000', 'score' => 'high'), 'expect' => null, 'expect_exception' => "Parameter 'score' failed the validation check 'types' for value string 'high'")); foreach ($tests as $i => $test) { $input = $test['input']; $expect = $test['expect']; $expect_exception = $test['expect_exception']; $got_exception = null; $validated_input = null; try { $validated_input = $validator->validate($input); } catch (Validate\ValidationException $e) { $got_exception = $e->getMessage(); } $this->assertEquals($expect, $validated_input, "Test {$i} validate() returns expected result."); $this->assertEquals($expect_exception, $got_exception, "Test {$i} throws the expected exception."); } }
* The 'after' function is executed after other spec validations have been performed. * * @author Craig Manley * @version $Id: validator_after.php,v 1.3 2016/06/13 20:04:08 cmanley Exp $ * @package Validate */ require_once __DIR__ . '/../src/Validator.php'; # Define the validation specs $specs = array('name' => array('type' => 'string', 'max_length' => 2, 'max_length' => 30), 'birthdate' => array('type' => 'string', 'regex' => '#^[0-3]\\d/[01]\\d/\\d{4}$#', 'after' => function (&$value) { // want yyyy-mm-dd if (is_string($value) && preg_match('#^(\\d{2})/(\\d{2})/(\\d{4})$#', $value, $matches)) { $value = $matches[3] . '-' . $matches[2] . '-' . $matches[1]; } }, 'optional' => true), 'score' => array('types' => array('float', 'integer'), 'max_value' => 10, 'min_value' => 0)); # Create a Validator $validator = new Validate\Validator(array('specs' => $specs)); # Define records to test $records = array(array('name' => 'Jane', 'birthdate' => '31/01/1984', 'score' => 7), array('name' => 'Curt', 'birthdate' => '22/03/1983', 'score' => 9)); # Validate the records $i = 0; foreach ($records as $record) { print 'Before validation: ' . print_r($record, true); try { $record = $validator->validate($record); print 'After validation: ' . print_r($record, true); } catch (Validate\ValidationException $e) { print 'Error at record ' . $i . ': ' . $e->getMessage() . "\n"; continue; } print "\n\n"; $i++;