Created
June 11, 2024 11:19
-
-
Save asrorbekh/bd3682f4fcd163fc5ecef5dc79e10e0e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Map | |
{ | |
private array $container = []; | |
public function __construct(array $data = [], $keyField = null) | |
{ | |
if (!empty($data) && $keyField !== null) { | |
$this->setMultiple($data, $keyField); | |
} | |
} | |
public function set($key, $value): void | |
{ | |
$this->container[$key] = $value; | |
} | |
public function get($key) | |
{ | |
return $this->container[$key] ?? null; | |
} | |
public function has($key): bool | |
{ | |
return array_key_exists($key, $this->container); | |
} | |
public function delete($key): bool | |
{ | |
if ($this->has($key)) { | |
unset($this->container[$key]); | |
return true; | |
} | |
return false; | |
} | |
public function clear(): void | |
{ | |
$this->container = []; | |
} | |
public function size(): int | |
{ | |
return count($this->container); | |
} | |
public function entries(): array | |
{ | |
return $this->container; | |
} | |
private function setMultiple(array $array, $keyField): void | |
{ | |
$this->container = array_column($array, null, $keyField); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment