PHP code example of celestriode / constructure-json
1. Go to this page and download the library: Download celestriode/constructure-json 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/ */
celestriode / constructure-json example snippets
$constructure = new JsonConstructure(new EventHandler(), new TypesMatch());
$expected = Json::object()->addChild(null, Json::string());
$raw1 = '{"hello": "yes", "goodbye": "no}'; // Both "hello" and "goodbye" pass.
$raw2 = '{"hello", "yes", "goodbye", 3}'; // "hello" passes but "goodbye" fails.
$expected = Json::array()->addElements(Json::string(), Json::boolean());
$raw1 = '[true, true, "hello", false, "goodbye"]'; // All elements pass.
$raw2 = '[3, true, "hello", false]'; // Element at index 0 fails.
$branchA = new Branch("Branch A", Json::object()->addChild("with", Json::boolean()->->$branchA, $branchB)
$raw1 = '{"method": "first", "with": true}'; // Passes.
$raw2 = '{"method": "second", "next": false}'; // Passes.
$raw3 = '{"method": "third"}'; // Passes because there is no HasValue audit on the "method" string itself.
$raw4 = '{}'; // Passes because the "method" string is not
class CustomAudit extends AbstractJsonAudit
{
protected function auditJson(AbstractConstructure $constructure, AbstractJsonStructure $input, AbstractJsonStructure $expected): bool
{
...
}
public static function getName(): string
{
return "custom_audit";
}
}
protected function auditJson(AbstractConstructure $constructure, AbstractJsonStructure $input, AbstractJsonStructure $expected): bool
{
// Ensure the input and expected structures are both strings.
// Feedback from this can be covered by the TypesMatch audit.
if (!($input instanceof JsonString) || !($expected instanceof JsonString)) {
return false;
}
// Check if the length of the string is less than 10.
if (strlen($input->getValue()) < 10) {
// Trigger an event. Pass in the audit, the input, and expected structure.
// The event can do what it likes with these.
$constructure->getEventHandler()->trigger("some unique event name", $this, $input, $expected);
// Since the audit failed, return false.
return false;
}
// The string has a length of 10 or more, so the audit passes.
return true;
}
class CustomAudit extends NumberRange
{
const INVALID_VALUE = '1627ea1f-e25c-4f08-aa31-eeb0cf6bdfec';
protected function auditPrimitive(AbstractConstructure $constructure, AbstractJsonPrimitive $input, AbstractJsonPrimitive $expected): bool
{
// Get the value of the input as a string and get its length.
$length = strlen($input->getString());
// Check the length of the string against the min and max.
if ($this->withinRange($length)) {
// The length is correct, so the audit passes.
return true;
}
// The length is incorrect, so the audit fails.
$constructure->getEventHandler()->trigger(self::INVALID_VALUE, $this, $input, $expected);
return false;
}
...
}
$eventHandler = new EventHandler();
$eventHandler->addEvent(...);
$constructure = new JsonConstructure($eventHandler);
$constructure = new JsonConstructure(new EventHandler());
$constructure->getEventHandler()->addEvent(...);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.