mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-02-25 02:02:26 +01:00
49 lines
938 B
PHP
49 lines
938 B
PHP
<?php
|
|
|
|
namespace DesignPatterns\Behavioral\Memento;
|
|
|
|
class State
|
|
{
|
|
const STATE_CREATED = 'created';
|
|
const STATE_OPENED = 'opened';
|
|
const STATE_ASSIGNED = 'assigned';
|
|
const STATE_CLOSED = 'closed';
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
private $state;
|
|
|
|
/**
|
|
* @var string[]
|
|
*/
|
|
private static $validStates = [
|
|
self::STATE_CREATED,
|
|
self::STATE_OPENED,
|
|
self::STATE_ASSIGNED,
|
|
self::STATE_CLOSED,
|
|
];
|
|
|
|
/**
|
|
* @param string $state
|
|
*/
|
|
public function __construct(string $state)
|
|
{
|
|
self::ensureIsValidState($state);
|
|
|
|
$this->state = $state;
|
|
}
|
|
|
|
private static function ensureIsValidState(string $state)
|
|
{
|
|
if (!in_array($state, self::$validStates)) {
|
|
throw new \InvalidArgumentException('Invalid state given');
|
|
}
|
|
}
|
|
|
|
public function __toString(): string
|
|
{
|
|
return $this->state;
|
|
}
|
|
}
|