|
| 1 | +# DotArray |
| 2 | + |
| 3 | +Compatible with `PHP >=5.5` |
| 4 | + |
| 5 | +Accessing PHP Arrays via DOT notation is easy as: |
| 6 | + |
| 7 | +```php |
| 8 | +DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{some.dotted.key}') |
| 9 | +``` |
| 10 | +[](https://packagist.org/packages/binary-cube/dot-array) |
| 11 | +[](https://packagist.org/packages/binary-cube/dot-array) |
| 12 | +[](https://www.codacy.com/app/microThread/dot-array?utm_source=github.com&utm_medium=referral&utm_content=binary-cube/dot-array&utm_campaign=Badge_Grade) |
| 13 | +[](https://scrutinizer-ci.com/g/binary-cube/dot-array/build-status/master) |
| 14 | +[](https://scrutinizer-ci.com/g/binary-cube/dot-array/?branch=master) |
| 15 | +[](https://scrutinizer-ci.com/g/binary-cube/dot-array/?branch=master) |
| 16 | +[](LICENSE) |
| 17 | + |
| 18 | +----- |
| 19 | + |
| 20 | +## Installing |
| 21 | + |
| 22 | +- **via "composer require"** |
| 23 | + ```shell |
| 24 | + composer require binary-cube/dot-array |
| 25 | + ``` |
| 26 | + |
| 27 | +- **via composer (manually)** |
| 28 | + |
| 29 | + If you're using Composer to manage dependencies, you can include the following |
| 30 | + in your `composer.json` file: |
| 31 | +
|
| 32 | + ```json |
| 33 | + { |
| 34 | + "require": { |
| 35 | + "binary-cube/dot-array": "1.*" |
| 36 | + } |
| 37 | + } |
| 38 | + ``` |
| 39 | +
|
| 40 | +## Usage |
| 41 | +
|
| 42 | +
|
| 43 | +####### REMEMBER: YOU NEED TO KNOW YOUR DATA! |
| 44 | +####### DotArray::get() can return a new instance of DotArray in case the accessed path is an array or it will return the raw data value or the default given value. |
| 45 | +
|
| 46 | +- **instantiation**: |
| 47 | + - ```php |
| 48 | + new DotArray($array); |
| 49 | + DotArray::create($array); |
| 50 | + DotArray::createFromJson($jsonString); |
| 51 | + ``` |
| 52 | +
|
| 53 | +- **get**: |
| 54 | + - ```php |
| 55 | + // Because the key `sci-fi & fantasy` is array the returning value it will be a new instance of DotArray. |
| 56 | + $dot('books.{sci-fi & fantasy}'); |
| 57 | + |
| 58 | + // Because the price is not an array, the result will be raw data, float in this case. |
| 59 | + $dot('books.{sci-fi & fantasy}.0.price'); |
| 60 | + |
| 61 | + // Accessing the raw array. |
| 62 | + $dot('books.{sci-fi & fantasy}')->toArray(); |
| 63 | + $dot->get('books.{sci-fi & fantasy}')->toArray(); |
| 64 | + |
| 65 | + // Accessing the last leaf and getting the raw data. |
| 66 | + $dot('books.{sci-fi & fantasy}.0.name'); |
| 67 | + $dot->get('books.{sci-fi & fantasy}.0.name'); |
| 68 | + |
| 69 | + // Vanilla PHP. |
| 70 | + $dot('books.{sci-fi & fantasy}.0.name'); |
| 71 | + $dot['books']['sci-fi & fantasy'][0]['name']; |
| 72 | + ``` |
| 73 | + |
| 74 | +- **get :: more-complex**: |
| 75 | + - ```php |
| 76 | + // Using dotted key and accessing without getting confused. |
| 77 | + // Allowed tokens for keeping the names with dot(.) togethers are: '', "", [], (), {} |
| 78 | + $dot->get('config.{elastic-search}.\'v5.0\'.host') |
| 79 | + $dot->get('config.{elastic-search}."v5.0".host') |
| 80 | + $dot->get('config.{elastic-search}.[v5.0].host') |
| 81 | + $dot->get('config.{elastic-search}.(v5.0).host') |
| 82 | + $dot->get('config.{elastic-search}.{v5.0}.host') |
| 83 | + ``` |
| 84 | +
|
| 85 | +- **set**: |
| 86 | + - ```php |
| 87 | + $dot->set('books.{sci-fi & fantasy}.0.name', 'New Name'); |
| 88 | + |
| 89 | + // Vanilla PHP. |
| 90 | + $dot['books.{sci-fi & fantasy}.0.name'] = 'New Name'; |
| 91 | + $dot['books']['sci-fi & fantasy'][0]['name'] = 'New Name'; |
| 92 | + ``` |
| 93 | +
|
| 94 | +- **clear** *(empty array <=> [])*: |
| 95 | + - ```php |
| 96 | + $dot->clear('books.{sci-fi & fantasy}'); |
| 97 | + $dot->clear('books.{sci-fi & fantasy}', null); |
| 98 | + $dot->clear('books.{sci-fi & fantasy}.0.name', null); |
| 99 | + |
| 100 | + // Multiple keys. |
| 101 | + $dot->clear([ |
| 102 | + 'books.{sci-fi & fantasy}', |
| 103 | + 'books.{childre\'s books}' |
| 104 | + ]); |
| 105 | + |
| 106 | + // Vanilla PHP. |
| 107 | + $dot['books.{sci-fi & fantasy}'] = []; |
| 108 | + ``` |
| 109 | +
|
| 110 | +- **delete** *(unset(...))*: |
| 111 | + - ```php |
| 112 | + $dot->delete('books.{sci-fi & fantasy}'); |
| 113 | + $dot->delete('books.{sci-fi & fantasy}.0.name'); |
| 114 | + $dot->delete(['books.{sci-fi & fantasy}.0', 'books.{childre\'s books}.0']); |
| 115 | + ``` |
| 116 | +
|
| 117 | +- **find**: |
| 118 | + - ```php |
| 119 | + /* |
| 120 | + Find the first item in an array that passes the truth test, otherwise return false |
| 121 | + The signature of the callable must be: `function ($value, $key)`. |
| 122 | + */ |
| 123 | + $book = $dot->get('books.{childre\'s books}')->find(function ($value, $key) { |
| 124 | + return $value['price'] > 0; |
| 125 | + }); |
| 126 | + ``` |
| 127 | +
|
| 128 | +- **filter**: |
| 129 | + - ```php |
| 130 | + /* |
| 131 | + Use a callable function to filter through items. |
| 132 | + The signature of the callable must be: `function ($value, $key)` |
| 133 | + */ |
| 134 | + $books = $dot->get('books.{childre\'s books}')->filter(function ($value, $key) { |
| 135 | + return $value['name'] === 'Harry Potter and the Order of the Phoenix'; |
| 136 | + }); |
| 137 | + |
| 138 | + $books->toArray(); |
| 139 | + ``` |
| 140 | +
|
| 141 | +- **where**: |
| 142 | + - ```php |
| 143 | + /* |
| 144 | + Allowed Operations: |
| 145 | + =, == ===, !=, !==, <, >, <=, >=, |
| 146 | + in, not-in, between, not-between, eq, ne, lt, gt, lte, gte, contains, not-contains |
| 147 | + */ |
| 148 | + |
| 149 | + // Example 1. |
| 150 | + $books = $dot->get('books.{childre\'s books}')->where(['between', 'price', 5, 12]); |
| 151 | +
|
| 152 | + $books->toArray(); |
| 153 | + |
| 154 | + // Example 2. |
| 155 | + $books = $dot->get('books.{childre\'s books}')->where(['>', 'price', 10]); |
| 156 | + |
| 157 | + // Example 3. |
| 158 | + $books = $dot->get('books.{childre\'s books}')->where(['in', 'price', [8.5, 15.49]]); |
| 159 | + |
| 160 | + // Example 4. |
| 161 | + $books = $dot->get('books.{childre\'s books}')->where(function ($value, $key) { |
| 162 | + return $value['name'] === 'Harry Potter and the Order of the Phoenix'; |
| 163 | + }); |
| 164 | + ``` |
| 165 | +
|
| 166 | +#### Data Sample: |
| 167 | +
|
| 168 | +```php |
| 169 | +$dummyArray = [ |
| 170 | + 'books' => [ |
| 171 | + 'sci-fi & fantasy' => |
| 172 | + [ |
| 173 | + [ |
| 174 | + 'name' => 'Chronicles of Narnia Box Set', |
| 175 | + 'price' => 24.55, |
| 176 | + 'currency' => '$', |
| 177 | + 'authors' => |
| 178 | + [ |
| 179 | + [ |
| 180 | + 'name' => 'C.S. Lewis' |
| 181 | + ], |
| 182 | + ], |
| 183 | + ], |
| 184 | + [ |
| 185 | + 'name' => 'A Game of Thrones / A Clash of Kings / A Storm of Swords / A Feast of Crows / A Dance with Dragons ', |
| 186 | + 'price' => 37.97, |
| 187 | + 'currency' => '$', |
| 188 | + 'authors' => |
| 189 | + [ |
| 190 | + [ |
| 191 | + 'name' => 'George R. R. Martin' |
| 192 | + ], |
| 193 | + ], |
| 194 | + ], |
| 195 | + ], |
| 196 | + |
| 197 | + 'childre\'s books' => |
| 198 | + [ |
| 199 | + [ |
| 200 | + 'name' => 'Harry Potter and the Order of the Phoenix', |
| 201 | + 'price' => 15.49, |
| 202 | + 'currency' => '$', |
| 203 | + 'authors' => |
| 204 | + [ |
| 205 | + [ |
| 206 | + 'name' => 'J. K. Rowling' |
| 207 | + ], |
| 208 | + ], |
| 209 | + ], |
| 210 | + [ |
| 211 | + 'name' => 'Harry Potter and the Cursed Child', |
| 212 | + 'price' => 8.5, |
| 213 | + 'currency' => '$', |
| 214 | + 'authors' => |
| 215 | + [ |
| 216 | + [ |
| 217 | + 'name' => 'J. K. Rowling', |
| 218 | + ], |
| 219 | + [ |
| 220 | + 'name' => 'Jack Thorne' |
| 221 | + ], |
| 222 | + ], |
| 223 | + ], |
| 224 | + ], |
| 225 | + |
| 226 | + ], |
| 227 | +]; |
| 228 | +``` |
| 229 | +
|
| 230 | +## Authors |
| 231 | +
|
| 232 | +* **Banciu N. Cristian Mihai** |
| 233 | +
|
| 234 | +See also the list of [contributors](https://github.com/binary-cube/dot-array/graphs/contributors) who participated in this project. |
| 235 | +
|
| 236 | +## License |
| 237 | +
|
| 238 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details |
0 commit comments