static function import(Registry $registry, $in, $allowIncomplete = false) { props: if (isset($in['props'])) { foreach ($in['props'] as &$item) { if ($item !== true) { if (isset($item['check']) || $item instanceof \Fulfil\CheckInterface) { $item = [$item]; } if (is_array($item)) { $new = []; foreach ($item as $k => $check) { if ($check instanceof \Fulfil\CheckInterface) { $new[$k] = $check; } else { $new[$k] = $registry->import('check', $check, $allowIncomplete); } } $item = $new; } } } } rules: if (isset($in['rules'])) { foreach ($in['rules'] as &$item) { $item = $registry->import('rule', $item, $allowIncomplete); } } return parent::import($registry, $in, $allowIncomplete); }
static function import(Registry $registry, $in, $allowIncomplete = false) { if (isset($in['checks'])) { foreach ($in['checks'] as &$item) { if (!$item instanceof \Fulfil\CheckInterface) { $item = $registry->import('check', $item, $allowIncomplete); } } } return parent::import($registry, $in, $allowIncomplete); }
function testUnknownCheckImportFailsWhenIncompleteDisallowed() { $registry = Registry::standard(); $registry->check = []; $this->setExpectedException(\InvalidArgumentException::class, 'Unknown check'); $registry->import('check', ['check' => 'whabadoo']); }
function testImportExport() { $in = ['check' => 'all', 'checks' => [['check' => 'basic'], ['check' => 'basic']]]; $check = Registry::standard()->import('check', $in); $expected = new Check\All(['checks' => [new Check\Basic(), new Check\Basic()]]); $this->assertEquals($expected, $check); $this->assertEquals($in, $check->export()); }
function testMapFormSchema() { $registry = \Fulfil\Registry::standard(); $check = new \Fulfil\Ext\FormSchema(['schemas' => new \Fulfil\Schema(['props' => ['foo' => new \Fulfil\Schema(['props' => ['bar' => true]])]], ['mapper' => new \Fulfil\Mapper\Vars()])], ['registry' => $registry]); $in = (object) ['foo' => (object) ['bar' => 'yep']]; $out = $check->map($in); $this->assertEquals(['foo' => ['bar' => 'yep']], $out); }
function __construct(\Fulfil\Registry $registry = null) { if ($registry) { $this->registry = $registry; } else { $this->registry = \Fulfil\Registry::standard(); $this->registry->specTest = ['test' => TestInfo::class, 'case' => TestCaseInfo::class]; $this->registry->check['decimal'] = ['class' => \Fulfil\Check\Decimal::class, 'config' => ['emitString' => true]]; } }
static function import(Registry $registry, $in, $allowIncomplete = false) { schemas: if (isset($in['schemas'])) { $schemas =& $in['schemas']; if (isset($schemas['check'])) { $schemas = [$schemas]; } foreach ($schemas as &$item) { $item = $registry->import('check', $item, $allowIncomplete); } } rules: if (isset($in['rules'])) { foreach ($in['rules'] as &$item) { $item = $registry->import('rule', $item, $allowIncomplete); } } return parent::import($registry, $in, $allowIncomplete); }
static function import(\Fulfil\Registry $registry, $in, $allowIncomplete = false) { $in['specTest'] = true; if (isset($in['test']['check'])) { $in['test'] = [$in['test']]; } if (isset($in['test'])) { foreach ($in['test'] as &$test) { if (!isset($test['check'])) { throw new \Exception(); } $test = $registry->import('check', $test, $allowIncomplete); } unset($test); } if (isset($in['data'])) { foreach ($in['data'] as $k => $i) { $i['specTest'] = 'case'; $i = $registry->import('specTest', $i, $allowIncomplete); $in['data'][$k] = $i; } } return parent::import($registry, $in, $allowIncomplete); }
function testImportWithClosureDeps() { $registry = Registry::standard(); $registry->check['test'] = ['class' => TestDepCheck::class, 'deps' => ['a' => 'a', 'b' => 'b']]; $registry->deps['a'] = function ($inReg) use($registry) { $this->assertSame($registry, $inReg); return 'foo'; }; $registry->deps['b'] = function () { return 'bar'; }; $out = $registry->import('check', ['check' => 'test']); $this->assertInstanceOf(TestDepCheck::class, $out); $this->assertEquals('foo', $out->a); $this->assertEquals('bar', $out->b); }
function main() { global $argv; $registry = \Fulfil\Registry::standard(); $pdo = new \PDO(getenv('JUNK_DB_DSN'), getenv('JUNK_DB_USER'), getenv('JUNK_DB_PASS'), [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]); $reader = new \Fulfil\Ext\MySqlReader($registry, $pdo); $schema = $reader->tableSchema($argv[1], $argv[2]); var_dump($schema); $ctx = new \Fulfil\Context(); $in = ['foo' => 'a', 'bar' => '2', 'baz' => ['zz', 'yy']]; var_dump($schema->apply($in, $ctx)); $msg = new \Fulfil\MessageSet\Developer(); foreach ($msg->formatContext($ctx) as $text) { echo "{$text}\n"; } }
function testAnnotatedClass() { $class = ClassBuilder::i()->registerOne(' /** * :fulfil = {"rules": [{"rule": "equal", "left": "foo", "right": "bar"}]}; */ class Pants { /** :fulfil = [{"check": "string", "required": true}]; */ public $foo; /** :fulfil = [{"check": "string", "required": true}]; */ public $bar; } '); $registry = \Fulfil\Registry::standard(); $anno = new AnnotatedClass(['class' => $class], ['registry' => $registry]); $schema = $anno->getSchema(); $this->assertCount(1, $schema->props['foo']); $this->assertInstanceOf(\Fulfil\Check\String_::class, $schema->props['foo'][0]); $this->assertCount(1, $schema->props['bar']); $this->assertInstanceOf(\Fulfil\Check\String_::class, $schema->props['bar'][0]); $this->assertCount(1, $schema->rules); $this->assertInstanceOf(\Fulfil\Rule\Equal::class, $schema->rules[0]); }
$schema = $options['<schema>']; $data = $options['<data>']; read: if ($schema == '-' || $data == '-') { $in = file_get_contents('php://stdin'); if ($schema == '-' && $data == '-') { $schema = $in['schema']; $data = $in['data']; } elseif ($schema == '-') { $schema = $in; } elseif ($data == '-') { $data = $in; } } apply: $registry = \Fulfil\Registry::standard(); $schema = json_decode($schema, !!'assoc'); if ($schema === null && ($err = json_last_error())) { die("Invalid schema\n"); } $data = json_decode($data, !!'assoc'); if ($data === null && ($err = json_last_error())) { die("Invalid data\n"); } $schema = $registry->import('check', $schema); $ctx = new \Fulfil\Context(); $data = $schema->apply($data, $ctx); $flat = $ctx->flatten(); if ($flat->valid) { echo "OK\n"; } else {
private function createFormSchema($check, $config = []) { $fs = new FormSchema(['schemas' => new \Fulfil\Schema(['props' => ['foo' => $check]])] + $config, ['registry' => \Fulfil\Registry::standard()]); return $fs; }
function setUp() { $this->registry = Registry::standard(); $this->reader = new FormReader($this->registry); }
function testUnknownRuleImportFailsWhenIncompleteDisallowed() { $registry = new Registry(); $this->setExpectedException(\InvalidArgumentException::class, 'Unknown rule'); $registry->import('rule', ['rule' => 'whabadoo']); }
function testDeepClone() { $schema = new \Fulfil\Schema(['props' => ['a' => new \Fulfil\Check\List_(['defaultItem' => new \Fulfil\Check\String_()])]]); $registry = \Fulfil\Registry::standard(); $cloned = $registry->deepClone($schema); $this->assertEquals($schema, $cloned); $this->assertNotSame($schema, $cloned); $this->assertNotSame($schema->props['a'][0], $cloned->props['a'][0]); $this->assertNotSame($schema->props['a'][0]->defaultItem, $cloned->props['a'][0]->defaultItem); }
<?php require __DIR__ . '/config.php'; goto defs; script: $fr = new Fulfil\Ext\FormReader(\Fulfil\Registry::standard()); $fr->handlePHPBrackets = true; $forms = $fr->readHTML(form()); if (isset($_SERVER['HTTP_USER_AGENT'])) { $schema = $forms[0]->schema; $values = $_POST ?: $schema->initial(); if ($_POST) { $ctx = new Fulfil\Context(); $values = $schema->apply($values, $ctx); $flat = $ctx->flatten(); if (!$flat->valid) { echo '<ul>'; foreach ($flat->messages as $message) { echo '<li>'; echo implode('.', $message->path) . ': '; echo $message->msg . "\n"; } echo '</ul>'; } } echo "<pre>" . json_encode($values, JSON_PRETTY_PRINT) . "</pre>"; echo form($values); echo "<pre>"; foreach ($forms as $form) { echo json_encode($form->schema->export(), JSON_PRETTY_PRINT) . "\n"; }