Пример #1
0
 public function test_equal()
 {
     Vacuum::$validatingData = ['a' => 'qwerty', 'b' => 234, 'c' => true, 'd' => null, 'e' => '', 'f' => false];
     $this->setRuleArgument('a');
     $this->true('qwerty');
     $this->false('qwert');
     $this->false('qwertyy');
     $this->setRuleArgument('b');
     $this->false(233);
     $this->true(234);
     $this->false(234.0);
     $this->false(235);
     $this->setRuleArgument('c');
     $this->true(true);
     $this->false(false);
     $this->false('true');
     $this->setRuleArgument('d');
     $this->true(null);
     $this->false('null');
     $this->setRuleArgument('e');
     $this->true('');
     $this->false(null);
     $this->setRuleArgument('f');
     $this->false(null);
     $this->true(false);
     $this->false(true);
 }
Пример #2
0
 public function test_invalidSignature2()
 {
     $flag = Vacuum::is('qwerty', 'string:76');
     $this->assertTrue($flag);
 }
Пример #3
0
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 ...
}
Пример #4
0
 public function test_parseSyntax()
 {
     $this->assertTrue(Vacuum::is(19, 'req, min:  10  , max: 20  '));
     $this->assertTrue(Vacuum::is('qwe', 'req, in:[q, qw, qwe]'));
 }