Skip to content

Commit 3fa0b9a

Browse files
committed
Merge branch 'webdevium-chain-support' into 3.x
2 parents 3857c8c + 3388c2f commit 3fa0b9a

File tree

3 files changed

+127
-3
lines changed

3 files changed

+127
-3
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ $dot = dot($array);
7070
$dot = dot($array, true);
7171
```
7272

73+
All methods not returning a specific value returns the Dot object for chaining:
74+
```php
75+
$dot = dot();
76+
77+
$dot->add('user.name', 'John')->set('user.email', '[email protected]')->clear(); // returns empty Dot
78+
```
79+
7380
## Methods
7481

7582
Dot has the following methods:

src/Dot.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function __construct($items = [], $parse = false)
5252
*
5353
* @param array|int|string $keys
5454
* @param mixed $value
55+
* @return Dot
5556
*/
5657
public function add($keys, $value = null)
5758
{
@@ -62,6 +63,8 @@ public function add($keys, $value = null)
6263
} elseif ($this->get($keys) === null) {
6364
$this->set($keys, $value);
6465
}
66+
67+
return $this;
6568
}
6669

6770
/**
@@ -78,26 +81,30 @@ public function all()
7881
* Delete the contents of a given key or keys
7982
*
8083
* @param array|int|string|null $keys
84+
* @return Dot
8185
*/
8286
public function clear($keys = null)
8387
{
8488
if ($keys === null) {
8589
$this->items = [];
8690

87-
return;
91+
return $this;
8892
}
8993

9094
$keys = (array) $keys;
9195

9296
foreach ($keys as $key) {
9397
$this->set($key, []);
9498
}
99+
100+
return $this;
95101
}
96102

97103
/**
98104
* Delete the given key or keys
99105
*
100106
* @param array|int|string $keys
107+
* @return Dot
101108
*/
102109
public function delete($keys)
103110
{
@@ -124,6 +131,8 @@ public function delete($keys)
124131

125132
unset($items[$lastSegment]);
126133
}
134+
135+
return $this;
127136
}
128137

129138
/**
@@ -280,6 +289,7 @@ public function isEmpty($keys = null)
280289
*
281290
* @param array|string|self $key
282291
* @param array|self $value
292+
* @return Dot
283293
*/
284294
public function merge($key, $value = [])
285295
{
@@ -293,6 +303,8 @@ public function merge($key, $value = [])
293303
} elseif ($key instanceof self) {
294304
$this->items = array_merge($this->items, $key->all());
295305
}
306+
307+
return $this;
296308
}
297309

298310
/**
@@ -303,6 +315,7 @@ public function merge($key, $value = [])
303315
*
304316
* @param array|string|self $key
305317
* @param array|self $value
318+
* @return Dot
306319
*/
307320
public function mergeRecursive($key, $value = [])
308321
{
@@ -316,6 +329,8 @@ public function mergeRecursive($key, $value = [])
316329
} elseif ($key instanceof self) {
317330
$this->items = array_merge_recursive($this->items, $key->all());
318331
}
332+
333+
return $this;
319334
}
320335

321336
/**
@@ -327,6 +342,7 @@ public function mergeRecursive($key, $value = [])
327342
*
328343
* @param array|string|self $key
329344
* @param array|self $value
345+
* @return Dot
330346
*/
331347
public function mergeRecursiveDistinct($key, $value = [])
332348
{
@@ -340,6 +356,8 @@ public function mergeRecursiveDistinct($key, $value = [])
340356
} elseif ($key instanceof self) {
341357
$this->items = $this->arrayMergeRecursiveDistinct($this->items, $key->all());
342358
}
359+
360+
return $this;
343361
}
344362

345363
/**
@@ -395,13 +413,14 @@ public function pull($key = null, $default = null)
395413
*
396414
* @param mixed $key
397415
* @param mixed $value
416+
* @return Dot
398417
*/
399418
public function push($key, $value = null)
400419
{
401420
if ($value === null) {
402421
$this->items[] = $key;
403422

404-
return;
423+
return $this;
405424
}
406425

407426
$items = $this->get($key);
@@ -410,6 +429,8 @@ public function push($key, $value = null)
410429
$items[] = $value;
411430
$this->set($key, $items);
412431
}
432+
433+
return $this;
413434
}
414435

415436
/**
@@ -418,6 +439,7 @@ public function push($key, $value = null)
418439
*
419440
* @param array|string|self $key
420441
* @param array|self $value
442+
* @return Dot
421443
*/
422444
public function replace($key, $value = [])
423445
{
@@ -431,13 +453,16 @@ public function replace($key, $value = [])
431453
} elseif ($key instanceof self) {
432454
$this->items = array_replace($this->items, $key->all());
433455
}
456+
457+
return $this;
434458
}
435459

436460
/**
437461
* Set a given key / value pair or pairs
438462
*
439463
* @param array|int|string $keys
440464
* @param mixed $value
465+
* @return Dot
441466
*/
442467
public function set($keys, $value = null)
443468
{
@@ -446,7 +471,7 @@ public function set($keys, $value = null)
446471
$this->set($key, $value);
447472
}
448473

449-
return;
474+
return $this;
450475
}
451476

452477
$items = &$this->items;
@@ -460,26 +485,34 @@ public function set($keys, $value = null)
460485
}
461486

462487
$items = $value;
488+
489+
return $this;
463490
}
464491

465492
/**
466493
* Replace all items with a given array
467494
*
468495
* @param mixed $items
496+
* @return Dot
469497
*/
470498
public function setArray($items)
471499
{
472500
$this->items = $this->getArrayItems($items);
501+
502+
return $this;
473503
}
474504

475505
/**
476506
* Replace all items with a given array as a reference
477507
*
478508
* @param array $items
509+
* @return Dot
479510
*/
480511
public function setReference(array &$items)
481512
{
482513
$this->items = &$items;
514+
515+
return $this;
483516
}
484517

485518
/**

tests/DotTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ public function testAddArrayOfKeyValuePairs()
100100
$this->assertSame(['foobar' => 'baz', 'corge' => 'grault'], $dot->all());
101101
}
102102

103+
public function testAddReturnsDot()
104+
{
105+
$dot = new Dot;
106+
107+
$this->assertInstanceOf(Dot::class, $dot->add('foo', 'bar'));
108+
}
109+
103110
/*
104111
* --------------------------------------------------------------
105112
* All
@@ -151,6 +158,13 @@ public function testClearAll()
151158
$this->assertSame([], $dot->all());
152159
}
153160

161+
public function testClearReturnsDot()
162+
{
163+
$dot = new Dot();
164+
165+
$this->assertInstanceOf(Dot::class, $dot->clear());
166+
}
167+
154168
/*
155169
* --------------------------------------------------------------
156170
* Delete
@@ -181,6 +195,13 @@ public function testDeleteArrayOfKeys()
181195
$this->assertSame([], $dot->all());
182196
}
183197

198+
public function testDeleteReturnsDot()
199+
{
200+
$dot = new Dot(['foo' => 'bar']);
201+
202+
$this->assertInstanceOf(Dot::class, $dot->clear('foo'));
203+
}
204+
184205
/*
185206
* --------------------------------------------------------------
186207
* Flatten
@@ -343,6 +364,13 @@ public function testMergeDotObjectWithKey()
343364
$this->assertEquals('qux', $dot1->get('foo.bar'));
344365
}
345366

367+
public function testMergeReturnsDot()
368+
{
369+
$dot = new Dot(['foo' => ['bar' => 'baz']]);
370+
371+
$this->assertInstanceOf(Dot::class, $dot->merge(['foo' => ['bar' => 'qux']]));
372+
}
373+
346374
/*
347375
* --------------------------------------------------------------
348376
* Recursive merge
@@ -387,6 +415,16 @@ public function testRecursiveMergeDotObjectWithKey()
387415
$this->assertEquals('quuz', $dot1->get('foo.quux'));
388416
}
389417

418+
public function testRecursiveMergeReturnsDot()
419+
{
420+
$dot = new Dot(['foo' => ['bar' => 'baz']]);
421+
422+
$this->assertInstanceOf(
423+
Dot::class,
424+
$dot->mergeRecursive(['foo' => ['bar' => 'qux', 'quux' => 'quuz']])
425+
);
426+
}
427+
390428
/*
391429
* --------------------------------------------------------------
392430
* Recursive distinct merge
@@ -431,6 +469,16 @@ public function testRecursiveDistinctMergeDotObjectWithKey()
431469
$this->assertEquals('quuz', $dot1->get('foo.quux'));
432470
}
433471

472+
public function testRecursivDistincteMergeReturnsDot()
473+
{
474+
$dot = new Dot(['foo' => ['bar' => 'baz']]);
475+
476+
$this->assertInstanceOf(
477+
Dot::class,
478+
$dot->mergeRecursiveDistinct(['foo' => ['bar' => 'qux', 'quux' => 'quuz']])
479+
);
480+
}
481+
434482
/*
435483
* --------------------------------------------------------------
436484
* Pull
@@ -489,6 +537,13 @@ public function testPushValueToKey()
489537
$this->assertSame(['bar', 'baz'], $dot->get('foo'));
490538
}
491539

540+
public function testPushReturnsDot()
541+
{
542+
$dot = $dot = new Dot();
543+
544+
$this->assertInstanceOf(Dot::class, $dot->push('foo'));
545+
}
546+
492547
/*
493548
* --------------------------------------------------------------
494549
* Replace
@@ -529,6 +584,13 @@ public function testReplaceKeyWithDot()
529584
$this->assertEquals(['bar' => 'baz', 'qux' => 'corge'], $dot1->get('foo'));
530585
}
531586

587+
public function testReplaceReturnsDot()
588+
{
589+
$dot = new Dot(['foo' => ['bar' => 'baz']]);
590+
591+
$this->assertInstanceOf(Dot::class, $dot->replace(['foo' => ['qux' => 'quux']]));
592+
}
593+
532594
/*
533595
* --------------------------------------------------------------
534596
* Set
@@ -551,6 +613,13 @@ public function testSetArrayOfKeyValuePairs()
551613
$this->assertSame(['foo' => 'bar', 'baz' => 'qux'], $dot->all());
552614
}
553615

616+
public function testSetReturnsDot()
617+
{
618+
$dot = new Dot();
619+
620+
$this->assertInstanceOf(Dot::class, $dot->set('foo.bar', 'baz'));
621+
}
622+
554623
/*
555624
* --------------------------------------------------------------
556625
* Set array
@@ -565,6 +634,13 @@ public function testSetArray()
565634
$this->assertSame(['foo' => 'bar'], $dot->all());
566635
}
567636

637+
public function testSetArrayReturnsDot()
638+
{
639+
$dot = new Dot();
640+
641+
$this->assertInstanceOf(Dot::class, $dot->setArray(['foo' => 'bar']));
642+
}
643+
568644
/*
569645
* --------------------------------------------------------------
570646
* Set reference
@@ -581,6 +657,14 @@ public function testSetReference()
581657
$this->assertEquals('baz', $items['foo']);
582658
}
583659

660+
public function testSetReferenceReturnsDot()
661+
{
662+
$dot = new Dot();
663+
$items = ['foo' => 'bar'];
664+
665+
$this->assertInstanceOf(Dot::class, $dot->setReference($items));
666+
}
667+
584668
/*
585669
* --------------------------------------------------------------
586670
* ArrayAccess interface

0 commit comments

Comments
 (0)