Skip to content

Commit 7b764a7

Browse files
committed
Code
[+]: Code improvements
1 parent d2c5f15 commit 7b764a7

File tree

3 files changed

+56
-59
lines changed

3 files changed

+56
-59
lines changed

README.md

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{som
1919

2020
## Installing
2121

22-
- **via "composer require"**
23-
```shell
24-
composer require binary-cube/dot-array
22+
- **via "composer require"**:
23+
24+
``` shell
25+
composer require binary-cube/dot-array
2526
```
2627

27-
- **via composer (manually)**
28-
28+
- **via composer (manually)**:
29+
2930
If you're using Composer to manage dependencies, you can include the following
3031
in your `composer.json` file:
3132
@@ -39,107 +40,106 @@ DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{som
3940
4041
## Usage
4142
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.
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
4545
4646
- **instantiation**:
47-
- ```php
47+
- ```php
4848
new DotArray($array);
4949
DotArray::create($array);
5050
DotArray::createFromJson($jsonString);
51-
```
51+
```
5252
5353
- **get**:
54-
- ```php
54+
- ```php
5555
// Because the key `sci-fi & fantasy` is array the returning value it will be a new instance of DotArray.
5656
$dot('books.{sci-fi & fantasy}');
57-
57+
5858
// Because the price is not an array, the result will be raw data, float in this case.
5959
$dot('books.{sci-fi & fantasy}.0.price');
60-
60+
6161
// Accessing the raw array.
6262
$dot('books.{sci-fi & fantasy}')->toArray();
6363
$dot->get('books.{sci-fi & fantasy}')->toArray();
64-
64+
6565
// Accessing the last leaf and getting the raw data.
6666
$dot('books.{sci-fi & fantasy}.0.name');
6767
$dot->get('books.{sci-fi & fantasy}.0.name');
6868

6969
// Vanilla PHP.
7070
$dot('books.{sci-fi & fantasy}.0.name');
7171
$dot['books']['sci-fi & fantasy'][0]['name'];
72-
```
72+
```
7373

7474
- **get :: more-complex**:
75-
- ```php
75+
- ```php
7676
// Using dotted key and accessing without getting confused.
7777
// Allowed tokens for keeping the names with dot(.) togethers are: '', "", [], (), {}
7878
$dot->get('config.{elastic-search}.\'v5.0\'.host')
7979
$dot->get('config.{elastic-search}."v5.0".host')
8080
$dot->get('config.{elastic-search}.[v5.0].host')
8181
$dot->get('config.{elastic-search}.(v5.0).host')
8282
$dot->get('config.{elastic-search}.{v5.0}.host')
83-
```
83+
```
8484
8585
- **set**:
86-
- ```php
86+
- ```php
8787
$dot->set('books.{sci-fi & fantasy}.0.name', 'New Name');
88-
88+
8989
// Vanilla PHP.
9090
$dot['books.{sci-fi & fantasy}.0.name'] = 'New Name';
9191
$dot['books']['sci-fi & fantasy'][0]['name'] = 'New Name';
92-
```
92+
```
9393
9494
- **clear** *(empty array <=> [])*:
95-
- ```php
95+
- ```php
9696
$dot->clear('books.{sci-fi & fantasy}');
9797
$dot->clear('books.{sci-fi & fantasy}', null);
9898
$dot->clear('books.{sci-fi & fantasy}.0.name', null);
99-
99+
100100
// Multiple keys.
101101
$dot->clear([
102102
'books.{sci-fi & fantasy}',
103103
'books.{childre\'s books}'
104104
]);
105-
105+
106106
// Vanilla PHP.
107107
$dot['books.{sci-fi & fantasy}'] = [];
108-
```
108+
```
109109
110110
- **delete** *(unset(...))*:
111-
- ```php
111+
- ```php
112112
$dot->delete('books.{sci-fi & fantasy}');
113113
$dot->delete('books.{sci-fi & fantasy}.0.name');
114114
$dot->delete(['books.{sci-fi & fantasy}.0', 'books.{childre\'s books}.0']);
115-
```
115+
```
116116
117117
- **find**:
118-
- ```php
118+
- ```php
119119
/*
120120
Find the first item in an array that passes the truth test, otherwise return false
121121
The signature of the callable must be: `function ($value, $key)`.
122122
*/
123123
$book = $dot->get('books.{childre\'s books}')->find(function ($value, $key) {
124124
return $value['price'] > 0;
125125
});
126-
```
126+
```
127127
128128
- **filter**:
129-
- ```php
129+
- ```php
130130
/*
131131
Use a callable function to filter through items.
132132
The signature of the callable must be: `function ($value, $key)`
133133
*/
134134
$books = $dot->get('books.{childre\'s books}')->filter(function ($value, $key) {
135-
return $value['name'] === 'Harry Potter and the Order of the Phoenix';
135+
return $value['name'] === 'Harry Potter and the Order of the Phoenix';
136136
});
137137
138138
$books->toArray();
139-
```
139+
```
140140
141141
- **where**:
142-
- ```php
142+
- ```php
143143
/*
144144
Allowed Operations:
145145
=, == ===, !=, !==, <, >, <=, >=,
@@ -161,9 +161,9 @@ DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{som
161161
$books = $dot->get('books.{childre\'s books}')->where(function ($value, $key) {
162162
return $value['name'] === 'Harry Potter and the Order of the Phoenix';
163163
});
164-
```
164+
```
165165
166-
#### Data Sample:
166+
### Data Sample:
167167
168168
```php
169169
$dummyArray = [

phpunit.xml.dist

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@
2828

2929

3030
<logging>
31-
<log type="tap" target="build/phpunit/report.tap"/>
32-
<log type="junit" target="build/phpunit/report.junit.xml"/>
33-
<log type="coverage-html" target="build/phpunit/coverage"/>
34-
<log type="coverage-text" target="build/phpunit/coverage.txt"/>
35-
<log type="coverage-clover" target="build/phpunit/logs/clover.xml"/>
31+
<log type="tap" target="build/phpunit/report.tap"/>
32+
<log type="junit" target="build/phpunit/report.junit.xml"/>
33+
34+
<log type="coverage-html" target="build/phpunit/coverage/html"/>
35+
<log type="coverage-text" target="build/phpunit/coverage/txt"/>
36+
<log type="coverage-clover" target="build/phpunit/coverage/xml"/>
3637
</logging>
3738
</phpunit>

src/DotArray.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,12 @@ protected function segmentsToKey(array $segments)
254254

255255

256256
/**
257-
* @param array|DotArray|mixed $a
258-
* @param array|DotArray|mixed $b
257+
* @param array|DotArray|mixed $array1
258+
* @param null|array|DotArray|mixed $array2
259259
*
260260
* @return array
261261
*/
262-
protected static function mergeRecursive($a, $b)
262+
protected static function mergeRecursive($array1, $array2 = null)
263263
{
264264
$args = \func_get_args();
265265
$res = \array_shift($args);
@@ -301,12 +301,12 @@ protected static function mergeRecursive($a, $b)
301301
* For integer-keyed elements, the elements from the latter array will
302302
* be appended to the former array.
303303
*
304-
* @param array|DotArray|mixed $a Array to be merged from. You can specify additional
305-
* arrays via third argument, fourth argument etc.
304+
* @param array|DotArray|mixed $array Array to be merged from. You can specify additional
305+
* arrays via second argument, third argument, fourth argument etc.
306306
*
307307
* @return static
308308
*/
309-
public function merge($a)
309+
public function merge($array)
310310
{
311311
$this->items = \call_user_func_array(
312312
[
@@ -709,17 +709,15 @@ public function where($criteria)
709709
foreach ($filters as $filter) {
710710
// Search for operation.
711711
if (\in_array($operation, $filter['tokens'])) {
712-
$closure = \Closure::fromCallable(
713-
function ($item) use ($filter, $property, $value) {
714-
$item = (array) $item;
712+
$closure = function ($item) use ($filter, $property, $value) {
713+
$item = (array) $item;
715714

716-
if (!array_key_exists($property, $item)) {
717-
return false;
718-
}
719-
720-
return $filter['closure']($item, $property, $value);
715+
if (!array_key_exists($property, $item)) {
716+
return false;
721717
}
722-
);
718+
719+
return $filter['closure']($item, $property, $value);
720+
};
723721

724722
break;
725723
}//end if
@@ -729,11 +727,9 @@ function ($item) use ($filter, $property, $value) {
729727

730728
// Dummy closure if nothing is provided.
731729
if (empty($closure)) {
732-
$closure = \Closure::fromCallable(
733-
function () {
734-
return true;
735-
}
736-
);
730+
$closure = function () {
731+
return true;
732+
};
737733
}
738734

739735
return $this->filter($closure, ARRAY_FILTER_USE_BOTH);

0 commit comments

Comments
 (0)