PHP code example of meraki / schema

1. Go to this page and download the library: Download meraki/schema library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

meraki / schema example snippets


$schema = new Definition('signup');

$schema->add(
    $schema->createEmailAddressField('email'),
    $schema->createPasswordField('password')->minLengthOf(12),
    $schema->createNumberField('age')->minValueOf(18),
);

$result = $schema->validate((object) [
    'email' => '[email protected]',
    'password' => 'correct horse battery staple',
    'age' => '34',
]);

$result->anyFailed();   // false

use Meraki\Schema\Definition;

$schema = new Definition('contact_form');

$schema->add(
    $schema->createTextField('username')->minLengthOf(3)->maxLengthOf(20),
    $schema->createEmailAddressField('email'),
);

$username = $schema->createTextField('username');
$username->minLengthOf(3);                              // configures nothing
$username = $username->minLengthOf(3);                  // configures something

$schema->validate((object) [
    'price' => (object) ['currency' => 'AUD', 'amount' => '12.50'],   // a record
    'lines' => [$row, $row],                                          // a list
]);

$result = $schema->validate($data);

$result->anyFailed();               // any field failed
$result->allPassed();               // every field passed
$field = $result->forField('email');

$field->given;        // exactly what was submitted, unchanged
$field->value;        // what the field made of it — an EmailAddress\Value
$field->source;       // Submitted | Prefilled | Default | None
$field->shape;        // could this be read at all?
$field->status;       // Passed | Failed | Skipped | Pending

$field = $schema->validate((object) ['email' => 'not an address'])->forField('email');

$field->shape->wasUnreadable();                    // true
$field->forConstraint('maxLength')->status->name;  // 'Skipped'

$failed = $field->getFailedConstraints()->getFirst();

$failed->name;    // 'minLength'
$failed->bound;   // 3
$failed->part;    // 'postal_code', or null for the whole value

use Meraki\Schema\Message\Mf2\Mf2Provider;

$provider = Mf2Provider::fromPackage('meraki/schema-language-english');

$result = $schema->validate($data, locale: 'en-AU', messages: $provider);

$result->forField('billing')->messages->forPart('postal_code')->first;
// "That is not a valid postcode for the country you chose."

$schema->add(
    $schema->createAddressField('billing', ['AU'])->mustBeVisitable(),
    $schema->createMoneyField('price', ['AUD' => 2])->minAmountOf('AUD', '10.00'),
);

$schema = (new Definition('booking'))->for('AU');
$schema->createAddressField('billing');      // restricted to AU
$schema->createPhoneNumberField('mobile');   // ditto

$schema->addRule(
    $whoFor->when()->equals('someone_else')
        ->then($participantName->makeRequired())
        ->else($participantName->makeOptional()),
);

->then($terms->makeRequired()->mustBeAccepted())
->then($discount->maxValueOf(50))

$age->when()->isAtLeast(18);            // 18 passes
$age->when()->isGreaterThan(18);        // 18 does not
$age->when()->isBetween(18, 65);        // both ends 

use Meraki\Schema\ValueScope;

// is the shipping address the billing address?
$schema->when(ValueScope::of('shipping'))->equals(ValueScope::of('billing'));

// are they at least in the same country?
$schema->when(ValueScope::of('shipping', 'country'))
    ->equals(ValueScope::of('billing', 'country'));

$result->forField('participant_name')->wasAlteredByRule();   // true

$schema = new Definition('signup');    // built once, at boot
// ...
$alice = $schema->validate($a);    // two requests, in any order,
$mallory = $schema->validate($b);  // interleaved or not

$schema->validate($submitted, prefilledWith: $knownAboutThisUser);