legiaifenix/json-element-parser

A small Json file parser to PHP to then be able to manipulate elements in our views

0.0.1 2020-04-16 15:08 UTC

This package is not auto-updated.

Last update: 2025-01-31 14:12:07 UTC


README

Elements Parser

Elements Parser is a small PHP package that reads files and tries to recreate their structure output into HTML.

The idea came with the necessity to quickly deploy something that allowed me to pass on a file with contents (In my case was terms and conditions) which such file could be quickly translated. The project at hands didn't have a dashboard and I didn't think a full on PHP file returning an array of translatable terms and conditions was the way to go.

Though it was easier to have specific language files already translated and that could easily be send over to anybody for them to do the translations and them easily attach them to the server again without having to push code for changes.

Installation

composer require legiaifenix/json-element-parser

Supported File types

The only supported file for the time being is JSON. Depending of necessity/popularity, more can be implemented, although the priority probably would be turned towards allowing specific style, class and ID adherence.

Usage

  1. First identify the path and file of the JSON file type you wish to use (E.g:. public/files/drawables/en.json);

  2. Call the factory and pass in your path:

    use legiaifenix\jsonParser\Factories\ElementsBuilderFactory;
    
       ...
    
    $builder = ElementsBuilderFactory::create($filePath);
  3. Whenever you wish, you can ask the builder to, well, build up from your file. When build() is called, the builder uses the entire content from the file and loops through it to understand which types of elements it should be drawing.

    $builder->build();
  4. Draw the entirety of the content as HTML by calling

    $builder->draw();

Exceptions

What can the package draw as HTML?

Titles

{
  "title": "My first test"
}

Output

<h1>My first test</h1>

Paragraph

{
  "Not informing a type defaults into paragraphs"
}

Output

<p>Not informing a type defaults into paragraphs</p>

Logical structures

[
    "My first paragraph!",
    {
        "title": "My main title",
        "content": [
          "This is the first paragraph of my main title",
          "This follows the first paragraph as its second",
          "Can carry on adding paragraphs"
        ]
    }
]

Output

<p>My first paragraph!</p>
<h1>My main title</h1>
<p>This is the first paragraph of my main title</p>
<p>This follows the first paragraph as its second</p>
<p>Can carry on adding paragraphs</p>