Merge branch '1.x' into 'master'
1.x See merge request olive/PHP/map!4
This commit is contained in:
commit
ac6038c213
|
@ -15,7 +15,7 @@ before_script:
|
||||||
apt-get update -yqq && apt-get upgrade -yqq && apt-get install -yqq git libzip-dev unzip zip libpcre3-dev && docker-php-ext-install zip && pecl install xdebug && docker-php-ext-enable xdebug
|
apt-get update -yqq && apt-get upgrade -yqq && apt-get install -yqq git libzip-dev unzip zip libpcre3-dev && docker-php-ext-install zip && pecl install xdebug && docker-php-ext-enable xdebug
|
||||||
fi
|
fi
|
||||||
- >
|
- >
|
||||||
if [ "$CI_JOB_NAME" == "test:7.4" ] || [ "$CI_JOB_NAME" == "test:8.0" ]; then
|
if [ "$CI_JOB_NAME" == "test:8.1" ]; then
|
||||||
pecl install ds && docker-php-ext-enable ds
|
pecl install ds && docker-php-ext-enable ds
|
||||||
fi
|
fi
|
||||||
- >
|
- >
|
||||||
|
@ -23,36 +23,20 @@ before_script:
|
||||||
curl -sS https://getcomposer.org/installer | php
|
curl -sS https://getcomposer.org/installer | php
|
||||||
php composer.phar install
|
php composer.phar install
|
||||||
fi
|
fi
|
||||||
test:7.4:
|
test:8.1:
|
||||||
only:
|
only:
|
||||||
- branches
|
- branches
|
||||||
tags:
|
tags:
|
||||||
- default
|
- default
|
||||||
image: php:7.4
|
image: php:8.1
|
||||||
script:
|
script:
|
||||||
- vendor/bin/phpunit --configuration phpunit.xml --coverage-text --colors=never
|
- vendor/bin/phpunit --configuration phpunit.xml --coverage-text --colors=never
|
||||||
test:7.4-without-ext-ds:
|
test:8.1-without-ext-ds:
|
||||||
only:
|
only:
|
||||||
- branches
|
- branches
|
||||||
tags:
|
tags:
|
||||||
- default
|
- default
|
||||||
image: php:7.4
|
image: php:8.1
|
||||||
script:
|
|
||||||
- vendor/bin/phpunit --configuration phpunit.xml --coverage-text --colors=never
|
|
||||||
test:8.0:
|
|
||||||
only:
|
|
||||||
- branches
|
|
||||||
tags:
|
|
||||||
- default
|
|
||||||
image: php:8.0
|
|
||||||
script:
|
|
||||||
- vendor/bin/phpunit --configuration phpunit.xml --coverage-text --colors=never
|
|
||||||
test:8.0-without-ext-ds:
|
|
||||||
only:
|
|
||||||
- branches
|
|
||||||
tags:
|
|
||||||
- default
|
|
||||||
image: php:8.0
|
|
||||||
script:
|
script:
|
||||||
- vendor/bin/phpunit --configuration phpunit.xml --coverage-text --colors=never
|
- vendor/bin/phpunit --configuration phpunit.xml --coverage-text --colors=never
|
||||||
stages:
|
stages:
|
||||||
|
|
|
@ -0,0 +1,77 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020 https://rewiv.com sikofitt@gmail.com
|
||||||
|
*
|
||||||
|
* This file is a part of Olive BBS
|
||||||
|
*
|
||||||
|
* This Source Code Form is subject to the
|
||||||
|
* terms of the Mozilla Public License, v. 2.0.
|
||||||
|
*
|
||||||
|
* If a copy of the MPL was not distributed with this file,
|
||||||
|
* You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
|
*
|
||||||
|
* ___ ___ ___
|
||||||
|
* ( ).-. ( ) ( )
|
||||||
|
* .--. | |( __)___ ___ .--. | |.-. | |.-. .--.
|
||||||
|
* / \| |(''"( )( / \| / \| / \ / _ \
|
||||||
|
* | .-. | | | | | | | | .-. | .-. | .-. |. .' `. ;
|
||||||
|
* | | | | | | | | | | | | | | | | | | | || ' | |
|
||||||
|
* | | | | | | | | | | | |/ | | | | | | |_\_`.(___)
|
||||||
|
* | | | | | | | | | | | ' _.| | | | | | ( ). '.
|
||||||
|
* | ' | | | | | ' ' ; | .'.-| ' | | ' | || | `\ |
|
||||||
|
* ' `-' | | | | \ `' /' `-' ' `-' ;' `-' ; ; '._,' '
|
||||||
|
* `.__.(___(___) '_.' `.__.' `.__. `.__. '.___.'
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
$header = '';
|
||||||
|
|
||||||
|
if (file_exists(__DIR__.'/header.txt')) {
|
||||||
|
$header = file_get_contents('header.txt');
|
||||||
|
}
|
||||||
|
return (new \PhpCsFixer\Config())
|
||||||
|
->setRiskyAllowed(true)
|
||||||
|
->setRules([
|
||||||
|
'@PSR2' => true,
|
||||||
|
'@PHP80Migration' => true,
|
||||||
|
'@PHP80Migration:risky' => true,
|
||||||
|
'@PHP81Migration' => true,
|
||||||
|
'header_comment' => ['header' => $header],
|
||||||
|
'ordered_class_elements' => true,
|
||||||
|
'ordered_imports' => true,
|
||||||
|
'no_mixed_echo_print' => ['use' => 'print'],
|
||||||
|
'strict_param' => true,
|
||||||
|
'strict_comparison' => true,
|
||||||
|
'single_import_per_statement' => false,
|
||||||
|
'phpdoc_order' => true,
|
||||||
|
'array_syntax' => ['syntax' => 'short'],
|
||||||
|
'phpdoc_add_missing_param_annotation' => true,
|
||||||
|
'psr_autoloading' => true,
|
||||||
|
'phpdoc_var_without_name' => false,
|
||||||
|
'no_unused_imports' => true,
|
||||||
|
'no_useless_else' => true,
|
||||||
|
'no_useless_return' => true,
|
||||||
|
'no_extra_blank_lines' => [
|
||||||
|
'tokens' => [
|
||||||
|
'break',
|
||||||
|
'continue',
|
||||||
|
'extra',
|
||||||
|
'return',
|
||||||
|
'throw',
|
||||||
|
'parenthesis_brace_block',
|
||||||
|
'square_brace_block',
|
||||||
|
'curly_brace_block',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
])->setFinder(
|
||||||
|
(new \PhpCsFixer\Finder())
|
||||||
|
->ignoreDotFiles(true)
|
||||||
|
->ignoreVCS(true)
|
||||||
|
->name('*.php')
|
||||||
|
->in([
|
||||||
|
'src',
|
||||||
|
'tests',
|
||||||
|
__DIR__,
|
||||||
|
])
|
||||||
|
);
|
58
.php_cs.dist
58
.php_cs.dist
|
@ -1,58 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
$header = '';
|
|
||||||
|
|
||||||
if(file_exists(__DIR__.'/header.txt')) {
|
|
||||||
$header = file_get_contents('header.txt');
|
|
||||||
}
|
|
||||||
|
|
||||||
return PhpCsFixer\Config::create()
|
|
||||||
->setRiskyAllowed(true)
|
|
||||||
->setRules(
|
|
||||||
[
|
|
||||||
'@PSR2' => true,
|
|
||||||
'@PHP70Migration' => true,
|
|
||||||
'@PHP70Migration:risky' => true,
|
|
||||||
'@PHP71Migration' => true,
|
|
||||||
'@PHP71Migration:risky' => true,
|
|
||||||
'@PHP73Migration' => true,
|
|
||||||
'@PHP74Migration' => true,
|
|
||||||
'@PHP74Migration:risky' => true,
|
|
||||||
'@PHP80Migration' => true,
|
|
||||||
'@PHP80Migration:risky' => true,
|
|
||||||
'header_comment' => ['header' => $header],
|
|
||||||
'ordered_class_elements' => true,
|
|
||||||
'ordered_imports' => true,
|
|
||||||
'no_mixed_echo_print' => ['use' => 'print'],
|
|
||||||
'strict_param' => true,
|
|
||||||
'strict_comparison' => true,
|
|
||||||
'single_import_per_statement' => false,
|
|
||||||
'phpdoc_order' => true,
|
|
||||||
'array_syntax' => ['syntax' => 'short'],
|
|
||||||
'phpdoc_add_missing_param_annotation' => true,
|
|
||||||
'psr4' => true,
|
|
||||||
'phpdoc_var_without_name' => false,
|
|
||||||
'no_unused_imports' => true,
|
|
||||||
'no_useless_else' => true,
|
|
||||||
'no_useless_return' => true,
|
|
||||||
'no_extra_consecutive_blank_lines' => [
|
|
||||||
'break',
|
|
||||||
'continue',
|
|
||||||
'extra',
|
|
||||||
'return',
|
|
||||||
'throw',
|
|
||||||
'parenthesis_brace_block',
|
|
||||||
'square_brace_block',
|
|
||||||
'curly_brace_block',
|
|
||||||
],
|
|
||||||
]
|
|
||||||
)->setFinder(
|
|
||||||
PhpCsFixer\Finder::create()
|
|
||||||
->ignoreDotFiles(true)
|
|
||||||
->ignoreVCS(true)
|
|
||||||
->name('*.php')
|
|
||||||
->in([
|
|
||||||
'src',
|
|
||||||
'tests',
|
|
||||||
])
|
|
||||||
);
|
|
|
@ -1,14 +1,13 @@
|
||||||
{
|
{
|
||||||
"name": "olivebbs/map",
|
"name": "olive/map",
|
||||||
"description": "Generic map for PHP",
|
"description": "Generic map for PHP",
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=7.4",
|
"php": ">=8.1",
|
||||||
"php-ds/php-ds": "^1.3",
|
"php-ds/php-ds": "^1.4"
|
||||||
"symfony/polyfill-php80": "^1.22"
|
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"friendsofphp/php-cs-fixer": "^2.18",
|
"friendsofphp/php-cs-fixer": "^3.8",
|
||||||
"phpunit/phpunit": "^9.5"
|
"phpunit/phpunit": "^9.5"
|
||||||
},
|
},
|
||||||
"license": "MPL-2.0",
|
"license": "MPL-2.0",
|
||||||
|
@ -19,7 +18,7 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"suggest": {
|
"suggest": {
|
||||||
"ext-ds": "ext-ds"
|
"ext-ds": "To use the Data structure extension instead of polyfill."
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -34,14 +34,15 @@ class CharMap extends GenericMap
|
||||||
{
|
{
|
||||||
public function __construct(array $initialValues = [])
|
public function __construct(array $initialValues = [])
|
||||||
{
|
{
|
||||||
parent::__construct(self::CHAR, self::CHAR);
|
parent::__construct(keyType: self::CHAR, valueType: self::CHAR);
|
||||||
|
|
||||||
$this->map->putAll($initialValues);
|
$this->map->putAll(values: $initialValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromString(string $string): CharMap
|
public static function fromString(string $string): static
|
||||||
{
|
{
|
||||||
$values = str_split($string);
|
$values = str_split(string: $string);
|
||||||
return new self(array_combine($values, $values));
|
|
||||||
|
return new static(initialValues: array_combine(keys: $values, values: $values));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,15 +28,19 @@
|
||||||
namespace Olivebbs\Map;
|
namespace Olivebbs\Map;
|
||||||
|
|
||||||
use ArrayAccess;
|
use ArrayAccess;
|
||||||
|
use function class_exists;
|
||||||
use Countable;
|
use Countable;
|
||||||
use Ds\Map;
|
use Ds\Map;
|
||||||
|
use function enum_exists;
|
||||||
|
use function in_array;
|
||||||
use function is_array;
|
use function is_array;
|
||||||
use function is_callable;
|
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 Olivebbs\Map\Exception\InvalidArgumentException;
|
use Olivebbs\Map\Exception\InvalidArgumentException;
|
||||||
|
use function sprintf;
|
||||||
use function strtolower;
|
use function strtolower;
|
||||||
use TypeError;
|
use TypeError;
|
||||||
use ValueError;
|
use ValueError;
|
||||||
|
@ -50,24 +54,23 @@ class GenericMap implements ArrayAccess, Countable
|
||||||
public const INTEGER = 'integer';
|
public const INTEGER = 'integer';
|
||||||
public const STRING = 'string';
|
public const STRING = 'string';
|
||||||
public const CHAR = 'char';
|
public const CHAR = 'char';
|
||||||
|
public const ENUM = 'enum';
|
||||||
public const ANY = 'any';
|
public const ANY = 'any';
|
||||||
|
|
||||||
protected Map $map;
|
protected Map $map;
|
||||||
protected string $keyType;
|
|
||||||
protected string $valueType;
|
|
||||||
|
|
||||||
public function __construct(string $keyType = self::ANY, string $valueType = self::ANY)
|
public function __construct(protected string $keyType = self::ANY, protected string $valueType = self::ANY)
|
||||||
{
|
{
|
||||||
$this->keyType = strtolower($keyType);
|
$this->keyType = strtolower(string: $keyType);
|
||||||
|
|
||||||
$this->valueType = class_exists($valueType) ? $valueType : strtolower($valueType);
|
$this->valueType = class_exists(class: $valueType) ? $valueType : strtolower(string: $valueType);
|
||||||
|
|
||||||
if (!$this->isValidKeyType($this->keyType)) {
|
if (!$this->isValidKeyType(type: $this->keyType)) {
|
||||||
throw new InvalidArgumentException(sprintf('Invalid key type (%s)', $this->keyType));
|
throw new InvalidArgumentException(message: sprintf('Invalid key type (%s)', $this->keyType));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->isValidValueType($this->valueType)) {
|
if (!$this->isValidValueType(type: $this->valueType)) {
|
||||||
throw new InvalidArgumentException(sprintf('Invalid value type (%s)', $this->valueType));
|
throw new InvalidArgumentException(message: sprintf('Invalid value type (%s)', $this->valueType));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->map = new Map();
|
$this->map = new Map();
|
||||||
|
@ -86,71 +89,45 @@ class GenericMap implements ArrayAccess, Countable
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
public function offsetExists($offset): bool
|
public function offsetExists(mixed $offset): bool
|
||||||
{
|
{
|
||||||
if (is_array($offset) || is_object($offset)) {
|
$this->checkTypes(offset: $offset);
|
||||||
throw new InvalidArgumentException('Map keys cannot be objects or arrays');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$this->checkType($this->keyType, $offset)) {
|
return $this->map->offsetExists(offset: $offset);
|
||||||
throw new TypeError(sprintf('Key should be of value %s.', $this->keyType));
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->map->offsetExists($offset);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
public function offsetGet($offset)
|
public function offsetGet(mixed $offset): mixed
|
||||||
{
|
{
|
||||||
if (is_array($offset) || is_object($offset)) {
|
$this->checkTypes(offset: $offset);
|
||||||
throw new InvalidArgumentException('Map keys cannot be objects or arrays');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$this->checkType($this->keyType, $offset)) {
|
if (!$this->map->offsetExists(offset: $offset)) {
|
||||||
throw new TypeError(sprintf('Key should be of value %s.', $this->keyType));
|
|
||||||
}
|
|
||||||
if (!$this->map->offsetExists($offset)) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return $this->map->offsetGet($offset);
|
|
||||||
|
return $this->map->offsetGet(offset: $offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
public function offsetSet($offset, $value): void
|
public function offsetSet(mixed $offset, mixed $value): void
|
||||||
{
|
{
|
||||||
if (is_array($offset) || is_object($offset)) {
|
$this->checkTypes($offset, $value);
|
||||||
throw new InvalidArgumentException('Map keys cannot be objects or arrays');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$this->checkType($this->keyType, $offset)) {
|
$this->map->offsetSet(offset: $offset, value: $value);
|
||||||
throw new TypeError(sprintf('Key should be of value %s.', $this->keyType));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$this->checkType($this->valueType, $value)) {
|
|
||||||
throw new ValueError(sprintf('Value should be of type %s.', $this->valueType));
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->map->offsetSet($offset, $value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
public function offsetUnset($offset): void
|
public function offsetUnset(mixed $offset): void
|
||||||
{
|
{
|
||||||
if (is_array($offset) || is_object($offset)) {
|
$this->checkTypes(offset: $offset);
|
||||||
throw new InvalidArgumentException('Map keys cannot be objects or arrays');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$this->checkType($this->keyType, $offset)) {
|
$this->map->offsetUnset(offset: $offset);
|
||||||
throw new TypeError(sprintf('Key should be of value %s.', $this->keyType));
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->map->offsetUnset($offset);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -175,61 +152,69 @@ class GenericMap implements ArrayAccess, Countable
|
||||||
protected function assertInitialValues(array $initialValues): bool
|
protected function assertInitialValues(array $initialValues): bool
|
||||||
{
|
{
|
||||||
foreach ($initialValues as $key => $value) {
|
foreach ($initialValues as $key => $value) {
|
||||||
if (!$this->checkType($this->keyType, $key) || !$this->checkType($this->valueType, $value)) {
|
if (!$this->checkType(type: $this->keyType, var: $key) || !$this->checkType(type: $this->valueType, var: $value)) {
|
||||||
throw new InvalidArgumentException(sprintf('Invalid types for map [%s => %s], they should be [%s => %s]', get_debug_type($key), get_debug_type($value), $this->keyType, $this->valueType));
|
throw new InvalidArgumentException(message: sprintf('Invalid types for map [%s => %s], they should be [%s => %s]', get_debug_type(value: $key), get_debug_type(value: $value), $this->keyType, $this->valueType));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function checkTypes(mixed $offset, mixed $value = null): void
|
||||||
|
{
|
||||||
|
if (is_array(value: $offset) || is_object(value: $offset)) {
|
||||||
|
throw new InvalidArgumentException(message: 'Map keys cannot be objects or arrays');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->checkType(type: $this->keyType, var: $offset)) {
|
||||||
|
throw new TypeError(message: sprintf('Key should be of value %s.', $this->keyType));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null !== $value && !$this->checkType(type: $this->valueType, var: $value)) {
|
||||||
|
throw new ValueError(message: sprintf('Value should be of type %s.', $this->valueType));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @codeCoverageIgnore
|
* @codeCoverageIgnore
|
||||||
* @param string $type
|
* @param string $type
|
||||||
* @param $var
|
* @param mixed $var
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function checkType(string $type, $var): bool
|
private function checkType(string $type, mixed $var): bool
|
||||||
{
|
{
|
||||||
switch ($type) {
|
return match ($type) {
|
||||||
case self::OBJECT:
|
self::OBJECT => is_object(value: $var),
|
||||||
return is_object($var);
|
self::CALLABLE => is_callable(value: $var),
|
||||||
case self::CALLABLE:
|
self::ARRAY => is_array(value: $var),
|
||||||
return is_callable($var);
|
self::INT, self::INTEGER => is_int(value: $var),
|
||||||
case self::ARRAY:
|
self::STRING => is_string(value: $var),
|
||||||
return is_array($var);
|
self::CHAR => is_string(value: $var) && (function_exists('mb_strlen') ? mb_strlen(string: $var) === 1 : strlen(string: $var) === 1),
|
||||||
case self::INT:
|
self::ENUM => is_object(value: $var) && enum_exists(enum: $var::class),
|
||||||
case self::INTEGER:
|
self::ANY => true,
|
||||||
return is_int($var);
|
default => $var instanceof $type,
|
||||||
case self::STRING:
|
};
|
||||||
return is_string($var);
|
|
||||||
case self::CHAR:
|
|
||||||
return is_string($var) && \mb_strlen($var) === 1;
|
|
||||||
case self::ANY:
|
|
||||||
return true;
|
|
||||||
default:
|
|
||||||
return $var instanceof $type;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function isValidKeyType(string $type): bool
|
private function isValidKeyType(string $type): bool
|
||||||
{
|
{
|
||||||
return in_array($type, [
|
return in_array(needle: $type, haystack: [
|
||||||
self::INT,
|
self::INT,
|
||||||
self::INTEGER,
|
self::INTEGER,
|
||||||
self::STRING,
|
self::STRING,
|
||||||
self::CHAR,
|
self::CHAR,
|
||||||
self::ANY,
|
self::ANY,
|
||||||
], true);
|
], strict: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function isValidValueType(string $type): bool
|
private function isValidValueType(string $type): bool
|
||||||
{
|
{
|
||||||
if (class_exists($type)) {
|
if (class_exists(class: $type)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return in_array($type, [
|
return in_array(needle: $type, haystack: [
|
||||||
self::OBJECT,
|
self::OBJECT,
|
||||||
self::CALLABLE,
|
self::CALLABLE,
|
||||||
self::ARRAY,
|
self::ARRAY,
|
||||||
|
@ -237,7 +222,8 @@ class GenericMap implements ArrayAccess, Countable
|
||||||
self::INTEGER,
|
self::INTEGER,
|
||||||
self::STRING,
|
self::STRING,
|
||||||
self::CHAR,
|
self::CHAR,
|
||||||
|
self::ENUM,
|
||||||
self::ANY,
|
self::ANY,
|
||||||
], true);
|
], strict: true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,28 +32,28 @@ use Olivebbs\Map\Exception\ImmutableMapException;
|
||||||
class ImmutableMap extends GenericMap
|
class ImmutableMap extends GenericMap
|
||||||
{
|
{
|
||||||
private function __construct(
|
private function __construct(
|
||||||
?string $keyType,
|
string $keyType,
|
||||||
?string $valueType,
|
string $valueType,
|
||||||
array $values
|
array $values
|
||||||
) {
|
) {
|
||||||
parent::__construct($keyType, $valueType);
|
parent::__construct(keyType: $keyType, valueType: $valueType);
|
||||||
|
|
||||||
$this->assertInitialValues($values);
|
$this->assertInitialValues(initialValues: $values);
|
||||||
$this->map->putAll($values);
|
$this->map->putAll(values: $values);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function offsetSet($offset, $value): void
|
public function offsetSet(mixed $offset, mixed $value): void
|
||||||
{
|
{
|
||||||
throw new ImmutableMapException(sprintf('Cannot change values in %s', __CLASS__));
|
throw new ImmutableMapException(message: sprintf('Cannot change values in %s', __CLASS__));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function offsetUnset($offset): void
|
public function offsetUnset(mixed $offset): void
|
||||||
{
|
{
|
||||||
throw new ImmutableMapException(sprintf('Cannot unset values in %s', __CLASS__));
|
throw new ImmutableMapException(message: sprintf('Cannot unset values in %s', __CLASS__));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function create(string $keyType, string $valueType, array $values): ImmutableMap
|
public static function create(string $keyType, string $valueType, array $values): static
|
||||||
{
|
{
|
||||||
return new self($keyType, $valueType, $values);
|
return new static(keyType: $keyType, valueType: $valueType, values: $values);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,14 +33,14 @@ class IntCharMap extends GenericMap
|
||||||
{
|
{
|
||||||
public function __construct(array $initialValues = [])
|
public function __construct(array $initialValues = [])
|
||||||
{
|
{
|
||||||
parent::__construct(self::INT, self::CHAR);
|
parent::__construct(keyType: self::INT, valueType: self::CHAR);
|
||||||
|
|
||||||
$this->map->putAll($initialValues);
|
$this->map->putAll(values: $initialValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromString(string $string): IntCharMap
|
public static function fromString(string $string): static
|
||||||
{
|
{
|
||||||
$values = str_split($string);
|
$values = str_split(string: $string);
|
||||||
return new self($values);
|
return new static(initialValues: $values);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -167,6 +167,22 @@ class GenericMapTest extends TestCase
|
||||||
$genericMap[$object] = 1;
|
$genericMap[$object] = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testEnumMap(): void
|
||||||
|
{
|
||||||
|
$enumMap = new class(TestEnum::cases()) extends GenericMap {
|
||||||
|
public function __construct(array $values = [])
|
||||||
|
{
|
||||||
|
parent::__construct(self::INT, self::ENUM);
|
||||||
|
$this->assertInitialValues($values);
|
||||||
|
$this->map->putAll($values);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
self::assertCount(8, $enumMap->toArray());
|
||||||
|
self::assertSame(TestEnum::a, $enumMap[0]);
|
||||||
|
self::assertSame(TestEnum::h, $enumMap[7]);
|
||||||
|
}
|
||||||
|
|
||||||
private function resetGenericMap(): void
|
private function resetGenericMap(): void
|
||||||
{
|
{
|
||||||
$this->genericMap = $this->getGenericMap();
|
$this->genericMap = $this->getGenericMap();
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020 https://rewiv.com sikofitt@gmail.com
|
||||||
|
*
|
||||||
|
* This file is a part of Olive BBS
|
||||||
|
*
|
||||||
|
* This Source Code Form is subject to the
|
||||||
|
* terms of the Mozilla Public License, v. 2.0.
|
||||||
|
*
|
||||||
|
* If a copy of the MPL was not distributed with this file,
|
||||||
|
* You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
|
*
|
||||||
|
* ___ ___ ___
|
||||||
|
* ( ).-. ( ) ( )
|
||||||
|
* .--. | |( __)___ ___ .--. | |.-. | |.-. .--.
|
||||||
|
* / \| |(''"( )( / \| / \| / \ / _ \
|
||||||
|
* | .-. | | | | | | | | .-. | .-. | .-. |. .' `. ;
|
||||||
|
* | | | | | | | | | | | | | | | | | | | || ' | |
|
||||||
|
* | | | | | | | | | | | |/ | | | | | | |_\_`.(___)
|
||||||
|
* | | | | | | | | | | | ' _.| | | | | | ( ). '.
|
||||||
|
* | ' | | | | | ' ' ; | .'.-| ' | | ' | || | `\ |
|
||||||
|
* ' `-' | | | | \ `' /' `-' ' `-' ;' `-' ; ; '._,' '
|
||||||
|
* `.__.(___(___) '_.' `.__.' `.__. `.__. '.___.'
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Olivebbs\Tests\Map;
|
||||||
|
|
||||||
|
enum TestEnum
|
||||||
|
{
|
||||||
|
case a;
|
||||||
|
case b;
|
||||||
|
case c;
|
||||||
|
case d;
|
||||||
|
case e;
|
||||||
|
case f;
|
||||||
|
case g;
|
||||||
|
case h;
|
||||||
|
}
|
Loading…
Reference in New Issue