1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /** |
||
6 | * @author : Jagepard <[email protected]> |
||
7 | * @license https://mit-license.org/ MIT |
||
8 | */ |
||
9 | |||
10 | namespace Behavioral\Memento; |
||
11 | |||
12 | class Caretaker implements CaretakerInterface |
||
13 | { |
||
14 | private OriginatorInterface $originator; |
||
15 | private array $history = []; |
||
16 | |||
17 | /** |
||
18 | * Accepts an object |
||
19 | * ------------------------- |
||
20 | * Принимает объект в работу |
||
21 | * |
||
22 | * @param OriginatorInterface $originator |
||
23 | */ |
||
24 | public function __construct(OriginatorInterface $originator) |
||
25 | { |
||
26 | $this->originator = $originator; |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Restores the previous state |
||
31 | * ------------------------------------ |
||
32 | * Восстанавливает предыдущее состояние |
||
33 | * |
||
34 | * @return void |
||
35 | */ |
||
36 | public function undo(): void |
||
37 | { |
||
38 | $memento = array_pop($this->history)->getMemento(); |
||
39 | $this->originator->setState($memento["state"], $memento["date"]); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Stores data about the current state of an object |
||
44 | * ------------------------------------------------ |
||
45 | * Сохраняет данные о текущем состоянии объекта |
||
46 | * |
||
47 | * @return void |
||
48 | */ |
||
49 | public function save(): void |
||
50 | { |
||
51 | sleep(1); // for example |
||
52 | $this->history[] = new Memento([ |
||
53 | "state" => $this->originator->getState(), |
||
54 | "date" => $this->originator->getDate() |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
55 | ] |
||
56 | ); |
||
57 | } |
||
58 | } |
||
59 |