Exemple #1
0
 /**
  * try/finally is required if you want to support 'break' in a generator, but
  * unfortunately PHP 5.6 doesn't work properly as of 5.6.10:
  * https://bugs.php.net/bug.php?id=69740
  * @group faulty
  */
 function testChildCheckReplacementBreak()
 {
     $schema = new \Fulfil\Schema(['props' => ['foo' => new \Fulfil\Filter\String_(['trim' => true])]]);
     $replacement = new \Fulfil\Check\Basic();
     foreach ($schema->childChecks(!!'replace') as &$check) {
         $check = $replacement;
         break;
     }
     $this->assertEquals([$replacement], $schema->props['foo']);
 }
Exemple #2
0
 function testSchemaMapperPopulateWithUnknownProperty()
 {
     $class = ClassBuilder::i()->registerOne('
         class PropObj {
             public $foo = "a";
             public $bar = "b";
             public function populate($in) {
                 foreach ($in as $k=>$v) {
                     if (!isset($this->$k)) {
                         throw new \\Exception();
                     }
                     $this->$k = $v;
                 }
             }
         }
     ');
     $schema = new Schema(['props' => ['foo' => new Check\String_(), 'bar' => new Check\String_()], 'filterUnknownProps' => true]);
     $in = ['foo' => 'yep', 'bar' => 'yep', 'baz' => 'yep'];
     $out = $schema->apply($in, $ctx = new \Fulfil\Context());
     $flat = $ctx->flatten();
     $this->assertFalse($flat->valid);
     $this->assertCount(1, $flat->messages);
     $this->assertEquals(['schema', 'propUnknown'], $flat->messages[0]->id);
     $obj = new $class();
     $obj->populate($out);
 }
Exemple #3
0
<?php

require __DIR__ . '/config.php';
use Fulfil\Check;
use Fulfil\Filter;
use Fulfil\Rule;
use Fulfil\Schema;
$schema = new Schema(['props' => ['foo' => true, 'minDate' => [new Filter\DateFormat(['initial' => '2014-01-01', 'formats' => ['Y-m-d']]), new Check\Date(['required' => false, 'message' => ['text' => 'PANTS']])], 'maxDate' => new Check\Date(['required' => false]), 'gg' => [new Filter\String_(['trim' => true]), new Check\String_(['name' => 'Governor General', 'initial' => ' boom ', 'lengthMax' => 15, 'values' => ['boom', 'bang', 'biff']])], 'boom' => new Check\Any(['name' => 'boom', 'message' => ['text' => "IT DIDN'T WORK"], 'checks' => [new Schema(['props' => ['quack' => new Check\Basic(['required' => true])]]), new Check\String_(), new Check\Bool_(), new Check\Int_(['min' => 10, 'max' => 30])]]), 'pow' => new Check\Bool_(), 'biff' => new Check\Bool_(['name' => 'BIFF']), 'whack' => new Check\Bool_(['name' => 'WHACK']), 'hello' => new Check\Email(['initial' => 'a@b', 'required' => true])], 'rules' => [new Rule\DateRange(['dateMin' => 'minDate', 'dateMax' => 'maxDate'])]]);
$registry = new \Fulfil\Registry();
// var_dump($schema->groups());
// initial values
$ctx = new \Fulfil\Context();
$initial = $schema->initial();
var_dump($schema->apply($initial, $ctx));
$flat = $ctx->flatten();
foreach ($flat->messages as $error) {
    echo implode(".", $error->path) . ": " . $error->text . "\n";
}
// actual validation process
$in = ['foo' => 'yep', 'minDate' => new \DateTime('yesterday'), 'maxDate' => new \DateTime('now'), 'gg' => 'boom', 'boom' => ['quack' => 'yep'], 'pow' => false, 'biff' => false, 'whack' => true, 'hello' => 'a@b'];
$ctx = new \Fulfil\Context();
var_dump($schema->apply($in, $ctx));
$flat = $ctx->flatten();
foreach ($flat->messages as $error) {
    echo implode(".", $error->path) . ": " . $error->msg . "\n";
}
echo "\n";
echo "Schema can modify  : " . ($schema->canModify() ? 'yes' : 'no') . "\n";
echo "Input modified     : " . ($flat->change ? 'yes' : 'no') . "\n";
echo "Validation complete: " . ($flat->complete ? 'yes' : 'no') . "\n";