项目作者: byjg

项目描述 :
Anydataset JSON abstraction. Anydataset is an agnostic data source abstraction layer in PHP.
高级语言: PHP
项目地址: git://github.com/byjg/anydataset-json.git
创建时间: 2018-11-17T20:51:54Z
项目社区:https://github.com/byjg/anydataset-json

开源协议:MIT License

下载


AnyDataset-Json

Build Status
Opensource ByJG
GitHub source
GitHub license
GitHub release

JSON abstraction dataset. Anydataset is an agnostic data source abstraction layer in PHP.

See more about Anydataset here.

Concept

The AnyDataset-Json is an abstraction layer to read a JSON data and transform it into a dataset,
and you can manipulate it as a table.

Some features:

  • Read a JSON file or string
  • Define and extract fields
  • Validate some elements such as if is required or not, datatype, etc

Example

example.json

  1. {
  2. "menu":{
  3. "header":"SVG Viewer",
  4. "items":[
  5. {
  6. "id":"Open",
  7. "metadata":{
  8. "version":"1",
  9. "date":"NA"
  10. }
  11. },
  12. {
  13. "id":"OpenNew",
  14. "label":"Open New",
  15. "metadata":{
  16. "version":"2",
  17. "date":"2021-10-01"
  18. }
  19. }
  20. ]
  21. }
  22. }

example.php

  1. $json = file_get_contents('example.json');
  2. $dataset = new \ByJG\AnyDataset\Json\JsonDataset($json);
  3. $iterator = $dataset->getIterator("/menu/items")
  4. ->withFields([
  5. JsonFieldDefinition::create("name", "id"),
  6. JsonFieldDefinition::create("version", "metadata/version")
  7. ]);
  8. foreach ($iterator as $row) {
  9. echo $row->get('name'); // Print "Open", "OpenNew"
  10. echo $row->get('version'); // Print "1", "2"
  11. }

Features

Install

  1. composer require "byjg/anydataset-json"

Running the Unit tests

  1. vendor/bin/phpunit

Dependencies

  1. flowchart TD
  2. byjg/anydataset-json --> byjg/anydataset
  3. byjg/anydataset-json --> ext-json

Open source ByJG