1. Go to this page and download the library: Download prewk/snapper-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/ */
prewk / snapper-php example snippets
use Prewk\Snapper;
// Define a recipe defining the fields and their references
$r = new Snapper\Recipe;
$recipe = [
"parents" => $r
->primary("id") // Primary key at field "id"
->ingredients([
"name" => $r->value(), // Field "name" is just a value
// Field "favorite_child" has a circular dependency to the "children" table
"favorite_child" => $r->circular(
// Define the relationship and values considered "no relationship"
$r->ref("children")->optional(null),
// Fallback to that value until the circular relationship can be resolved
$r->raw(null)
),
]),
"children" => $r
->primary("id") // Primary key at field "id"
->ingredients([
"parent_id" => $r->ref("parents"), // Field "parent_id" is referencing the "parents" table
"description" => $r->value() // Field "description" is just a value
])
];
// Create a serializer
$serializer = new Snapper\Serializer(
new Snapper\Sorter,
new Snapper\Serializer\SerializationBookKeeper,
$recipe
);
// Feed the serializer with database rows
$serializer->add("parents", [
"id" => 1,
"name" => "The parent",
"favorite_child" => 2
]);
$serializer->add("children", [
"id" => 1,
"parent_id" => 1,
"description" => "I'm child A"
]);
$serializer->add("children", [
"id" => 2,
"parent_id" => 1,
"description" => "I'm child B"
]);
// Serialize into a snapshot
$serialization = $serializer->compile()->getOps();
use Prewk\Snapper;
// $recipe = <Same as above>
// $serialization = <The snapshot>
// $dbh = <PDO handle>
// Create inserters
$inserters = [
"parents" => function(array $rows) use ($dbh) {
$ids = [];
foreach ($rows as $row) {
$stmt = $dbh->prepare("INSERT INTO parents (name, favorite_child) VALUES (:name, :favorite_child)");
$stmt->execute([
":name" => $row["name"],
":favorite_child" => $row["favorite_child"]
]);
$ids[] = $dbh->lastInsertId();
}
return $ids;
},
"children" => function(array $rows) use ($dbh) {
$ids = [];
foreach ($rows as $row) {
$stmt = $dbh->prepare("INSERT INTO children (parent_id, description) VALUES (:parent_id, :description)");
$stmt->execute([
":parent_id" => $row["parent_id"],
":description" => $row["description"]
]);
$ids[] = $dbh->lastInsertId();
}
return $ids;
}
];
// Create updaters
$updaters = [
"parents" => function(array $rows) use ($dbh) {
foreach ($rows as $row) {
$stmt = $dbh->prepare("UPDATE parents SET favorite_child=:favorite_child WHERE id=:id");
$stmt->execute([
":id" => $row["id"],
":favorite_child" => $row["favorite_child"]
]);
}
},
"children" => null, // Won't be called in this example
];
// Create a deserializer
$deserializer = new Snapper\Deserializer(
new Snapper\DeserializationBookKeeper,
$recipes,
$inserters,
$updaters
);
// Deserialize
$deserializer->deserialize($serialization);
use \Prewk\Snapper\Ingredients\Json;
[
"data" => $recipe->json(function(Json\JsonRecipe $json) {
return $json
// Match { "foo": { "bar": { "baz": <value> } } }
->path("foo.bar.baz", function(Json\MatchedJson $matched) {
return $matched
->ref("bazes")->optional(null, 0); // Treat null and 0 as value instead of reference
})
// Match { "quxes": [<value>, <value>, <value>, <value>] }
->pattern("/quxes\\.\\d+$/", function(Json\MatchedJson $matched) {
return $matched
->ref("quxes");
})
// Match { "content": <value> }
->path("content", function(Json\MatchedJson $matched) {
return $matched
// Match { "content": "Lorem ipsum qux:=123= dolor qux:=456= amet" }
->pattern("qux:=(.*?)=", function(
Json\PatternReplacer $replacer,
string $replacement
) {
// Here we tell the recipe about what references we found and
// teach it to search and replace them
return $replacer->replace(
"quxes",
1, // Refers to the index of the resulting preg, so: $matches[1]
"qux:=$replacement="
);
});
});
})
]
$deserializer->onDeps("foos", function(string $dependeeType, $dependeeId, $dependencyId) {
// Every time a dependency of type "foos" has been deserialized, this closure will be called
});
use Prewk\Snapper;
$validator = new Snapper\Validator(new DeserializationBookKeeper, $recipes);
$isValid = $validator->validate($serialization);
use Prewk\Snapper;
use JsonSchema\Validator as JsonValidator; // https://github.com/justinrainbow/json-schema
$validator = new Snapper\SchemaValidator(new JsonValidator);
$json = file_get_contents("recipe.json");
// Note: don't decode to associative array
$validator->validate(json_decode($json));