• Stars
    star
    136
  • Rank 266,940 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 7 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This tool expands property references in PHP arrays.

CI Packagist Total Downloads Coverage Status

This tool expands dot-notated, string property references into their corresponding values. This is useful for run time expansion of property references in configuration files.

For example implementation, see Yaml Expander.

Installation

composer require grasmash/expander

Example usage:

Property references use dot notation to indicate array keys, and must be wrapped in ${}.

Expansion logic:

<?php

$array = [
    'type' => 'book',
    'book' => [
        'title' => 'Dune',
        'author' => 'Frank Herbert',
        'copyright' => '${book.author} 1965',
        'protaganist' => '${characters.0.name}',
        'media' => [
            0 => 'hardcover',
            1 => 'paperback',
        ],
        'nested-reference' => '${book.sequel}',
    ],
    'characters' => [
        0 => [
            'name' => 'Paul Atreides',
            'occupation' => 'Kwisatz Haderach',
            'aliases' => [
                0 => 'Usul',
                1 => 'Muad\'Dib',
                2 => 'The Preacher',
            ],
        ],
        1 => [
            'name' => 'Duncan Idaho',
            'occupation' => 'Swordmaster',
        ],
    ],
    'summary' => '${book.title} by ${book.author}',
    'publisher' => '${not.real.property}',
    'sequels' => '${book.sequel}, and others.',
    'available-products' => '${book.media.1}, ${book.media.0}',
    'product-name' => '${${type}.title}',
    'boolean-value' => true,
    'expand-boolean' => '${boolean-value}',
    'null-value' => NULL,
    'inline-array' => [
        0 => 'one',
        1 => 'two',
        2 => 'three',
    ],
    'expand-array' => '${inline-array}',
    'env-test' => '${env.test}',
];

$expander = new Expander();
// Optionally set a logger.
$expander->setLogger(new Psr\Log\NullLogger());
// Optionally set a Stringfier, used to convert array placeholders into strings. Defaults to using implode() with `,` delimeter.
// @see StringifierInterface.
$expander->setStringifier(new Grasmash\Expander\Stringifier());

// Parse an array, expanding internal property references.
$expanded = $expander->expandArrayProperties($array);

// Parse an array, expanding references using both internal and supplementary values.
$reference_properties =  'book' => ['sequel' => 'Dune Messiah'];
// Set an environmental variable.
putenv("test=gomjabbar");
$expanded = $expander->expandArrayProperties($array, $reference_properties);

print_r($expanded);

Resultant array:

Array
(
    [type] => book
    [book] => Array
        (
            [title] => Dune
            [author] => Frank Herbert
            [copyright] => Frank Herbert 1965
            [protaganist] => Paul Atreides
            [media] => Array
                (
                    [0] => hardcover
                    [1] => paperback
                )

            [nested-reference] => Dune Messiah
        )

    [characters] => Array
        (
            [0] => Array
                (
                    [name] => Paul Atreides
                    [occupation] => Kwisatz Haderach
                    [aliases] => Array
                        (
                            [0] => Usul
                            [1] => Muad\'Dib
                            [2] => The Preacher
                        )

                )

            [1] => Array
                (
                    [name] => Duncan Idaho
                    [occupation] => Swordmaster
                )

        )

    [summary] => Dune by Frank Herbert
    [publisher] => ${not.real.property}
    [sequels] => Dune Messiah, and others.
    [available-products] => paperback, hardcover
    [product-name] => Dune
    [boolean-value] => true,
    [expand-boolean] => true,
    [null-value] =>
    [inline-array] => Array
        (
            [0] => one
            [1] => two
            [2] => three
        )

    [expand-array] => one,two,three
    [env-test] => gomjabbar
    [env] => Array
        (
            [test] => gomjabbar
        )

)

More Repositories

1

yaml-expander

Expands internal property references in a yaml file.
PHP
154
star
2

composerize-drupal

Convert a non-Composer-managed Drupal application (e.g., one created via tarball) to a Composer-managed Drupal application.
PHP
125
star
3

yaml-cli

A command line tool for reading and manipulating yaml files.
PHP
22
star
4

xdebug-toggle

Simple CLI tools to toggle xdebug easily.
PHP
16
star
5

drupal8to9-upgrade

PHP
12
star
6

drupal-issue-chrome

A chrome extension that renders links to Drupal.org issues based on issue status.
JavaScript
8
star
7

drupal-security-warning

PHP
7
star
8

drupal-composer-training

How to build a Drupal site with Composer AND keep all of your hair! DrupalCon Nashville 2018
6
star
9

drupal-views-itunes-rss

Drupal 8 module for providing <itunes:*> tags via Views RSS.
PHP
6
star
10

project-browser

A project browser embedded in the Drupal UI.
JavaScript
6
star
11

drupal-usasearch

The USASearch module for Drupal.
PHP
5
star
12

composer-scaffold

Allows Composer to copy files around after package installation.
PHP
5
star
13

drupal-module-evaluator

PHP
4
star
14

symfony-console-checklist

A utility for creating animated spinners via Symfony Console.
PHP
2
star
15

drupal7-starter-kit

1
star
16

drupal-table-of-contents

Fork of table_of_contents module.
1
star
17

migrate_html

Example module for migrating static HTML to Drupal
PHP
1
star
18

transcript-analyzer

PHP
1
star
19

artifice

PHP
1
star
20

Fivestar-Node-Settings-Patch

Patch for Drupal 6's Fivestar module-- will allow enabling/disabing fivestar for specific nodes
JavaScript
1
star
21

drupal-gov-features

An assortment of Drupal features for government websites.
HTML
1
star
22

drupal-og-fpp

Organic Groups Fieldable Panels Panes
PHP
1
star