This commit is contained in:
2021-11-30 14:59:51 +08:00
parent 2870a64792
commit e5b57cbcdb
2 changed files with 104 additions and 5 deletions
+98
View File
@@ -0,0 +1,98 @@
<?php
namespace Kiri\Core;
use JetBrains\PhpStorm\Pure;
class HashMap implements \ArrayAccess
{
/**
* @var array
*/
private array $lists = [];
/**
* @param string $key
* @param $value
*/
public function put(string $key, $value)
{
$this->lists[$key] = $value;
}
/**
* @param string $key
* @return mixed
*/
#[Pure] public function get(string $key): mixed
{
if (!$this->has($key)) {
return null;
}
return $this->lists[$key];
}
/**
* @param string $key
*/
public function del(string $key)
{
if (!$this->has($key)) {
return;
}
unset($this->lists[$key]);
}
/**
* @param string $key
* @return bool
*/
public function has(string $key): bool
{
return array_key_exists($key, $this->lists);
}
/**
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset): bool
{
return isset($this->lists[$offset]);
}
/**
* @param mixed $offset
* @return mixed
*/
#[Pure] public function offsetGet($offset): mixed
{
return $this->get($offset);
}
/**
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value)
{
$this->put($offset, $value);
}
/**
* @param mixed $offset
*/
public function offsetUnset($offset)
{
unset($this->lists[$offset]);
}
}