Pull merge.
[yaffs-website] / vendor / grasmash / expander / README.md
1 [![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)
2 [![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)
3
4 This tool expands property references in PHP arrays. For example implementation, see Yaml Expander.
5
6 ### Installation
7
8     composer require grasmash/expander
9
10 ### Example usage:
11
12 Property references use dot notation to indicate array keys, and must be wrapped in `${}`.
13
14 Expansion logic:
15
16 ```php
17 <?php
18
19 $array = [
20     'type' => 'book',
21     'book' => [
22         'title' => 'Dune',
23         'author' => 'Frank Herbert',
24         'copyright' => '${book.author} 1965',
25         'protaganist' => '${characters.0.name}',
26         'media' => [
27             0 => 'hardcover',
28             1 => 'paperback',
29         ],
30         'nested-reference' => '${book.sequel}',
31     ],
32     'characters' => [
33         0 => [
34             'name' => 'Paul Atreides',
35             'occupation' => 'Kwisatz Haderach',
36             'aliases' => [
37                 0 => 'Usul',
38                 1 => 'Muad\'Dib',
39                 2 => 'The Preacher',
40             ],
41         ],
42         1 => [
43             'name' => 'Duncan Idaho',
44             'occupation' => 'Swordmaster',
45         ],
46     ],
47     'summary' => '${book.title} by ${book.author}',
48     'publisher' => '${not.real.property}',
49     'sequels' => '${book.sequel}, and others.',
50     'available-products' => '${book.media.1}, ${book.media.0}',
51     'product-name' => '${${type}.title}',
52     'boolean-value' => true,
53     'null-value' => NULL,
54     'inline-array' => [
55         0 => 'one',
56         1 => 'two',
57         2 => 'three',
58     ],
59     'expand-array' => '${inline-array}',
60     'env-test' => '${env.test}',
61 ];
62
63 $expander = new Expander();
64 // Optionally set a logger.
65 $expander->setLogger(new Psr\Log\NullLogger());
66 // Optionally set a Stringfier, used to convert array placeholders into strings. Defaults to using implode() with `,` delimeter.
67 // @see StringifierInterface.
68 $expander->setStringifier(new Grasmash\Expander\Stringifier());
69
70 // Parse an array, expanding internal property references.
71 $expanded = $expander->expandArrayProperties($array);
72
73 // Parse an array, expanding references using both internal and supplementary values.
74 $reference_properties =  'book' => ['sequel' => 'Dune Messiah'];
75 // Set an environmental variable.
76 putenv("test=gomjabbar");
77 $expanded = $expander->expandArrayProperties($array, $reference_properties);
78
79 print_r($expanded);
80 ````
81
82 Resultant array:
83
84 ```php
85 Array
86 (
87     [type] => book
88     [book] => Array
89         (
90             [title] => Dune
91             [author] => Frank Herbert
92             [copyright] => Frank Herbert 1965
93             [protaganist] => Paul Atreides
94             [media] => Array
95                 (
96                     [0] => hardcover
97                     [1] => paperback
98                 )
99
100             [nested-reference] => Dune Messiah
101         )
102
103     [characters] => Array
104         (
105             [0] => Array
106                 (
107                     [name] => Paul Atreides
108                     [occupation] => Kwisatz Haderach
109                     [aliases] => Array
110                         (
111                             [0] => Usul
112                             [1] => Muad'Dib
113                             [2] => The Preacher
114                         )
115
116                 )
117
118             [1] => Array
119                 (
120                     [name] => Duncan Idaho
121                     [occupation] => Swordmaster
122                 )
123
124         )
125
126     [summary] => Dune by Frank Herbert
127     [publisher] => ${not.real.property}
128     [sequels] => Dune Messiah, and others.
129     [available-products] => paperback, hardcover
130     [product-name] => Dune
131     [boolean-value] => 1
132     [null-value] =>
133     [inline-array] => Array
134         (
135             [0] => one
136             [1] => two
137             [2] => three
138         )
139
140     [expand-array] => one,two,three
141     [env-test] => gomjabbar
142     [env] => Array
143         (
144             [test] => gomjabbar
145         )
146
147 )
148
149 ```