|
1 | 1 | # DotArray
|
2 | 2 |
|
3 |
| -Require `PHP >=7.1` |
| 3 | +[](https://github.com/binary-cube/dot-array) |
4 | 4 |
|
5 | 5 | Accessing PHP Arrays via DOT notation is easy as:
|
6 | 6 |
|
@@ -65,6 +65,9 @@ DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{som
|
65 | 65 | // Accessing the last leaf and getting the raw data.
|
66 | 66 | $dot('books.{sci-fi & fantasy}.0.name');
|
67 | 67 | $dot->get('books.{sci-fi & fantasy}.0.name');
|
| 68 | + |
| 69 | + // Giving a default value in case the requested key is not found. |
| 70 | + $dot->get('key.not.exist', 'not-found-as-string'); |
68 | 71 |
|
69 | 72 | // Vanilla PHP.
|
70 | 73 | $dot('books.{sci-fi & fantasy}.0.name');
|
@@ -137,27 +140,66 @@ DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{som
|
137 | 140 |
|
138 | 141 | $books->toArray();
|
139 | 142 | ```
|
| 143 | + |
| 144 | +- **filterBy**: |
| 145 | + - ```php |
| 146 | + /* |
| 147 | + Allowed comparison operators: |
| 148 | + - [ =, ==, eq (equal) ] |
| 149 | + - [ ===, i (identical) ] |
| 150 | + - [ !=, ne (not equal) ] |
| 151 | + - [ !==, ni (not identical) ] |
| 152 | + - [ <, lt (less than) ] |
| 153 | + - [ >, gr (greater than) ] |
| 154 | + - [ <=, lte (less than or equal to) ] |
| 155 | + - [ =>, gte (greater than or equal to) ] |
| 156 | + - [ in, contains ] |
| 157 | + - [ not-in, not-contains ] |
| 158 | + - [ between ] |
| 159 | + - [ not-between ] |
| 160 | + */ |
| 161 | + // Example 1. |
| 162 | + $books = $dot->get('books.{childre\'s books}')->filterBy('price', 'between', 5, 12); |
| 163 | +
|
| 164 | + // Example 2. |
| 165 | + $books = $dot->get('books.{childre\'s books}')->filterBy('price', '>', 10); |
| 166 | +
|
| 167 | + // Example 3. |
| 168 | + $books = $dot->get('books.{childre\'s books}')->filterBy('price', 'in', [8.5, 15.49]); |
| 169 | + ``` |
140 | 170 |
|
141 | 171 | - **where**:
|
142 | 172 | - ```php
|
143 | 173 | /*
|
144 |
| - Allowed Operations: |
145 |
| - =, == ===, !=, !==, <, >, <=, >=, |
146 |
| - in, not-in, between, not-between, eq, ne, lt, gt, lte, gte, contains, not-contains |
| 174 | + The signature of the `where` call can be: |
| 175 | + - where([property, comparisonOperator, ...value]) |
| 176 | + - where(\Closure) :: The signature of the callable must be: `function ($value, $key)` |
| 177 | +
|
| 178 | + Allowed comparison operators: |
| 179 | + - [ =, ==, eq (equal) ] |
| 180 | + - [ ===, i (identical) ] |
| 181 | + - [ !=, ne (not equal) ] |
| 182 | + - [ !==, ni (not identical) ] |
| 183 | + - [ <, lt (less than) ] |
| 184 | + - [ >, gr (greater than) ] |
| 185 | + - [ <=, lte (less than or equal to) ] |
| 186 | + - [ =>, gte (greater than or equal to) ] |
| 187 | + - [ in, contains ] |
| 188 | + - [ not-in, not-contains ] |
| 189 | + - [ between ] |
| 190 | + - [ not-between ] |
147 | 191 | */
|
148 | 192 |
|
149 |
| - // Example 1. |
150 |
| - $books = $dot->get('books.{childre\'s books}')->where(['between', 'price', 5, 12]); |
| 193 | + // Example 1. (using the signature: [property, comparisonOperator, ...value]) |
| 194 | + $books = $dot->get('books.{childre\'s books}')->where(['price', 'between', 5, 12]); |
151 | 195 |
|
152 |
| - $books->toArray(); |
153 |
| - |
154 |
| - // Example 2. |
155 |
| - $books = $dot->get('books.{childre\'s books}')->where(['>', 'price', 10]); |
| 196 | + // Example 2. (using the signature: [property, comparisonOperator, ...value]) |
| 197 | + $books = $dot->get('books.{childre\'s books}')->where(['price', '>', 10]); |
156 | 198 |
|
157 |
| - // Example 3. |
158 |
| - $books = $dot->get('books.{childre\'s books}')->where(['in', 'price', [8.5, 15.49]]); |
| 199 | + // Example 3. (using the signature: [property, comparisonOperator, ...value]) |
| 200 | + $books = $dot->get('books.{childre\'s books}')->where(['price', 'in', [8.5, 15.49]]); |
159 | 201 |
|
160 |
| - // Example 4. |
| 202 | + // Example 4. (using the signature: \Closure) |
161 | 203 | $books = $dot->get('books.{childre\'s books}')->where(function ($value, $key) {
|
162 | 204 | return $value['name'] === 'Harry Potter and the Order of the Phoenix';
|
163 | 205 | });
|
|
0 commit comments