olajoscs/nestable-generator

A json generator and parser for the nestable structure editor

1.0.1 2017-09-07 07:54 UTC

This package is not auto-updated.

Last update: 2024-09-14 02:48:15 UTC


README

This is a small PHP package, to have a generator and parser for the Nestable2 javascript plugin.

Requirements

  • php ~7.1

Usage

Instantiating the NestableGenerator class

  • First of all, an array of elements are needed, which will be the base to generate the json. These elements must implement the Element interface. Basic functionality is implemented in the AbstractElement class, but it can be skipped if own implementation is needed.
  • An object is needed which implements the NestedElement interface - this object is the source of the json generation. The Element objects are transformed to NestedElement objects. These NestedElements objects have a children property, to make possible the structure building.
  • The NestableGenerator object needs only one constructor parameter: an ElementCollection object, which are built with the array of Element objects. This ElementCollection needs an array of the Element objects and an empty NestableElement object, which will be used as reference.

Generating json

Once the NestableGenerator is created with the ElementCollection, just call generate(). It returns the JSON which is needed to Nestable to generate the block.

Parsing JSON

Nestable can send the created structure in json form. To parse this you need to instantiate the NestableGenerator class and call parse($json), where the $json is the JSON string, generated by Nestable . It returns the array of the Element objects.