Skip to content

Commit 0e4e9da

Browse files
author
Robin de Graaf
committed
API consistency and default get value
1 parent 9c38f58 commit 0e4e9da

5 files changed

Lines changed: 56 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Parable GetSet
22

3+
## 0.1.1
4+
5+
_Changes_
6+
- Add `string` type-hinting to `$key` for `get()` and `getAndRemove()`, in line with the rest.
7+
- Add `$default = null` parameter to `get()`, so that the scenarios of 'not set' and 'set but `null`' can be distinguished between.
8+
39
## 0.1.0
410

511
_Changes_

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ dot-notation get/set logic.
7171

7272
- `getAll(): array` - returns all values currently stored
7373
- `getAllAndClear(): array` - returns all values currently stored and then clears them
74-
- `get(string $key): mixed` - returns value by key or `null` if not found
75-
- `getAndRemove(string $key): mixed` - returns value by key and then removes it, or `null` if not found
74+
- `get(string $key, $default = null): mixed` - returns value by key or `null` if not found
75+
- `getAndRemove(string $key, $default = null): mixed` - returns value by key and then removes it, or `null` if not found
7676
- `set(string $key, $value): void` - set a value by key
7777
- `setMany(array $values): void` - set all values passed one-by-one, overwriting pre-existing values
7878
- `setAll(array $values): void` - set all values, overwriting any pre-existing ones

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
}
1414
],
1515
"require": {
16-
"php": ">=7.1"
16+
"php": ">=7.1",
17+
"ext-json": "*"
1718
},
1819
"require-dev": {
1920
"phpunit/phpunit": "^7.0"

src/BaseCollection.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,23 @@ public function getAllAndClear(): array
3232
return $data;
3333
}
3434

35-
public function get($key)
35+
public function get(string $key, $default = null)
3636
{
3737
$keys = explode('.', $key);
3838

3939
$resource = $this->getAll();
4040

4141
foreach ($keys as $key) {
4242
if (!isset($resource[$key])) {
43-
return null;
43+
return $default;
4444
}
4545
$resource = &$resource[$key];
4646
}
4747

4848
return $resource;
4949
}
5050

51-
public function getAndRemove($key)
51+
public function getAndRemove(string $key)
5252
{
5353
$data = $this->get($key);
5454
$this->remove($key);
@@ -100,13 +100,6 @@ public function setAll(array $values): void
100100

101101
public function remove(string $key): void
102102
{
103-
if (!$this->has($key)) {
104-
throw new Exception(sprintf(
105-
"Cannot remove non-existing value by key '%s'",
106-
$key
107-
));
108-
}
109-
110103
$keys = explode('.', $key);
111104

112105
$data = $this->getAll();
@@ -115,7 +108,10 @@ public function remove(string $key): void
115108

116109
foreach ($keys as $index => $key) {
117110
if (!isset($resource[$key])) {
118-
return;
111+
throw new Exception(sprintf(
112+
"Cannot remove non-existing value by key '%s'",
113+
$key
114+
));
119115
}
120116
if ($index < (count($keys) - 1)) {
121117
$resource = &$resource[$key];
@@ -148,8 +144,14 @@ public function has(string $key): bool
148144
return true;
149145
}
150146

151-
public function count(): int
147+
public function count(string $key = null): int
152148
{
153-
return count($this->getAll());
149+
if ($key === null) {
150+
$data = $this->getAll();
151+
} else {
152+
$data = $this->get($key, []);
153+
}
154+
155+
return count($data);
154156
}
155157
}

tests/GetSetTest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,41 @@ public function testRemoveHierarchalKey()
313313
self::assertTrue(is_array($this->getSet->get('one')));
314314
}
315315

316-
public function testGetNonExistingKeyReturnsNull()
316+
public function testCountWithKey()
317+
{
318+
$this->getSet->set('one.two.three', 'totally');
319+
$this->getSet->set('one.two.four', 'also');
320+
321+
// count only counts the top level, which is 1: ['one']
322+
self::assertSame(1, $this->getSet->count());
323+
// this level is 1: ['two']
324+
self::assertSame(1, $this->getSet->count('one'));
325+
// but 'two' contains both 'three' and 'four'
326+
self::assertSame(2, $this->getSet->count('one.two'));
327+
}
328+
329+
public function testHas()
330+
{
331+
$this->getSet->set('one.two.three', 'totally');
332+
333+
self::assertTrue($this->getSet->has('one'));
334+
self::assertTrue($this->getSet->has('one.two'));
335+
self::assertTrue($this->getSet->has('one.two.three'));
336+
337+
self::assertFalse($this->getSet->has('one.two.three.totally'));
338+
self::assertFalse($this->getSet->has('random'));
339+
}
340+
341+
public function testGetNonExistingKeyReturnsNullByDefault()
317342
{
318343
self::assertNull($this->getSet->get('nope'));
319344
}
320345

346+
public function testGetNonExistingKeyReturnsDefaultIfPassed()
347+
{
348+
self::assertSame('default', $this->getSet->get('nope', 'default'));
349+
}
350+
321351
public function testGlobalValuesAreSetGlobally()
322352
{
323353
$server = new ServerCollection();

0 commit comments

Comments
 (0)