sintese/phancackes

v0.4.0 2025-02-15 20:32 UTC

This package is auto-updated.

Last update: 2025-02-16 19:32:04 UTC


README

Case study for Tabulation and Expansion of objects built from a json-schema.

Latest Stable Version Security Rating

Usage

Given the definition of an object specified by a json-schema:

$schema = <<<JSON 
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/product.schema.json",
  "title": "Product",
  "description": "A product from Acme's catalog",
  "type": "object",
  "properties": {
    "productId": {
      "description": "The unique identifier for a product",
      "type": "integer"
    },
    "dimensions": {
      "type": "object",
      "properties": {
        "width": {
          "type": "number"
        },
        "height": {
          "type": "number"
        }
      }
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          }
        }
      }
    }
  }
}
JSON

Our goal is to tabulate its content to simplify manipulation:

echo (new SchemaFlatten())->flat(new SchemaObject($schema));

The tabulated structure will compose key (path) and value (definition) at the same level:

{
  "$.productId": {
    "description": "The unique identifier for a product",
    "type": "integer",
    "path": "$.productId",
    "prop": "productId"
  },
  "$.dimensions.width": {
    "type": "number",
    "path": "$.dimensions.width",
    "prop": "width"
  },
  "$.dimensions.height": {
    "type": "number",
    "path": "$.dimensions.height",
    "prop": "height"
  },
  "$.tags[0].name": {
    "type": "string",
    "path": "$.tags[0].name",
    "prop": "name"
  }
}

Having the tabulated structure in hand, we can create a flattened object to simplify its storage:

$payload = <<<JSON
{
  "$.productId": 1,
  "$.dimensions.width": 3,
  "$.dimensions.height": 6,
  "$.tags[0].name": "tag"
}
JSON;

This same structure can later be used to recompose the original object:

echo (new SchemaFlatten())->unflat($payload);

Thus assuming the format specified by the JSON schema used as the tabulation basis:

{
  "productId": 1,
  "dimensions": {
    "width": 3,
    "height": 6
  },
  "tags": [
    {
      "name": "tag"
    }
  ]
}

Contributions

Contributions, corrections, and improvement suggestions are very welcome.