From 5c6ce9770d484da55ee225c3ffc92e192269afc1 Mon Sep 17 00:00:00 2001 From: Robin de Graaf Date: Mon, 9 Mar 2020 11:00:14 +0000 Subject: [PATCH] Add getMultiple() --- CHANGELOG.md | 10 ++++ src/BaseCollection.php | 19 +++++++ tests/GetSetTest.php | 77 +++++++++++++++++++++-------- tests/InputStreamCollectionTest.php | 11 +++-- tests/SimpleCollectionsTest.php | 17 ++++--- 5 files changed, 100 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f101e3b..322a688 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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_ diff --git a/src/BaseCollection.php b/src/BaseCollection.php index fd5745f..d583b59 100644 --- a/src/BaseCollection.php +++ b/src/BaseCollection.php @@ -7,6 +7,9 @@ abstract class BaseCollection { + /** + * @var array + */ protected $localValues = []; public function getAll(): array @@ -29,6 +32,7 @@ public function getAll(): array public function getAllAndClear(): array { $data = $this->getAll(); + $this->clear(); return $data; @@ -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)) { @@ -75,6 +91,7 @@ public function set(string $key, $value): void if (!isset($resource[$key]) || !is_array($resource[$key])) { $resource[$key] = []; } + $resource = &$resource[$key]; } @@ -120,6 +137,7 @@ public function remove(string $key): void $key )); } + if ($index < (count($keys) - 1)) { $resource = &$resource[$key]; } @@ -145,6 +163,7 @@ public function has(string $key): bool if (!isset($resource[$key])) { return false; } + $resource = &$resource[$key]; } diff --git a/tests/GetSetTest.php b/tests/GetSetTest.php index d595d7e..865ce9d 100644 --- a/tests/GetSetTest.php +++ b/tests/GetSetTest.php @@ -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 @@ -23,7 +24,7 @@ public function setUp() }; } - public function testLocalResourceGetSet() + public function testLocalResourceGetSet(): void { $globalsBefore = $GLOBALS; @@ -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 @@ -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."); @@ -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."); @@ -83,7 +84,7 @@ public function testNoResourceInterfaceSetThrowsExceptionOnSetAll() $getSet->setAll([]); } - public function testSetAllGetAll() + public function testSetAllGetAll(): void { $this->getSet->setAll([ 'key1' => 'yo1', @@ -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', @@ -117,7 +151,7 @@ public function testGetAllAndClear() self::assertSame([], $this->getSet->getAll()); } - public function testGetAndRemove() + public function testGetAndRemove(): void { $this->getSet->setAll([ 'key1' => 'yo1', @@ -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'); @@ -168,7 +203,7 @@ public function testSetGetSpecificAndGetAll() ); } - public function testSetAllVersusSetMany() + public function testSetAllVersusSetMany(): void { self::assertCount(0, $this->getSet->getAll()); @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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(); diff --git a/tests/InputStreamCollectionTest.php b/tests/InputStreamCollectionTest.php index 18c8b39..75a099b 100644 --- a/tests/InputStreamCollectionTest.php +++ b/tests/InputStreamCollectionTest.php @@ -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'."); @@ -18,7 +19,7 @@ public function testInputStreamThrowsExceptionIfSourceUnreadable() }; } - public function testJsonParsedCorrectly() + public function testJsonParsedCorrectly(): void { $inputStream = new class extends InputStreamCollection { @@ -33,7 +34,7 @@ public function testJsonParsedCorrectly() ); } - public function testParameterStringParsedCorrectly() + public function testParameterStringParsedCorrectly(): void { $inputStream = new class extends InputStreamCollection { @@ -48,7 +49,7 @@ public function testParameterStringParsedCorrectly() ); } - public function testGetAndGetAllMethodsAllWork() + public function testGetAndGetAllMethodsAllWork(): void { $inputStream = new class extends InputStreamCollection { diff --git a/tests/SimpleCollectionsTest.php b/tests/SimpleCollectionsTest.php index 5cd9b70..0b1c45e 100644 --- a/tests/SimpleCollectionsTest.php +++ b/tests/SimpleCollectionsTest.php @@ -9,10 +9,11 @@ use Parable\GetSet\PostCollection; use Parable\GetSet\ServerCollection; use Parable\GetSet\SessionCollection; +use PHPUnit\Framework\TestCase; -class SimpleCollectionsTest extends \PHPUnit\Framework\TestCase +class SimpleCollectionsTest extends TestCase { - public function testCookieCollection() + public function testCookieCollection(): void { $_COOKIE['hello'] = 'yay'; @@ -29,7 +30,7 @@ public function testCookieCollection() ); } - public function testFilesCollection() + public function testFilesCollection(): void { $files = new FilesCollection(); @@ -43,7 +44,7 @@ public function testFilesCollection() ); } - public function testGetCollection() + public function testGetCollection(): void { $get = new GetCollection(); @@ -57,7 +58,7 @@ public function testGetCollection() ); } - public function testInternalCollection() + public function testInternalCollection(): void { $internal = new DataCollection(); @@ -71,7 +72,7 @@ public function testInternalCollection() ); } - public function testPostCollection() + public function testPostCollection(): void { $post = new PostCollection(); @@ -85,7 +86,7 @@ public function testPostCollection() ); } - public function testServerCollection() + public function testServerCollection(): void { $server = new ServerCollection(); @@ -97,7 +98,7 @@ public function testServerCollection() self::assertArrayHasKey('test', $server->getAll()); } - public function testSessionCollection() + public function testSessionCollection(): void { $session = new SessionCollection();