예제 #1
0
파일: Examples.php 프로젝트: comm1x/vacuum
function __autoload($class)
{
    require_once 'src/' . str_replace('\\', '/', $class) . '.php';
}
// Example 1: Simple rules
// Source data
$account = ['example' => '#1: Simple validation, this field will be geted', 'name' => 'Sherlock', 'surname' => 'Holmes', 'age' => 29, 'car' => null];
$validData = Vacuum::get($account, ['name' => 'req', 'surname' => 'req', 'age' => 'req, integer', 'car' => 'asis']);
var_dump($validData);
// Example 2: Rules with arguments
$account = ['login' => '*****@*****.**', 'password' => 'qwerty1234567890', 'name' => 'Sherlock', 'country' => 'Russia', 'age' => 24];
$validData = Vacuum::get($account, ['login' => 'req, email', 'password' => 'req, minlen:15', 'name' => 'req, maxlen:15', 'country' => 'req, in:[Russia, China, USA, Japan]', 'age' => 'req, min:18, max:100']);
var_dump($validData);
// Example 3: Composite key
$account = ['name' => ['first' => 'Sherlock', 'last' => 'Holmes'], 'address' => ['country' => 'England', 'city' => ['name' => 'London', 'code' => 815], 'street' => ['Baker Street', 221, 'b']], 'color' => ['R' => 188, 'G' => 16, 'B' => 154]];
$validData = Vacuum::get($account, ['name.first' => 'req', 'name.last' => 'req', 'address.country' => 'req, in:[Russia, England]', 'address.city.name' => 'req, in:[London, Moscow]', 'address.city.code' => 'integer', 'address.street.0' => 'req', 'address.street.1' => ['req', 'in' => [220, 221, 222]], 'address.street.2' => 'req, in:[a, b, 220, 221]', 'color' => 'containall:[R, G, B], each:[req, min:0, max:255]']);
var_dump($validData);
// Example 4: Custom message
$account = ['login' => '*****@*****.**', 'password' => 'qwerty1234567890', 'name' => 'Sherlock', 'country' => 'Russia', 'age' => 19];
$validData = Vacuum::get($account, ['login' => 'req, email, msg:Invalid email: {value}', 'age' => ['min' => 18, 'max' => 24, 'msg' => function ($error) {
    if ($error['rule'] == 'min') {
        return 'Sorry bro, less than ' . $error['arg'] . 'Your: {value}';
    }
    return 'Build your logic using templates: {rule}, {arg}, {value}, and its value';
}], 'country' => ['req', 'msg' => 'Or you can use template here: {rule}, {arg}']]);
var_dump($validData);
// Example 5: Condition use case
$age = 27;
if (Vacuum::isValidVar($age, 'req, min:18, max:30')) {
    // do something ...
}
예제 #2
0
 public function test_setMessageAsArray()
 {
     try {
         Vacuum::get(['login' => 'qwe'], ['login' => ['req', 'minlen' => 5, 'msg' => ['req' => 'Login is required', 'minlen' => 'Login is short']]]);
         $this->fail('Exception was not thrown');
     } catch (\vacuum\exception\ValidationException $e) {
         $this->assertEquals($e->getMessage(), 'Login is short');
     }
     try {
         Vacuum::get(['login' => 'qwe'], ['login' => ['req', 'minlen' => 5, 'msg' => ['value', 'qwe' => 'No qwe login', 'rty' => 'No rty login']]]);
         $this->fail('Exception was not thrown');
     } catch (\vacuum\exception\ValidationException $e) {
         $this->assertEquals($e->getMessage(), 'No qwe login');
     }
     try {
         Vacuum::get(['login' => 'qwe'], ['login' => 'req, minlen:5, msg:[value, qwe:Login is short]']);
         $this->fail('Exception was not thrown');
     } catch (\vacuum\exception\ValidationException $e) {
         $this->assertEquals($e->getMessage(), 'Login is short');
     }
     try {
         Vacuum::get(['login' => 'qwe'], ['login' => 'req, minlen:5, msg:[value]']);
         $this->fail('Exception was not thrown');
     } catch (\vacuum\exception\ValidationException $e) {
         $msg = strtr(Vacuum::$config['default-message'], ['{field}' => 'login', '{rule}' => 'minlen']);
         $this->assertEquals($e->getMessage(), $msg);
     }
     Vacuum::get(['login' => 'qwerty'], ['login' => 'req, minlen:5, msg:[value]']);
 }