From b0002ef7f721e2edc6b5e7d331f2e4e0332b99c9 Mon Sep 17 00:00:00 2001 From: "R. Eric Wheeler" Date: Thu, 4 Mar 2021 10:51:09 -0800 Subject: [PATCH] Fix not being able to use a class as the value type, also added CLOSURE constant value type --- composer.lock | 72 ++++++++++++++++----------------- src/Olivebbs/Map/GenericMap.php | 18 ++++++++- tests/GenericMapTest.php | 15 ++++++- 3 files changed, 67 insertions(+), 38 deletions(-) diff --git a/composer.lock b/composer.lock index 815ce2e..6c283d5 100644 --- a/composer.lock +++ b/composer.lock @@ -2657,16 +2657,16 @@ }, { "name": "symfony/console", - "version": "v5.2.3", + "version": "v5.2.4", "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "89d4b176d12a2946a1ae4e34906a025b7b6b135a" + "reference": "d6d0cc30d8c0fda4e7b213c20509b0159a8f4556" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/89d4b176d12a2946a1ae4e34906a025b7b6b135a", - "reference": "89d4b176d12a2946a1ae4e34906a025b7b6b135a", + "url": "https://api.github.com/repos/symfony/console/zipball/d6d0cc30d8c0fda4e7b213c20509b0159a8f4556", + "reference": "d6d0cc30d8c0fda4e7b213c20509b0159a8f4556", "shasum": "" }, "require": { @@ -2734,7 +2734,7 @@ "terminal" ], "support": { - "source": "https://github.com/symfony/console/tree/v5.2.3" + "source": "https://github.com/symfony/console/tree/v5.2.4" }, "funding": [ { @@ -2750,7 +2750,7 @@ "type": "tidelift" } ], - "time": "2021-01-28T22:06:19+00:00" + "time": "2021-02-23T10:08:49+00:00" }, { "name": "symfony/deprecation-contracts", @@ -2821,16 +2821,16 @@ }, { "name": "symfony/event-dispatcher", - "version": "v5.2.3", + "version": "v5.2.4", "source": { "type": "git", "url": "https://github.com/symfony/event-dispatcher.git", - "reference": "4f9760f8074978ad82e2ce854dff79a71fe45367" + "reference": "d08d6ec121a425897951900ab692b612a61d6240" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/4f9760f8074978ad82e2ce854dff79a71fe45367", - "reference": "4f9760f8074978ad82e2ce854dff79a71fe45367", + "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/d08d6ec121a425897951900ab692b612a61d6240", + "reference": "d08d6ec121a425897951900ab692b612a61d6240", "shasum": "" }, "require": { @@ -2886,7 +2886,7 @@ "description": "Provides tools that allow your application components to communicate with each other by dispatching events and listening to them", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/event-dispatcher/tree/v5.2.3" + "source": "https://github.com/symfony/event-dispatcher/tree/v5.2.4" }, "funding": [ { @@ -2902,7 +2902,7 @@ "type": "tidelift" } ], - "time": "2021-01-27T10:36:42+00:00" + "time": "2021-02-18T17:12:37+00:00" }, { "name": "symfony/event-dispatcher-contracts", @@ -2985,16 +2985,16 @@ }, { "name": "symfony/filesystem", - "version": "v5.2.3", + "version": "v5.2.4", "source": { "type": "git", "url": "https://github.com/symfony/filesystem.git", - "reference": "262d033b57c73e8b59cd6e68a45c528318b15038" + "reference": "710d364200997a5afde34d9fe57bd52f3cc1e108" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/filesystem/zipball/262d033b57c73e8b59cd6e68a45c528318b15038", - "reference": "262d033b57c73e8b59cd6e68a45c528318b15038", + "url": "https://api.github.com/repos/symfony/filesystem/zipball/710d364200997a5afde34d9fe57bd52f3cc1e108", + "reference": "710d364200997a5afde34d9fe57bd52f3cc1e108", "shasum": "" }, "require": { @@ -3027,7 +3027,7 @@ "description": "Provides basic utilities for the filesystem", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/filesystem/tree/v5.2.3" + "source": "https://github.com/symfony/filesystem/tree/v5.2.4" }, "funding": [ { @@ -3043,20 +3043,20 @@ "type": "tidelift" } ], - "time": "2021-01-27T10:01:46+00:00" + "time": "2021-02-12T10:38:38+00:00" }, { "name": "symfony/finder", - "version": "v5.2.3", + "version": "v5.2.4", "source": { "type": "git", "url": "https://github.com/symfony/finder.git", - "reference": "4adc8d172d602008c204c2e16956f99257248e03" + "reference": "0d639a0943822626290d169965804f79400e6a04" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/finder/zipball/4adc8d172d602008c204c2e16956f99257248e03", - "reference": "4adc8d172d602008c204c2e16956f99257248e03", + "url": "https://api.github.com/repos/symfony/finder/zipball/0d639a0943822626290d169965804f79400e6a04", + "reference": "0d639a0943822626290d169965804f79400e6a04", "shasum": "" }, "require": { @@ -3088,7 +3088,7 @@ "description": "Finds files and directories via an intuitive fluent interface", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/finder/tree/v5.2.3" + "source": "https://github.com/symfony/finder/tree/v5.2.4" }, "funding": [ { @@ -3104,11 +3104,11 @@ "type": "tidelift" } ], - "time": "2021-01-28T22:06:19+00:00" + "time": "2021-02-15T18:55:04+00:00" }, { "name": "symfony/options-resolver", - "version": "v5.2.3", + "version": "v5.2.4", "source": { "type": "git", "url": "https://github.com/symfony/options-resolver.git", @@ -3157,7 +3157,7 @@ "options" ], "support": { - "source": "https://github.com/symfony/options-resolver/tree/v5.2.3" + "source": "https://github.com/symfony/options-resolver/tree/v5.2.4" }, "funding": [ { @@ -3724,7 +3724,7 @@ }, { "name": "symfony/process", - "version": "v5.2.3", + "version": "v5.2.4", "source": { "type": "git", "url": "https://github.com/symfony/process.git", @@ -3766,7 +3766,7 @@ "description": "Executes commands in sub-processes", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/process/tree/v5.2.3" + "source": "https://github.com/symfony/process/tree/v5.2.4" }, "funding": [ { @@ -3865,7 +3865,7 @@ }, { "name": "symfony/stopwatch", - "version": "v5.2.3", + "version": "v5.2.4", "source": { "type": "git", "url": "https://github.com/symfony/stopwatch.git", @@ -3907,7 +3907,7 @@ "description": "Provides a way to profile code", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/stopwatch/tree/v5.2.3" + "source": "https://github.com/symfony/stopwatch/tree/v5.2.4" }, "funding": [ { @@ -3927,16 +3927,16 @@ }, { "name": "symfony/string", - "version": "v5.2.3", + "version": "v5.2.4", "source": { "type": "git", "url": "https://github.com/symfony/string.git", - "reference": "c95468897f408dd0aca2ff582074423dd0455122" + "reference": "4e78d7d47061fa183639927ec40d607973699609" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/string/zipball/c95468897f408dd0aca2ff582074423dd0455122", - "reference": "c95468897f408dd0aca2ff582074423dd0455122", + "url": "https://api.github.com/repos/symfony/string/zipball/4e78d7d47061fa183639927ec40d607973699609", + "reference": "4e78d7d47061fa183639927ec40d607973699609", "shasum": "" }, "require": { @@ -3990,7 +3990,7 @@ "utf8" ], "support": { - "source": "https://github.com/symfony/string/tree/v5.2.3" + "source": "https://github.com/symfony/string/tree/v5.2.4" }, "funding": [ { @@ -4006,7 +4006,7 @@ "type": "tidelift" } ], - "time": "2021-01-25T15:14:59+00:00" + "time": "2021-02-16T10:20:28+00:00" }, { "name": "theseer/tokenizer", diff --git a/src/Olivebbs/Map/GenericMap.php b/src/Olivebbs/Map/GenericMap.php index cd5e674..ae06c04 100644 --- a/src/Olivebbs/Map/GenericMap.php +++ b/src/Olivebbs/Map/GenericMap.php @@ -35,6 +35,8 @@ use function is_int; use function is_object; use function is_string; use function mb_strlen; +use function strtolower; + use Olivebbs\Map\Exception\InvalidArgumentException; use TypeError; use ValueError; @@ -42,6 +44,7 @@ use ValueError; class GenericMap implements ArrayAccess, Countable { public const OBJECT = 'object'; + public const CALLABLE = 'callable'; public const ARRAY = 'array'; public const INT = 'int'; public const INTEGER = 'integer'; @@ -55,8 +58,16 @@ class GenericMap implements ArrayAccess, Countable public function __construct(?string $keyType = null, ?string $valueType = null) { + + + $this->keyType = strtolower($keyType ?? self::ANY); - $this->valueType = strtolower($valueType ?? self::ANY); + + $this->valueType = $valueType ?? self::ANY; + + if(!class_exists($this->valueType)) { + $this->valueType = strtolower($this->valueType); + } if (!$this->isValidKeyType($this->keyType)) { throw new InvalidArgumentException(sprintf('Invalid key type (%s)', $this->keyType)); @@ -219,8 +230,13 @@ class GenericMap implements ArrayAccess, Countable private function isValidValueType(string $type): bool { + if(class_exists($type)) { + return true; + } + return in_array($type, [ self::OBJECT, + self::CALLABLE, self::ARRAY, self::INT, self::INTEGER, diff --git a/tests/GenericMapTest.php b/tests/GenericMapTest.php index c6192b0..1e045fb 100644 --- a/tests/GenericMapTest.php +++ b/tests/GenericMapTest.php @@ -30,6 +30,7 @@ namespace Olivebbs\Tests\Map; use Olivebbs\Map\Exception\InvalidArgumentException; use Olivebbs\Map\GenericMap; use PHPUnit\Framework\TestCase; +use ValueError; class GenericMapTest extends TestCase { @@ -118,7 +119,7 @@ class GenericMapTest extends TestCase public function testOffsetSetThrowsValueErrorException(): void { $genericMap = $this->getGenericMap(); - $this->expectException(\ValueError::class); + $this->expectException(ValueError::class); $genericMap[0] = 'string'; } @@ -146,6 +147,18 @@ class GenericMapTest extends TestCase $genericMap = new GenericMap(GenericMap::CHAR, 'test'); } + public function testUsingClassAsValue(): void + { + $genericMap = new GenericMap(GenericMap::INT, \SplObjectStorage::class); + $splObject = new \SplObjectStorage(); + $stdClass = new \stdClass(); + $splObject->attach($stdClass); + $genericMap[0] = $splObject; + self::assertSame($splObject, $genericMap[0]); + self::assertTrue($genericMap[0]->contains($stdClass)); + + } + private function resetGenericMap(): void { $this->genericMap = $this->getGenericMap();