Skip to content

Commit d508af4

Browse files
committed
add Data::get and Data::value
1 parent 64a3ae2 commit d508af4

1 file changed

Lines changed: 66 additions & 7 deletions

File tree

v5/utilities.md

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ use \TypeRocket\Utility\Arr;
135135

136136
$array = ['products' => ['mic' => ['price' => 99]];
137137

138-
$price = Arr::get($array, 'products.mic.price');
138+
Arr::get($array, 'products.mic.price');
139139
// 99
140140
```
141141

@@ -146,10 +146,10 @@ use \TypeRocket\Utility\Arr;
146146

147147
$array = ['product' => ['name' => null, 'price' => 99]];
148148

149-
$price = Arr::get($array, 'product.tax', 10);
149+
Arr::get($array, 'product.tax', 10);
150150
// 10
151151

152-
$price = Arr::get($array, 'product.name', 10);
152+
Arr::get($array, 'product.name', 10);
153153
// null
154154
```
155155

@@ -162,10 +162,10 @@ use \TypeRocket\Utility\Arr;
162162

163163
$array = ['product' => ['name' => null, 'price' => 99]];
164164

165-
$price = Arr::has($array, 'product.name');
165+
Arr::has($array, 'product.name');
166166
// true
167167

168-
$price = Arr::has($array, 'product.tax');
168+
Arr::has($array, 'product.tax');
169169
// false
170170
```
171171

@@ -176,7 +176,7 @@ use \TypeRocket\Utility\Arr;
176176

177177
$array = ['product' => ['name' => null, 'price' => 99]];
178178

179-
$price = Arr::has($array, ['product.name', 'product.price']);
179+
Arr::has($array, ['product.name', 'product.price']);
180180
// true
181181
```
182182

@@ -466,7 +466,7 @@ use \TypeRocket\Utility\Arr;
466466

467467
$array = ['product' => ['name' => null, 'price' => 99]];
468468

469-
$price = Arr::set('product.name', $array, 'mic');
469+
Arr::set('product.name', $array, 'mic');
470470
// ['product' => ['name' => 'mic', 'price' => 99]]
471471
```
472472

@@ -981,6 +981,47 @@ Data::cast(1, 'array');
981981
// 1 - is not compatible with array cast
982982
```
983983

984+
### Data::get
985+
986+
The `Data::get()` method retrieves a value from a deeply nested `array` or `object` using dot notation:
987+
988+
```php
989+
use \TypeRocket\Utility\Data;
990+
991+
$product = new \stdClass;
992+
$product->name = 'mic';
993+
$product->price = 99;
994+
$product->type = null;
995+
996+
$array = ['products' => [$product];
997+
998+
Data::get($array, 'products.0.price');
999+
// 99
1000+
1001+
Data::get($array, ['products.0.price', 'products.0.type']);
1002+
// ['products.0.price' => 99, 'products.0.type' => null]
1003+
```
1004+
1005+
The `Data::get()` method also takes a default value, which will be returned if the specified key is **not present** or its value is `null` in the array:
1006+
1007+
```php
1008+
use \TypeRocket\Utility\Data;
1009+
1010+
$product = new \stdClass;
1011+
$product->name = 'mic';
1012+
$product->price = 99;
1013+
$product->type = null;
1014+
1015+
Data::get($array, 'products.0.type', 10);
1016+
// 10
1017+
1018+
Data::get($array, 'products.0.color', 'blue');
1019+
// 'blue'
1020+
1021+
Data::get($array, 'products.1.color', 'blue');
1022+
// 'blue'
1023+
```
1024+
9841025
### Data:isJson
9851026

9861027
The `Data::isJson()` method returns `true` if the given value is valid `json`:
@@ -1067,6 +1108,24 @@ Data::nil($value)->one->['two']->three->get();
10671108
// null
10681109
```
10691110

1111+
### Data::value
1112+
1113+
The `Data::value()` method returns the given value unless it is a `callable`. If the value is `callable`, the method calls it using the `\TypeRocket\Core\Resolver::resolveCallable()` method:
1114+
1115+
```php
1116+
use TypeRocket\Utility\Data;
1117+
1118+
Data::value(1);
1119+
// 1
1120+
1121+
$args = ['id' => 1];
1122+
1123+
Data::value(function(\TypeRocket\Http\Request $request, $id = null) {
1124+
return $id;
1125+
}, $args);
1126+
// 1
1127+
```
1128+
10701129
### Data::walk
10711130

10721131
The `Data::isJson()` method used dot notation to look up a value by array keys and/or object properties.

0 commit comments

Comments
 (0)