1. Go to this page and download the library: Download hi-folks/data-block 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/ */
$appleData = $data->getBlock("apple")
// $data is the Block instance so that you can access
// to the Block methods like count()
$data->getBlock("apple")->count();
$articleText = "Some words as a sample sentence";
$textField = Block::make();
$textField->set("type", "doc");
$textField->set("content.0.content.0.text", $articleText);
$textField->set("content.0.content.0.type", "text");
$textField->set("content.0.type", "paragraph");
$data = Block::make($fruitsArray);
$keys = $data->getBlock("avocado")->keys();
/*
Array
(
[0] => name
[1] => fruit
[2] => wikipedia
[3] => color
[4] => rating
)
*/
$file = "./composer.json";
$composerContent = Block::fromJsonFile($file);
// here you can manage $composerContent with Block methods
// end then exports the Block data into a native array
$array = $composerContent->toArray();
$file = "./.github/workflows/run-tests.yml";
$workflow = Block::fromYamlFile($file);
echo $workflow->get("name"); // Name of the GitHub Action Workflow
echo $workflow->get("jobs.test.runs-on");
echo $workflow->get("on.0"); // push , the first event
$data1 = Block::make(["a", "b"]);
$arrayData2 = ["c", "d"];
$data1->appendItem($arrayData2);
$data1->count(); // 3 because a, b, and the whole array c,d as single element
$data1->toArray();
/*
[
'a',
'b',
[
'c',
'd',
],
]
*/
$table = Block::make($dataTable, false);
$data = $table->select('product', 'price')->where('price', ">", 100, false);
foreach ($data as $key => $item) {
print_r($item); // $item is an array
}
$table = Block::make($dataTable);
foreach ($table as $key => $item) {
expect($item)->toBeInstanceOf(Block::class);
expect($key)->toBeInt();
expect($item->get("price"))->toBeGreaterThan(10);
}
// iterateBlock(false if you need array instad of a nested Block)
foreach ($table->iterateBlock(false) as $key => $item) {
expect($item)->toBeArray();
expect($key)->toBeInt();
expect($item["price"])->toBeGreaterThan(10);
}
$url = "https://dummyjson.com/posts";
$posts = Block::fromJsonUrl($url) // Load the Block from the remote URL
->getBlock("posts") // get the `posts` as Block object
->where(
field:"tags",
operator: "in",
value: "love",
preseveKeys: false,
) // filter the posts, selecting only the posts with tags "love"
->forEach(fn($element): array => [
"title" => strtoupper((string) $element->get("title")),
"tags" => count($element->get("tags")),
]);
// The `$posts` object is an instance of the `Block` class.
// The `$posts` object contains the items that matches the `where` method.
// You can access to the elements via the nested keys
// $posts->get("0.title"); // "HOPES AND DREAMS WERE DASHED THAT DAY."
// $posts->get("0.tags"); // 3
$file = "./.github/workflows/run-tests.yml";
$workflow = Block::fromYamlFile($file);
$workflow->validateJsonViaUrl(
'https://json.schemastore.org/github-workflow'
); // TRUE if the Block is a valid GitHub Actions Workflow
// load the schema as Block object...
$schemaBlock = Block::fromJsonString($schemaJson);
// so that you can change the type
$schemaBlock->set(
"items.properties.rating.type",
"integer"
);
// the validation should be false because integer vs number
$data->validateJsonWithSchema(
$schemaBlock->toJson()
);
// Assuming $object is an instance of the class that contains the applyField method
$object
->set('name', 'John Doe')
->applyField('name', 'uppercase_name', function($value) {
return strtoupper($value);
});
echo $object->get('uppercase_name'); // Outputs: JOHN DOE
## Testing
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.