Skip to content

Commit 553697e

Browse files
vanodeviumadbario
authored andcommitted
Added chain support
1 parent 3857c8c commit 553697e

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public function testAddKeyValuePair()
7979
$dot->add('foo.bar', 'baz');
8080

8181
$this->assertEquals('baz', $dot->get('foo.bar'));
82+
$this->assertInstanceOf(Dot::class, $dot);
8283
}
8384

8485
public function testAddValueToExistingKey()
@@ -125,6 +126,7 @@ public function testClearKey()
125126
$dot->clear('foo.bar');
126127

127128
$this->assertSame([], $dot->get('foo.bar'));
129+
$this->assertInstanceOf(Dot::class, $dot);
128130
}
129131

130132
public function testClearNonExistingKey()
@@ -163,6 +165,7 @@ public function testDeleteKey()
163165
$dot->delete('foo.bar');
164166

165167
$this->assertFalse($dot->has('foo.bar'));
168+
$this->assertInstanceOf(Dot::class, $dot);
166169
}
167170

168171
public function testDeleteNonExistingKey()
@@ -315,6 +318,7 @@ public function testMergeArrayWithDot()
315318
$dot->merge(['foo' => ['bar' => 'qux']]);
316319

317320
$this->assertEquals('qux', $dot->get('foo.bar'));
321+
$this->assertInstanceOf(Dot::class, $dot);
318322
}
319323

320324
public function testMergeArrayWithKey()
@@ -356,6 +360,7 @@ public function testRecursiveMergeArrayWithDot()
356360

357361
$this->assertEquals(['baz', 'qux'], $dot->get('foo.bar'));
358362
$this->assertEquals('quuz', $dot->get('foo.quux'));
363+
$this->assertInstanceOf(Dot::class, $dot);
359364
}
360365

361366
public function testRecursiveMergeArrayWithKey()
@@ -400,6 +405,7 @@ public function testRecursiveDistinctMergeArrayWithDot()
400405

401406
$this->assertEquals('qux', $dot->get('foo.bar'));
402407
$this->assertEquals('quuz', $dot->get('foo.quux'));
408+
$this->assertInstanceOf(Dot::class, $dot);
403409
}
404410

405411
public function testRecursiveDistinctMergeArrayWithKey()
@@ -479,6 +485,7 @@ public function testPushValue()
479485
$dot->push('foo');
480486

481487
$this->assertEquals('foo', $dot->get(0));
488+
$this->assertInstanceOf(Dot::class, $dot);
482489
}
483490

484491
public function testPushValueToKey()
@@ -501,6 +508,7 @@ public function testReplaceWithArray()
501508
$dot->replace(['foo' => ['qux' => 'quux']]);
502509

503510
$this->assertEquals(['qux' => 'quux'], $dot->get('foo'));
511+
$this->assertInstanceOf(Dot::class, $dot);
504512
}
505513

506514
public function testReplaceKeyWithArray()
@@ -541,6 +549,7 @@ public function testSetKeyValuePair()
541549
$dot->set('foo.bar', 'baz');
542550

543551
$this->assertEquals('baz', $dot->get('foo.bar'));
552+
$this->assertInstanceOf(Dot::class, $dot);
544553
}
545554

546555
public function testSetArrayOfKeyValuePairs()
@@ -563,6 +572,7 @@ public function testSetArray()
563572
$dot->setArray(['foo' => 'bar']);
564573

565574
$this->assertSame(['foo' => 'bar'], $dot->all());
575+
$this->assertInstanceOf(Dot::class, $dot);
566576
}
567577

568578
/*
@@ -579,6 +589,7 @@ public function testSetReference()
579589
$dot->set('foo', 'baz');
580590

581591
$this->assertEquals('baz', $items['foo']);
592+
$this->assertInstanceOf(Dot::class, $dot);
582593
}
583594

584595
/*

0 commit comments

Comments
 (0)