コード例 #1
0
ファイル: readforms.php プロジェクト: shabbyrobe/fulfil
<?php

$usage = <<<DOCOPT
Usage: readforms [options] <file> [<query>]

Options:
  --php          Handle PHP brackets, i.e. `name="foo[bar]"`
  --fmt=<fmt>    Output format. json or php. [default: json]
  --no-initials  Ignore initial values

DOCOPT;
$options = (new \Docopt\Handler())->handle($usage);
$fmt = $options['--fmt'];
$registry = new \Fulfil\Registry();
$reader = new \Fulfil\Ext\FormReader($registry);
if ($options['--php']) {
    $reader->handlePHPBrackets = true;
    $reader->ignoreInitials = $options['--no-initials'];
}
if ($options['<file>'] == '-') {
    $options['<file>'] = 'php://stdin';
}
$forms = $reader->readHTML(file_get_contents($options['<file>']));
foreach ($forms as $form) {
    $schema = $form->schema->export();
    // move it to the bottom
    unset($form->schema);
    $form->schema = $schema;
}
if ($fmt == 'json') {
    echo json_encode($forms, JSON_PRETTY_PRINT) . "\n";
コード例 #2
0
ファイル: formreader.php プロジェクト: shabbyrobe/fulfil
<?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";
    }