mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-12 18:04:04 +02:00
PHP7 Memento
This commit is contained in:
49
Behavioral/Memento/Ticket.php
Normal file
49
Behavioral/Memento/Ticket.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user