Giter Site home page Giter Site logo

pancakes's Introduction

Pancakes

Latest Stable Version

Estudo de caso para Tabulação e Expansão de objetos construídos a partir de um json-schema.

Definição de Uso

Dado a definição de um objeto especificada por um 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

Temos por objetivo realizar a tabulação de seu conteúdo visando simplificar a sua manipulação:

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

A estrutura tabulada irá compor em um mesmo nível chave (caminho) e valor (definição):

{
  "$.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"
  }
}

Tendo a estrutura tabulada em mãos podemos criar um objeto achatado simplificando seu armazenamento:

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

Essa mesma estrutura pode ser utilizada em um segundo momento para recompor o objeto original:

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

Assumindo assim o formato especificado pelo json schema utilizado como base de tabulação:

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

Contribuições

Contribuições, correções e sugestões de melhoria são muito bem-vindas.

pancakes's People

Contributors

realfabecker avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.