Fix not being able to use a class as the value type, also added CLOSURE constant value type

This commit is contained in:
R. Eric Wheeler 2021-03-04 10:51:09 -08:00
parent 8083f7e393
commit b0002ef7f7
3 changed files with 67 additions and 38 deletions

72
composer.lock generated
View File

@ -2657,16 +2657,16 @@
}, },
{ {
"name": "symfony/console", "name": "symfony/console",
"version": "v5.2.3", "version": "v5.2.4",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/console.git", "url": "https://github.com/symfony/console.git",
"reference": "89d4b176d12a2946a1ae4e34906a025b7b6b135a" "reference": "d6d0cc30d8c0fda4e7b213c20509b0159a8f4556"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/console/zipball/89d4b176d12a2946a1ae4e34906a025b7b6b135a", "url": "https://api.github.com/repos/symfony/console/zipball/d6d0cc30d8c0fda4e7b213c20509b0159a8f4556",
"reference": "89d4b176d12a2946a1ae4e34906a025b7b6b135a", "reference": "d6d0cc30d8c0fda4e7b213c20509b0159a8f4556",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -2734,7 +2734,7 @@
"terminal" "terminal"
], ],
"support": { "support": {
"source": "https://github.com/symfony/console/tree/v5.2.3" "source": "https://github.com/symfony/console/tree/v5.2.4"
}, },
"funding": [ "funding": [
{ {
@ -2750,7 +2750,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2021-01-28T22:06:19+00:00" "time": "2021-02-23T10:08:49+00:00"
}, },
{ {
"name": "symfony/deprecation-contracts", "name": "symfony/deprecation-contracts",
@ -2821,16 +2821,16 @@
}, },
{ {
"name": "symfony/event-dispatcher", "name": "symfony/event-dispatcher",
"version": "v5.2.3", "version": "v5.2.4",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/event-dispatcher.git", "url": "https://github.com/symfony/event-dispatcher.git",
"reference": "4f9760f8074978ad82e2ce854dff79a71fe45367" "reference": "d08d6ec121a425897951900ab692b612a61d6240"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/4f9760f8074978ad82e2ce854dff79a71fe45367", "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/d08d6ec121a425897951900ab692b612a61d6240",
"reference": "4f9760f8074978ad82e2ce854dff79a71fe45367", "reference": "d08d6ec121a425897951900ab692b612a61d6240",
"shasum": "" "shasum": ""
}, },
"require": { "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", "description": "Provides tools that allow your application components to communicate with each other by dispatching events and listening to them",
"homepage": "https://symfony.com", "homepage": "https://symfony.com",
"support": { "support": {
"source": "https://github.com/symfony/event-dispatcher/tree/v5.2.3" "source": "https://github.com/symfony/event-dispatcher/tree/v5.2.4"
}, },
"funding": [ "funding": [
{ {
@ -2902,7 +2902,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2021-01-27T10:36:42+00:00" "time": "2021-02-18T17:12:37+00:00"
}, },
{ {
"name": "symfony/event-dispatcher-contracts", "name": "symfony/event-dispatcher-contracts",
@ -2985,16 +2985,16 @@
}, },
{ {
"name": "symfony/filesystem", "name": "symfony/filesystem",
"version": "v5.2.3", "version": "v5.2.4",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/filesystem.git", "url": "https://github.com/symfony/filesystem.git",
"reference": "262d033b57c73e8b59cd6e68a45c528318b15038" "reference": "710d364200997a5afde34d9fe57bd52f3cc1e108"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/filesystem/zipball/262d033b57c73e8b59cd6e68a45c528318b15038", "url": "https://api.github.com/repos/symfony/filesystem/zipball/710d364200997a5afde34d9fe57bd52f3cc1e108",
"reference": "262d033b57c73e8b59cd6e68a45c528318b15038", "reference": "710d364200997a5afde34d9fe57bd52f3cc1e108",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -3027,7 +3027,7 @@
"description": "Provides basic utilities for the filesystem", "description": "Provides basic utilities for the filesystem",
"homepage": "https://symfony.com", "homepage": "https://symfony.com",
"support": { "support": {
"source": "https://github.com/symfony/filesystem/tree/v5.2.3" "source": "https://github.com/symfony/filesystem/tree/v5.2.4"
}, },
"funding": [ "funding": [
{ {
@ -3043,20 +3043,20 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2021-01-27T10:01:46+00:00" "time": "2021-02-12T10:38:38+00:00"
}, },
{ {
"name": "symfony/finder", "name": "symfony/finder",
"version": "v5.2.3", "version": "v5.2.4",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/finder.git", "url": "https://github.com/symfony/finder.git",
"reference": "4adc8d172d602008c204c2e16956f99257248e03" "reference": "0d639a0943822626290d169965804f79400e6a04"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/finder/zipball/4adc8d172d602008c204c2e16956f99257248e03", "url": "https://api.github.com/repos/symfony/finder/zipball/0d639a0943822626290d169965804f79400e6a04",
"reference": "4adc8d172d602008c204c2e16956f99257248e03", "reference": "0d639a0943822626290d169965804f79400e6a04",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -3088,7 +3088,7 @@
"description": "Finds files and directories via an intuitive fluent interface", "description": "Finds files and directories via an intuitive fluent interface",
"homepage": "https://symfony.com", "homepage": "https://symfony.com",
"support": { "support": {
"source": "https://github.com/symfony/finder/tree/v5.2.3" "source": "https://github.com/symfony/finder/tree/v5.2.4"
}, },
"funding": [ "funding": [
{ {
@ -3104,11 +3104,11 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2021-01-28T22:06:19+00:00" "time": "2021-02-15T18:55:04+00:00"
}, },
{ {
"name": "symfony/options-resolver", "name": "symfony/options-resolver",
"version": "v5.2.3", "version": "v5.2.4",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/options-resolver.git", "url": "https://github.com/symfony/options-resolver.git",
@ -3157,7 +3157,7 @@
"options" "options"
], ],
"support": { "support": {
"source": "https://github.com/symfony/options-resolver/tree/v5.2.3" "source": "https://github.com/symfony/options-resolver/tree/v5.2.4"
}, },
"funding": [ "funding": [
{ {
@ -3724,7 +3724,7 @@
}, },
{ {
"name": "symfony/process", "name": "symfony/process",
"version": "v5.2.3", "version": "v5.2.4",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/process.git", "url": "https://github.com/symfony/process.git",
@ -3766,7 +3766,7 @@
"description": "Executes commands in sub-processes", "description": "Executes commands in sub-processes",
"homepage": "https://symfony.com", "homepage": "https://symfony.com",
"support": { "support": {
"source": "https://github.com/symfony/process/tree/v5.2.3" "source": "https://github.com/symfony/process/tree/v5.2.4"
}, },
"funding": [ "funding": [
{ {
@ -3865,7 +3865,7 @@
}, },
{ {
"name": "symfony/stopwatch", "name": "symfony/stopwatch",
"version": "v5.2.3", "version": "v5.2.4",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/stopwatch.git", "url": "https://github.com/symfony/stopwatch.git",
@ -3907,7 +3907,7 @@
"description": "Provides a way to profile code", "description": "Provides a way to profile code",
"homepage": "https://symfony.com", "homepage": "https://symfony.com",
"support": { "support": {
"source": "https://github.com/symfony/stopwatch/tree/v5.2.3" "source": "https://github.com/symfony/stopwatch/tree/v5.2.4"
}, },
"funding": [ "funding": [
{ {
@ -3927,16 +3927,16 @@
}, },
{ {
"name": "symfony/string", "name": "symfony/string",
"version": "v5.2.3", "version": "v5.2.4",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/string.git", "url": "https://github.com/symfony/string.git",
"reference": "c95468897f408dd0aca2ff582074423dd0455122" "reference": "4e78d7d47061fa183639927ec40d607973699609"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/string/zipball/c95468897f408dd0aca2ff582074423dd0455122", "url": "https://api.github.com/repos/symfony/string/zipball/4e78d7d47061fa183639927ec40d607973699609",
"reference": "c95468897f408dd0aca2ff582074423dd0455122", "reference": "4e78d7d47061fa183639927ec40d607973699609",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -3990,7 +3990,7 @@
"utf8" "utf8"
], ],
"support": { "support": {
"source": "https://github.com/symfony/string/tree/v5.2.3" "source": "https://github.com/symfony/string/tree/v5.2.4"
}, },
"funding": [ "funding": [
{ {
@ -4006,7 +4006,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2021-01-25T15:14:59+00:00" "time": "2021-02-16T10:20:28+00:00"
}, },
{ {
"name": "theseer/tokenizer", "name": "theseer/tokenizer",

View File

@ -35,6 +35,8 @@ use function is_int;
use function is_object; use function is_object;
use function is_string; use function is_string;
use function mb_strlen; use function mb_strlen;
use function strtolower;
use Olivebbs\Map\Exception\InvalidArgumentException; use Olivebbs\Map\Exception\InvalidArgumentException;
use TypeError; use TypeError;
use ValueError; use ValueError;
@ -42,6 +44,7 @@ use ValueError;
class GenericMap implements ArrayAccess, Countable class GenericMap implements ArrayAccess, Countable
{ {
public const OBJECT = 'object'; public const OBJECT = 'object';
public const CALLABLE = 'callable';
public const ARRAY = 'array'; public const ARRAY = 'array';
public const INT = 'int'; public const INT = 'int';
public const INTEGER = 'integer'; public const INTEGER = 'integer';
@ -55,8 +58,16 @@ class GenericMap implements ArrayAccess, Countable
public function __construct(?string $keyType = null, ?string $valueType = null) public function __construct(?string $keyType = null, ?string $valueType = null)
{ {
$this->keyType = strtolower($keyType ?? self::ANY); $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)) { if (!$this->isValidKeyType($this->keyType)) {
throw new InvalidArgumentException(sprintf('Invalid key type (%s)', $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 private function isValidValueType(string $type): bool
{ {
if(class_exists($type)) {
return true;
}
return in_array($type, [ return in_array($type, [
self::OBJECT, self::OBJECT,
self::CALLABLE,
self::ARRAY, self::ARRAY,
self::INT, self::INT,
self::INTEGER, self::INTEGER,

View File

@ -30,6 +30,7 @@ namespace Olivebbs\Tests\Map;
use Olivebbs\Map\Exception\InvalidArgumentException; use Olivebbs\Map\Exception\InvalidArgumentException;
use Olivebbs\Map\GenericMap; use Olivebbs\Map\GenericMap;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use ValueError;
class GenericMapTest extends TestCase class GenericMapTest extends TestCase
{ {
@ -118,7 +119,7 @@ class GenericMapTest extends TestCase
public function testOffsetSetThrowsValueErrorException(): void public function testOffsetSetThrowsValueErrorException(): void
{ {
$genericMap = $this->getGenericMap(); $genericMap = $this->getGenericMap();
$this->expectException(\ValueError::class); $this->expectException(ValueError::class);
$genericMap[0] = 'string'; $genericMap[0] = 'string';
} }
@ -146,6 +147,18 @@ class GenericMapTest extends TestCase
$genericMap = new GenericMap(GenericMap::CHAR, 'test'); $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 private function resetGenericMap(): void
{ {
$this->genericMap = $this->getGenericMap(); $this->genericMap = $this->getGenericMap();