Skip to content

Commit

Permalink
Add getMultiple()
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin de Graaf committed Mar 9, 2020
1 parent e81f3b3 commit 5c6ce97
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 34 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Parable GetSet

## 0.1.4

_Changes_
- Added `getMultiple(string ...$keys): array` so you can request multiple keys in one go. The default value if not found is always `null`.

```php
// Example of getMultiple:
[$title, $content] = $postColection->getMultiple('title', 'content');
```

## 0.1.3

_Changes_
Expand Down
19 changes: 19 additions & 0 deletions src/BaseCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

abstract class BaseCollection
{
/**
* @var array
*/
protected $localValues = [];

public function getAll(): array
Expand All @@ -29,6 +32,7 @@ public function getAll(): array
public function getAllAndClear(): array
{
$data = $this->getAll();

$this->clear();

return $data;
Expand All @@ -44,12 +48,24 @@ public function get(string $key, $default = null)
if (!isset($resource[$key])) {
return $default;
}

$resource = &$resource[$key];
}

return $resource;
}

public function getMultiple(string ...$keys): array
{
$values = [];

foreach ($keys as $key) {
$values[] = $this->get($key);
}

return $values;
}

public function getAndRemove(string $key, $default = null)
{
if (!$this->has($key)) {
Expand All @@ -75,6 +91,7 @@ public function set(string $key, $value): void
if (!isset($resource[$key]) || !is_array($resource[$key])) {
$resource[$key] = [];
}

$resource = &$resource[$key];
}

Expand Down Expand Up @@ -120,6 +137,7 @@ public function remove(string $key): void
$key
));
}

if ($index < (count($keys) - 1)) {
$resource = &$resource[$key];
}
Expand All @@ -145,6 +163,7 @@ public function has(string $key): bool
if (!isset($resource[$key])) {
return false;
}

$resource = &$resource[$key];
}

Expand Down
77 changes: 56 additions & 21 deletions tests/GetSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
use Parable\GetSet\Exception;
use Parable\GetSet\Resource\GlobalResourceInterface;
use Parable\GetSet\Resource\LocalResourceInterface;
use PHPUnit\Framework\TestCase;

class GetSetTest extends \PHPUnit\Framework\TestCase
class GetSetTest extends TestCase
{
/**
* @var BaseCollection
Expand All @@ -23,7 +24,7 @@ public function setUp()
};
}

public function testLocalResourceGetSet()
public function testLocalResourceGetSet(): void
{
$globalsBefore = $GLOBALS;

Expand All @@ -40,7 +41,7 @@ public function testLocalResourceGetSet()
);
}

public function testGlobalResourceGetSet()
public function testGlobalResourceGetSet(): void
{
$getSet = new class extends BaseCollection implements GlobalResourceInterface {
public function getResource(): string
Expand All @@ -61,7 +62,7 @@ public function getResource(): string
self::assertSame('value', $GLOBALS['_TEST']['test']);
}

public function testNoResourceInterfaceSetThrowsExceptionOnGetAll()
public function testNoResourceInterfaceSetThrowsExceptionOnGetAll(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage("No resource interface implemented.");
Expand All @@ -72,7 +73,7 @@ public function testNoResourceInterfaceSetThrowsExceptionOnGetAll()
$getSet->getAll();
}

public function testNoResourceInterfaceSetThrowsExceptionOnSetAll()
public function testNoResourceInterfaceSetThrowsExceptionOnSetAll(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage("No resource interface implemented.");
Expand All @@ -83,7 +84,7 @@ public function testNoResourceInterfaceSetThrowsExceptionOnSetAll()
$getSet->setAll([]);
}

public function testSetAllGetAll()
public function testSetAllGetAll(): void
{
$this->getSet->setAll([
'key1' => 'yo1',
Expand All @@ -99,7 +100,40 @@ public function testSetAllGetAll()
);
}

public function testGetAllAndClear()
public function testGetMultiple(): void
{
$this->getSet->setAll([
'key1' => 'yo1',
'key2' => 'yo2',
'key3' => 'yo3',
]);

self::assertSame(
[
'yo1',
'yo3',
null,
],
$this->getSet->getMultiple('key1', 'key3', 'nope')
);
}

public function testGetMultipleCanBeListed(): void
{
$this->getSet->setAll([
'key1' => 'yo1',
'key2' => 'yo2',
'key3' => 'yo3',
]);

[$key1, $key3, $shouldBeNull] = $this->getSet->getMultiple('key1', 'key3', 'nope');

self::assertSame('yo1', $key1);
self::assertSame('yo3', $key3);
self::assertNull($shouldBeNull);
}

public function testGetAllAndClear(): void
{
$this->getSet->setAll([
'key1' => 'yo1',
Expand All @@ -117,7 +151,7 @@ public function testGetAllAndClear()
self::assertSame([], $this->getSet->getAll());
}

public function testGetAndRemove()
public function testGetAndRemove(): void
{
$this->getSet->setAll([
'key1' => 'yo1',
Expand All @@ -138,19 +172,20 @@ public function testGetAndRemove()
self::assertSame(1, $this->getSet->count());
}

public function testRemoveNonExistingKeyThrows()
public function testRemoveNonExistingKeyThrows(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage("Cannot remove non-existing value by key 'stuff'");

$this->getSet->remove('stuff');
}
public function testGetAndRemoveNonExistingKeyDoesNotThrow()

public function testGetAndRemoveNonExistingKeyDoesNotThrow(): void
{
self::assertNull($this->getSet->getAndRemove('stuff'));
}

public function testSetGetSpecificAndGetAll()
public function testSetGetSpecificAndGetAll(): void
{
$this->getSet->set('key1', 'yo1');
$this->getSet->set('key2', 'yo2');
Expand All @@ -168,7 +203,7 @@ public function testSetGetSpecificAndGetAll()
);
}

public function testSetAllVersusSetMany()
public function testSetAllVersusSetMany(): void
{
self::assertCount(0, $this->getSet->getAll());

Expand Down Expand Up @@ -219,7 +254,7 @@ public function testSetAllVersusSetMany()
);
}

public function testGetSetAndRemoveWithHierarchalKeys()
public function testGetSetAndRemoveWithHierarchalKeys(): void
{
$this->getSet->set('one', ['this' => 'should stay']);
$this->getSet->set('one.two.three.four', 'totally nested, yo');
Expand Down Expand Up @@ -284,7 +319,7 @@ public function testGetSetAndRemoveWithHierarchalKeys()
);
}

public function testRemoveHierarchalKey()
public function testRemoveHierarchalKey(): void
{
$this->getSet->set('one.two.three', 'totally');
$this->getSet->set('one.two.four', 'also');
Expand All @@ -307,7 +342,7 @@ public function testRemoveHierarchalKey()
self::assertTrue(is_array($this->getSet->get('one')));
}

public function testCountWithKey()
public function testCountWithKey(): void
{
$this->getSet->set('one.two.three', 'totally');
$this->getSet->set('one.two.four', 'also');
Expand All @@ -320,7 +355,7 @@ public function testCountWithKey()
self::assertSame(2, $this->getSet->count('one.two'));
}

public function testHas()
public function testHas(): void
{
$this->getSet->set('one.two.three', 'totally');

Expand All @@ -332,27 +367,27 @@ public function testHas()
self::assertFalse($this->getSet->has('random'));
}

public function testGetNonExistingKeyReturnsNullByDefault()
public function testGetNonExistingKeyReturnsNullByDefault(): void
{
self::assertNull($this->getSet->get('nope'));
}

public function testGetNonExistingKeyReturnsDefaultIfPassed()
public function testGetNonExistingKeyReturnsDefaultIfPassed(): void
{
self::assertSame('default', $this->getSet->get('nope', 'default'));
}

public function testGetAndRemoveNonExistingKeyReturnsNullByDefault()
public function testGetAndRemoveNonExistingKeyReturnsNullByDefault(): void
{
self::assertNull($this->getSet->getAndRemove('nope'));
}

public function testGetAndRemoveNonExistingKeyReturnsDefaultIfPassed()
public function testGetAndRemoveNonExistingKeyReturnsDefaultIfPassed(): void
{
self::assertSame('default', $this->getSet->getAndRemove('nope', 'default'));
}

public function testGlobalValuesAreSetGlobally()
public function testGlobalValuesAreSetGlobally(): void
{
$server = new ServerCollection();

Expand Down
11 changes: 6 additions & 5 deletions tests/InputStreamCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

use Parable\GetSet\InputStreamCollection;
use Parable\GetSet\Exception;
use PHPUnit\Framework\TestCase;

class InputStreamTest extends \PHPUnit\Framework\TestCase
class InputStreamTest extends TestCase
{
public function testInputStreamThrowsExceptionIfSourceUnreadable()
public function testInputStreamThrowsExceptionIfSourceUnreadable(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage("Could not read from input source 'This file definitely does not exist'.");
Expand All @@ -18,7 +19,7 @@ public function testInputStreamThrowsExceptionIfSourceUnreadable()
};
}

public function testJsonParsedCorrectly()
public function testJsonParsedCorrectly(): void
{
$inputStream = new class extends InputStreamCollection
{
Expand All @@ -33,7 +34,7 @@ public function testJsonParsedCorrectly()
);
}

public function testParameterStringParsedCorrectly()
public function testParameterStringParsedCorrectly(): void
{
$inputStream = new class extends InputStreamCollection
{
Expand All @@ -48,7 +49,7 @@ public function testParameterStringParsedCorrectly()
);
}

public function testGetAndGetAllMethodsAllWork()
public function testGetAndGetAllMethodsAllWork(): void
{
$inputStream = new class extends InputStreamCollection
{
Expand Down
Loading

0 comments on commit 5c6ce97

Please sign in to comment.