2017 © Pedro Peláez
 

library elevator

Elevates a map which keys are a materialized path to a tree. Folds a tree to a map by materializing the paths to the leafs.

image

pflorek/elevator

Elevates a map which keys are a materialized path to a tree. Folds a tree to a map by materializing the paths to the leafs.

  • Friday, March 17, 2017
  • by pflorek
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

PHP Elevator

Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version Total Downloads Latest Unstable Version License Monthly Downloads Daily Downloads composer.lock, (*1)

This library provides a simple way to elevate a map (associative array) to a tree or to flatten a tree to a map (associative array). The tree's node keys are the tokens of the map's materialized path separated by a delimiter., (*2)

This library comes handy to map flat lists like SQL results to documents. The result's column name could be a materialized path of the target document. You can pass a result row to Elevator::up and proceed marshalling by passing the elevated tree to a JSON or XML serializer. On unmarshalling just pass the deserialized array tree to Elevator::down and feed e.g. a SQL statement., (*3)

Usage

Elevate

Elevate a map with materialized paths to a tree:, (*4)

use PFlorek\Elevator\Elevator;
use PFlorek\Elevator\ElevatorFactory;
use function \PFlorek\Elevator\array_elevate;

$flattened = [
    'World.Asia.Afghanistan.0' => '...',
    'World.Africa' => true,
    'World.Antarctica' => -25.2,
    'World.Europe' => new \stdClass(),
    'World.North America' => [],
];

// object oriented
$factory = ElevatorFactory::getInstance();
$elevator = $factory->create();
$elevator->up($flattened);

// or functional
$elevated = array_elevate($flattened);

var_dump($elevated);

//returns ["World"] => array(5) {
//  ["Asia"] => array(1) {
//    ["Afghanistan"] => array(1) {
//      [0] => string(3) "..."
//    }
//  }
//  ["Africa"] => bool(true)
//  ["Antarctica"] => float(-25.2)
//  ["Europe"]=> object(stdClass)#298 (0) {}
//  ["North America"]=> []
//}

Flatten

Flattens a tree to a map which keys are the materialized path of the node's keys:, (*5)

use PFlorek\Elevator\Elevator;
use PFlorek\Elevator\ElevatorFactory;
use function \PFlorek\Elevator\array_flatten;

$elevated = [
    'World' => [
        'Asia' => [
            'Afghanistan' => [
                '...'
            ]
        ],
        'Africa' => true,
        'Antarctica' => -25.2,
        'Europe' => new \stdClass(),
        'North America' => [],
    ]
];

// object oriented
$factory = ElevatorFactory::getInstance();
$elevator = $factory->create();
$elevator->down($flattened);

// or functional
$flattened = array_flatten($elevated);

var_dump($flattened);

//returns array(5) {
//  ["World.Asia.Afghanistan.0"] => string(3) "..."
//  ["World.Africa"] => bool(true)
//  ["World.Antarctica"] => float(-25.2)
//  ["World.Europe"] => object(stdClass) (0) { }
//  ["World.North America"] => array(0) { }
//}

Installation

Use Composer to install the package:, (*6)

composer require pflorek/elevator

Authors

Contribute

Contributions are always welcome!, (*7)

License

All contents of this package are licensed under the MIT license., (*8)

The Versions

17/03 2017

dev-master

9999999-dev

Elevates a map which keys are a materialized path to a tree. Folds a tree to a map by materializing the paths to the leafs.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

serialize tree map materialized path

17/03 2017

1.0.0-RC1

1.0.0.0-RC1

Elevates a map which keys are a materialized path to a tree. Folds a tree to a map by materializing the paths to the leafs.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

serialize tree map materialized path