mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-01-17 21:48:59 +01:00
51 lines
960 B
PHP
51 lines
960 B
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace DesignPatterns\Behavioral\Memento;
|
|
|
|
/**
|
|
* Ticket is the "Originator" in this implementation
|
|
*/
|
|
class Ticket
|
|
{
|
|
/**
|
|
* @var State
|
|
*/
|
|
private $currentState;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->currentState = new State(State::STATE_CREATED);
|
|
}
|
|
|
|
public function open()
|
|
{
|
|
$this->currentState = new State(State::STATE_OPENED);
|
|
}
|
|
|
|
public function assign()
|
|
{
|
|
$this->currentState = new State(State::STATE_ASSIGNED);
|
|
}
|
|
|
|
public function close()
|
|
{
|
|
$this->currentState = new State(State::STATE_CLOSED);
|
|
}
|
|
|
|
public function saveToMemento(): Memento
|
|
{
|
|
return new Memento(clone $this->currentState);
|
|
}
|
|
|
|
public function restoreFromMemento(Memento $memento)
|
|
{
|
|
$this->currentState = $memento->getState();
|
|
}
|
|
|
|
public function getState(): State
|
|
{
|
|
return $this->currentState;
|
|
}
|
|
}
|