20220717
This commit is contained in:
103
fcms-core/components/Dictionary.php
Normal file
103
fcms-core/components/Dictionary.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/**
|
||||
* Класс словаря
|
||||
*/
|
||||
|
||||
namespace freecms\components;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Словарь (реализация интерфейса IDictionary
|
||||
*/
|
||||
class Dictionary implements IDictionary
|
||||
{
|
||||
/**
|
||||
* @var array Содержимое словаря
|
||||
*/
|
||||
private array $dict;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->dict = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function add(string $key, mixed $value): void
|
||||
{
|
||||
if (!$this->isKeyExists($key)) {
|
||||
$this->dict[$key] = $value;
|
||||
} else {
|
||||
$this->update($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isKeyExists(string $key): bool
|
||||
{
|
||||
return array_key_exists($key, $this->dict);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function update(string $key, mixed $newValue): void
|
||||
{
|
||||
if (!$this->isKeyExists($key)) {
|
||||
$this->add($key, $newValue);
|
||||
} else {
|
||||
$this->dict[$key] = $newValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws Exception Если ключ не существует
|
||||
*/
|
||||
public function get(string $key): mixed
|
||||
{
|
||||
if ($this->isKeyExists($key)) {
|
||||
return $this->dict[$key];
|
||||
} else {
|
||||
throw new Exception('Ключ "' . $key . '" не существует в этом словаре!', 0, null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws Exception Если ключ не существует
|
||||
*/
|
||||
public function clear(): void
|
||||
{
|
||||
$keys = $this->getAllKeys();
|
||||
|
||||
foreach ($keys as $key) {
|
||||
$this->remove($key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getAllKeys(): array
|
||||
{
|
||||
return array_keys($this->dict);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws Exception Если ключ не существует
|
||||
*/
|
||||
public function remove(string $key): void
|
||||
{
|
||||
if ($this->isKeyExists($key)) {
|
||||
unset($this->dict[$key]);
|
||||
} else {
|
||||
throw new Exception('Ключ "' . $key . '" не существует в этом словаре!', 0, null);
|
||||
}
|
||||
}
|
||||
}
|
62
fcms-core/components/IDictionary.php
Normal file
62
fcms-core/components/IDictionary.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/**
|
||||
* Интерфейс словаря.
|
||||
* @package freecms
|
||||
*/
|
||||
|
||||
namespace freecms\components;
|
||||
|
||||
/**
|
||||
* Словарь
|
||||
*/
|
||||
interface IDictionary
|
||||
{
|
||||
/**
|
||||
* Добавление строки в словарь
|
||||
* @param string $key Ключ
|
||||
* @param mixed $value Значение
|
||||
* @return void
|
||||
*/
|
||||
public function add(string $key, mixed $value): void;
|
||||
|
||||
/**
|
||||
* Получение значения по ключу
|
||||
* @param string $key Ключ
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $key): mixed;
|
||||
|
||||
/**
|
||||
* Обновление значения
|
||||
* @param string $key Ключ
|
||||
* @param mixed $newValue Новое значение
|
||||
* @return void
|
||||
*/
|
||||
public function update(string $key, mixed $newValue): void;
|
||||
|
||||
/**
|
||||
* Получение всех ключей
|
||||
* @return array Одномерный массив ключей
|
||||
*/
|
||||
public function getAllKeys(): array;
|
||||
|
||||
/**
|
||||
* Проверяет существование ключа
|
||||
* @param string $key Ключ
|
||||
* @return bool
|
||||
*/
|
||||
public function isKeyExists(string $key): bool;
|
||||
|
||||
/**
|
||||
* Удаляет ключ
|
||||
* @param string $key Ключ
|
||||
* @return void
|
||||
*/
|
||||
public function remove(string $key): void;
|
||||
|
||||
/**
|
||||
* Очищает словарь
|
||||
* @return void
|
||||
*/
|
||||
public function clear(): void;
|
||||
}
|
Reference in New Issue
Block a user