PHP code example of grithin / php-conform

1. Go to this page and download the library: Download grithin/php-conform library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

grithin / php-conform example snippets


# Simplest example
$Conform = new Conform(['age'=>' 10 ']);
if($conformed = $Conform->get(['age'=>'f.int'])){
	# in converting age to in, Conform has stripped the spaces
	echo $conformed['age']; # => 10
}


# You can set the input either at construction or after
$Conform = new Conform(['name'=>'bob']);
$Conform->input(['name'=>'sue']);

# if you do not provide an input on the construction (input value is false), Conform will use $_GET and $_POST as defaults, with $_POST overwriting colliding $_GET keys
$_POST = ['name'=>'bob'];
$Conform = new Conform();
$Conform->input(); # > ['name'=>'bob']

# if you want to set the input to the default input after construction, you can use the `request_input` method
$Conform->input(Conform::request_input());

# the request_input has some additional logic about pulling input from the request that 

$Conform = new Conform(['age'=>'bob']);
$rules = ['age'=>'f.digits, v.int'];

if($conformed = $Conform->get($rules)){
	# ...
}else{
	echo 'did not validate';
}
# > did not validate

$rules = ['age'=>'g.strtoupper'];

$Conform->conformer_add($string_identity, $pathed_callable);

# a closure
$Conform->conformer_add('upper', function($x){ return strtoupper($x);});
$rules = ['name'=>'upper'];

# an object with methods
$Conform->conformer_add('f', new \Grithin\Conform\Filter);
$rules = ['id'=>'f.int'];

# a function reference
$Conform->conformer_add('upper', 'strtoupper');
$rules = ['name'=>'upper']; # note, this will error b/c strtoupper does not expect 2 parameters ($Conform instance is passed as the last parameter)

$rules = ['id' => `!v.int, db.check`];

$rules = [
	'id' => `!!v.int, !!db.check`,
	'name' => 'db.matches_id'
];

$rules = [
	'email' => `?!v.filled, v.email`,
];

$rules = [
	'email' => `v.filled, v.email, &db.check`,
];

$rules = [
	'id' => `!v.int, db.check`,
	'name' => 'v.name, &&db.matches_id'
];

$rules = [
	'email' => `~db.email_exists`,
];

$rules = ['age'=>'!v.range|18;60'];

# array seperated rules
$rules = ['age'=>['!v.int', '!v.range|18;60']];
# array separated parameters
$rules = ['age'=>[['!v.range', 18, 60]]];
# array separated callable
$rules = ['age'=>[[['!', 'v.range'], 18, 60]]];

$rules = [
	'age'=>[
		[['!', function(){}], 18, 60]]
	];

$input = ['name' => 'bob', 'age' => 2];
$Conform = new Conform($input);
$conformed = $Conform->get(['name'=>'v.int', 'age'=>'v.int']);
# > false
$Conform->output;
# > ['age'=>2]


$input = ['year' => 'bob', 'age' => 2, 'number'=>'bill'];
$Conform = new Conform($input);
$conformed = $Conform->get([
	'year'=>'v.int',
	'age'=>'v.int',
	'number'=>'v.int'
]);

foreach($Conform->errors() as $error){
	var_export((array)$error);
}

/*
array (
  'message' => 'v.int',
  'fields' =>
  array (
    0 => 'year',
  ),
  'type' => 'v.int',
)array (
  'message' => 'v.int',
  'fields' =>
  array (
    0 => 'number',
  ),
  'type' => 'v.int',
)
*/

//...
# get all the errors for the field "year"
$Conform->field_errors('year');
# get all the errors for the fields "year" and "number"
$Conform->fields_errors(['year', 'number']);

$conformer = function($v, $Conform){
	$Conform->error('Error Message');
	return $v;
};
$Conform->conformer_add('conformer', $conformer);

$conformer = function($v, $Conform){
	$error = [
		'message' => 'Error Message',
		'type' => 'custom_type',
		'fields' => ['name', 'age']
	];
	$Conform->error($error);
	return $v;
};
$Conform->conformer_add('conformer', $conformer);

$auth = function($v, $Conform){
	$query = ['id'=>$Conform->output['id'], 'password'=>$conform->input['password']]
	if(!Db::check($query)){
		$Conform->error('Password and id did not match');
	}
	return $v;
};

Filter::init()->url($url)
Validate::init()->test('url', $url);