Fix logic when using class or object as value type

This commit is contained in:
R. Eric Wheeler 2021-03-04 11:16:57 -08:00
parent b0002ef7f7
commit 5b57ce98c6
2 changed files with 22 additions and 19 deletions

View File

@ -31,13 +31,13 @@ use ArrayAccess;
use Countable; use Countable;
use Ds\Map; use Ds\Map;
use function is_array; use function is_array;
use function is_callable;
use function is_int; 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 strtolower;
use Olivebbs\Map\Exception\InvalidArgumentException; use Olivebbs\Map\Exception\InvalidArgumentException;
use function strtolower;
use TypeError; use TypeError;
use ValueError; use ValueError;
@ -56,18 +56,11 @@ class GenericMap implements ArrayAccess, Countable
protected string $keyType; protected string $keyType;
protected string $valueType; protected string $valueType;
public function __construct(?string $keyType = null, ?string $valueType = null) public function __construct(string $keyType = self::ANY, string $valueType = self::ANY)
{ {
$this->keyType = strtolower($keyType);
$this->valueType = class_exists($valueType) ? $valueType : strtolower($valueType);
$this->keyType = strtolower($keyType ?? 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));
@ -96,7 +89,7 @@ class GenericMap implements ArrayAccess, Countable
public function offsetExists($offset): bool public function offsetExists($offset): bool
{ {
if (is_array($offset) || is_object($offset)) { if (is_array($offset) || is_object($offset)) {
throw new InvalidArgumentException('Map Keys cannot be objects or arrays'); throw new InvalidArgumentException('Map keys cannot be objects or arrays');
} }
if (!$this->checkType($this->keyType, $offset)) { if (!$this->checkType($this->keyType, $offset)) {
@ -112,7 +105,7 @@ class GenericMap implements ArrayAccess, Countable
public function offsetGet($offset) public function offsetGet($offset)
{ {
if (is_array($offset) || is_object($offset)) { if (is_array($offset) || is_object($offset)) {
throw new InvalidArgumentException('Map Keys cannot be objects or arrays'); throw new InvalidArgumentException('Map keys cannot be objects or arrays');
} }
if (!$this->checkType($this->keyType, $offset)) { if (!$this->checkType($this->keyType, $offset)) {
@ -130,7 +123,7 @@ class GenericMap implements ArrayAccess, Countable
public function offsetSet($offset, $value): void public function offsetSet($offset, $value): void
{ {
if (is_array($offset) || is_object($offset)) { if (is_array($offset) || is_object($offset)) {
throw new InvalidArgumentException('Map Keys cannot be objects or arrays'); throw new InvalidArgumentException('Map keys cannot be objects or arrays');
} }
if (!$this->checkType($this->keyType, $offset)) { if (!$this->checkType($this->keyType, $offset)) {
@ -150,7 +143,7 @@ class GenericMap implements ArrayAccess, Countable
public function offsetUnset($offset): void public function offsetUnset($offset): void
{ {
if (is_array($offset) || is_object($offset)) { if (is_array($offset) || is_object($offset)) {
throw new InvalidArgumentException('Map Keys cannot be objects or arrays'); throw new InvalidArgumentException('Map keys cannot be objects or arrays');
} }
if (!$this->checkType($this->keyType, $offset)) { if (!$this->checkType($this->keyType, $offset)) {
@ -202,6 +195,8 @@ class GenericMap implements ArrayAccess, Countable
switch ($type) { switch ($type) {
case self::OBJECT: case self::OBJECT:
return is_object($var); return is_object($var);
case self::CALLABLE:
return is_callable($var);
case self::ARRAY: case self::ARRAY:
return is_array($var); return is_array($var);
case self::INT: case self::INT:
@ -210,7 +205,7 @@ class GenericMap implements ArrayAccess, Countable
case self::STRING: case self::STRING:
return is_string($var); return is_string($var);
case self::CHAR: case self::CHAR:
return is_string($var) && mb_strlen($var) === 1; return is_string($var) && \mb_strlen($var) === 1;
case self::ANY: case self::ANY:
return true; return true;
default: default:
@ -230,7 +225,7 @@ class GenericMap implements ArrayAccess, Countable
private function isValidValueType(string $type): bool private function isValidValueType(string $type): bool
{ {
if(class_exists($type)) { if (class_exists($type)) {
return true; return true;
} }

View File

@ -156,7 +156,15 @@ class GenericMapTest extends TestCase
$genericMap[0] = $splObject; $genericMap[0] = $splObject;
self::assertSame($splObject, $genericMap[0]); self::assertSame($splObject, $genericMap[0]);
self::assertTrue($genericMap[0]->contains($stdClass)); self::assertTrue($genericMap[0]->contains($stdClass));
}
public function testObjectCantBeUsedAsKeyWithAny(): void
{
$genericMap = new GenericMap(GenericMap::ANY, GenericMap::ANY);
$object = new \SplObjectStorage();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Map keys cannot be objects or arrays');
$genericMap[$object] = 1;
} }
private function resetGenericMap(): void private function resetGenericMap(): void