Chris@0: [![Build Status](https://travis-ci.org/grasmash/expander.svg?branch=master)](https://travis-ci.org/grasmash/expander) [![Packagist](https://img.shields.io/packagist/v/grasmash/expander.svg)](https://packagist.org/packages/grasmash/expander) Chris@0: [![Total Downloads](https://poser.pugx.org/grasmash/expander/downloads)](https://packagist.org/packages/grasmash/expander) [![Coverage Status](https://coveralls.io/repos/github/grasmash/expander/badge.svg?branch=master)](https://coveralls.io/github/grasmash/expander?branch=master) Chris@0: Chris@0: This tool expands property references in PHP arrays. For example implementation, see Yaml Expander. Chris@0: Chris@0: ### Installation Chris@0: Chris@0: composer require grasmash/expander Chris@0: Chris@0: ### Example usage: Chris@0: Chris@0: Property references use dot notation to indicate array keys, and must be wrapped in `${}`. Chris@0: Chris@0: Expansion logic: Chris@0: Chris@0: ```php Chris@0: 'book', Chris@0: 'book' => [ Chris@0: 'title' => 'Dune', Chris@0: 'author' => 'Frank Herbert', Chris@0: 'copyright' => '${book.author} 1965', Chris@0: 'protaganist' => '${characters.0.name}', Chris@0: 'media' => [ Chris@0: 0 => 'hardcover', Chris@0: 1 => 'paperback', Chris@0: ], Chris@0: 'nested-reference' => '${book.sequel}', Chris@0: ], Chris@0: 'characters' => [ Chris@0: 0 => [ Chris@0: 'name' => 'Paul Atreides', Chris@0: 'occupation' => 'Kwisatz Haderach', Chris@0: 'aliases' => [ Chris@0: 0 => 'Usul', Chris@0: 1 => 'Muad\'Dib', Chris@0: 2 => 'The Preacher', Chris@0: ], Chris@0: ], Chris@0: 1 => [ Chris@0: 'name' => 'Duncan Idaho', Chris@0: 'occupation' => 'Swordmaster', Chris@0: ], Chris@0: ], Chris@0: 'summary' => '${book.title} by ${book.author}', Chris@0: 'publisher' => '${not.real.property}', Chris@0: 'sequels' => '${book.sequel}, and others.', Chris@0: 'available-products' => '${book.media.1}, ${book.media.0}', Chris@0: 'product-name' => '${${type}.title}', Chris@0: 'boolean-value' => true, Chris@0: 'null-value' => NULL, Chris@0: 'inline-array' => [ Chris@0: 0 => 'one', Chris@0: 1 => 'two', Chris@0: 2 => 'three', Chris@0: ], Chris@0: 'expand-array' => '${inline-array}', Chris@0: 'env-test' => '${env.test}', Chris@0: ]; Chris@0: Chris@0: $expander = new Expander(); Chris@0: // Optionally set a logger. Chris@0: $expander->setLogger(new Psr\Log\NullLogger()); Chris@0: // Optionally set a Stringfier, used to convert array placeholders into strings. Defaults to using implode() with `,` delimeter. Chris@0: // @see StringifierInterface. Chris@0: $expander->setStringifier(new Grasmash\Expander\Stringifier()); Chris@0: Chris@0: // Parse an array, expanding internal property references. Chris@0: $expanded = $expander->expandArrayProperties($array); Chris@0: Chris@0: // Parse an array, expanding references using both internal and supplementary values. Chris@0: $reference_properties = 'book' => ['sequel' => 'Dune Messiah']; Chris@0: // Set an environmental variable. Chris@0: putenv("test=gomjabbar"); Chris@0: $expanded = $expander->expandArrayProperties($array, $reference_properties); Chris@0: Chris@0: print_r($expanded); Chris@0: ```` Chris@0: Chris@0: Resultant array: Chris@0: Chris@0: ```php Chris@0: Array Chris@0: ( Chris@0: [type] => book Chris@0: [book] => Array Chris@0: ( Chris@0: [title] => Dune Chris@0: [author] => Frank Herbert Chris@0: [copyright] => Frank Herbert 1965 Chris@0: [protaganist] => Paul Atreides Chris@0: [media] => Array Chris@0: ( Chris@0: [0] => hardcover Chris@0: [1] => paperback Chris@0: ) Chris@0: Chris@0: [nested-reference] => Dune Messiah Chris@0: ) Chris@0: Chris@0: [characters] => Array Chris@0: ( Chris@0: [0] => Array Chris@0: ( Chris@0: [name] => Paul Atreides Chris@0: [occupation] => Kwisatz Haderach Chris@0: [aliases] => Array Chris@0: ( Chris@0: [0] => Usul Chris@0: [1] => Muad'Dib Chris@0: [2] => The Preacher Chris@0: ) Chris@0: Chris@0: ) Chris@0: Chris@0: [1] => Array Chris@0: ( Chris@0: [name] => Duncan Idaho Chris@0: [occupation] => Swordmaster Chris@0: ) Chris@0: Chris@0: ) Chris@0: Chris@0: [summary] => Dune by Frank Herbert Chris@0: [publisher] => ${not.real.property} Chris@0: [sequels] => Dune Messiah, and others. Chris@0: [available-products] => paperback, hardcover Chris@0: [product-name] => Dune Chris@0: [boolean-value] => 1 Chris@0: [null-value] => Chris@0: [inline-array] => Array Chris@0: ( Chris@0: [0] => one Chris@0: [1] => two Chris@0: [2] => three Chris@0: ) Chris@0: Chris@0: [expand-array] => one,two,three Chris@0: [env-test] => gomjabbar Chris@0: [env] => Array Chris@0: ( Chris@0: [test] => gomjabbar Chris@0: ) Chris@0: Chris@0: ) Chris@0: Chris@0: ```