PHP code example of mcvalidator / mcvalidator-php
1. Go to this page and download the library: Download mcvalidator/mcvalidator-php 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/ */
mcvalidator / mcvalidator-php example snippets
use McValidator as MV;
// Builder is important because we can chain validator with them without
// too much work.
$builder = MV\valid('rule/is-string');
// Build the pipe
$pipe = $builder->build();
// Pump the value through the pipe
// Result contains the value and also informations about
// the runtime, such as errors and messages
$result = $pipe->pump(10);
// Gets the runtime state
$state = $result->getState();
// Gets the message of the head(first item) of errors
echo $state->getErrors()->head()->getMessage(); // Value is not a string
// We need more!
$builder2 = MV\shape_of([
'a' => $builder
]);
$pipe2 = $builder2->build();
$result2 = $pipe2->pump(dict([
'a' => 10
]));
// Gets the runtime state
$state2 = $result2->getState();
// Gets the message of the head(first item) of errors
echo $state2->getErrors()->head()->getMessage(); // outputs `Value is not a string`
// Gets the field path of the error!
echo $state2->getErrors()->head()->getStringPath('/' // <-- separator); // outputs `$/a`
// `$` means root of path