34 lines
523 B
PHP
34 lines
523 B
PHP
|
<?php
|
||
|
|
||
|
namespace Machine\Memory;
|
||
|
|
||
|
use InvalidArgumentException;
|
||
|
use Tool\Tool;
|
||
|
|
||
|
class ToolMemory
|
||
|
{
|
||
|
/** @var Tool[] */
|
||
|
private array $memory = [];
|
||
|
|
||
|
|
||
|
public function write(Tool $tool): void
|
||
|
{
|
||
|
$this->guardInvalidToolNumber($tool->T);
|
||
|
$this->memory[$tool->T] = $tool;
|
||
|
}
|
||
|
|
||
|
|
||
|
public function read(int $T): ?Tool
|
||
|
{
|
||
|
return @$this->memory[$T];
|
||
|
}
|
||
|
|
||
|
|
||
|
private function guardInvalidToolNumber(int $T): void
|
||
|
{
|
||
|
if ($T < 1 || $T > 64) {
|
||
|
throw new InvalidArgumentException('Tool number must be in range 1 - 64');
|
||
|
}
|
||
|
}
|
||
|
}
|