Skip to content

Convert lists in indentedLines format to php arrays

Notifications You must be signed in to change notification settings

lingtalfi/IndentedLines

Repository files navigation

IndentedLines

2015-12-15 -> 2021-03-05

Convert lists in indentedLines format to php arrays.

IndentedLines is part of the universe framework.

Install

Using the planet installer via light-cli

lt install Ling.IndentedLines

Using the uni tool

uni import Ling/IndentedLines

This tool is a php implementation of the indentedLines notation, which aims at creating programming arrays using a simple language independent notation.

IndentedLines can be installed as a planet.

How to use it?

A basic example

<?php


use Ling\IndentedLines\Tool\IndentedLinesTool;

require_once "bigbang.php";



$string = <<<EEE
apple
banana
cherry
lemon
EEE;


a(IndentedLinesTool::parseFlatList($string)); // [apple, banana, cherry, lemon]

A demonstration example

<?php


use Ling\IndentedLines\Tool\IndentedLinesTool;

require_once "bigbang.php";



$string = <<<EEE

# this is a comment
- apple
- all:
----- peter
----- colors:
--------- red
--------- blue
- signature: <
    This is a multi line.
    And this is the second line.
>
- banana

EEE;


a(IndentedLinesTool::parseDashList($string));

The resulting array is as you would expect:

array (size=4)
  0 => string 'apple' (length=5)
  'all' => 
    array (size=2)
      0 => string 'peter' (length=5)
      'colors' => 
        array (size=2)
          0 => string 'red' (length=3)
          1 => string 'blue' (length=4)
  'signature' => string 'This is a multi line.
And this is the second line.' (length=50)
  1 => string 'banana' (length=6)

How does it work under the hood?

If you like very boring stories, you are in the good section. A very good start is to read the IndentedLines notation, which is the goal of this implementation.

I will give you an overview of how this specific implementation works though:

The NodeTreeBuilder is the main object, it is responsible for parsing the string top down, line by line. It handles multi lines and comments.

The NodeTreeBuilder uses a KeyFinder object, which goal is to separate the key from the value. For instance:

- fruit: apple      // key=fruit, value=apple 
- apple      // value=apple, no key as far as the keyFinder is concerned 

Once the KeyFinder has found (or not found) the key, the NodeTreeBuilder is able to determine the value.

The NodeTreeBuilder does his job, and in the end, it produces a big Node object, which can contain other Node objects recursively, and which represents the whole parsed structure.

I used Node objects because with a Node it's easier to keep track of which line is the children of which in trees that sometimes get complex.

Since our goal is to produce a php array, there is a NodeToArrayConvertor, that does the job of converting the big Node to a php array structure.

To hide the implementation details, I added the IndentedLinesTool which provides two simple general methods: parseFlatList and parseDashList.

Dependencies

History Log

  • 1.2.3 -- 2021-03-05

    • update README.md, add install alternative
  • 1.2.2 -- 2020-12-08

    • Fix lpi-deps not using natsort.
  • 1.2.1 -- 2020-12-04

    • Add lpi-deps.byml file
  • 1.2.0 -- 2016-01-11

    • add QuotableValueInterpreter.setQuotedValueIsAlwaysString method
  • 1.1.0 -- 2015-12-19

    • add QuotableValueInterpreter
  • 1.0.2 -- 2015-12-19

    • fix BaseNodeTreeBuild.setKeyFinder, return the instance for fluidity
  • 1.0.1 -- 2015-12-19

    • enhance KeyFinder, add static create method
  • 1.0.0 -- 2015-12-15

    • initial commit

About

Convert lists in indentedLines format to php arrays

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages