1. Go to this page and download the library: Download sgpinkus/jsonschema 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/ */
sonSchema\JsonSchema;
use JsonSchema\Constraint\Constraint;
use JsonSchema\Constraint\Exception\ConstraintParseException;
use JsonSchema\Constraint\ValidationError;
class ModuloConstraint extends Constraint
{
private $modulo;
private function __construct(int $modulo) {
$this->modulo = $modulo;
}
public static function getName() {
return 'modulo';
}
public function validate($doc, $context) {
if(is_int($doc) && $doc % $this->modulo !== 0) {
return new ValidationError($this, "$doc is not modulo {$this->modulo}", $context);
}
return true;
}
public static function build($context) {
if(!is_int($context->modulo)) {
throw new ConstraintParseException("The value of 'modulo' MUST be an integer.");
}
return new static($context->modulo);
}
}
$doc = 7;
$schema = '{
"type": "integer",
"modulo": 2
}';
$schema = new JsonSchema($schema, ['ModuloConstraint']);
$valid = $schema->validate($doc);
if($valid === true)
print "OK\n";
else
print $valid;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.