Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\BrowserKit;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * History.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
18 */
|
Chris@0
|
19 class History
|
Chris@0
|
20 {
|
Chris@17
|
21 protected $stack = [];
|
Chris@0
|
22 protected $position = -1;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * Clears the history.
|
Chris@0
|
26 */
|
Chris@0
|
27 public function clear()
|
Chris@0
|
28 {
|
Chris@17
|
29 $this->stack = [];
|
Chris@0
|
30 $this->position = -1;
|
Chris@0
|
31 }
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * Adds a Request to the history.
|
Chris@0
|
35 */
|
Chris@0
|
36 public function add(Request $request)
|
Chris@0
|
37 {
|
Chris@17
|
38 $this->stack = \array_slice($this->stack, 0, $this->position + 1);
|
Chris@0
|
39 $this->stack[] = clone $request;
|
Chris@17
|
40 $this->position = \count($this->stack) - 1;
|
Chris@0
|
41 }
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * Returns true if the history is empty.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @return bool true if the history is empty, false otherwise
|
Chris@0
|
47 */
|
Chris@0
|
48 public function isEmpty()
|
Chris@0
|
49 {
|
Chris@17
|
50 return 0 == \count($this->stack);
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * Goes back in the history.
|
Chris@0
|
55 *
|
Chris@0
|
56 * @return Request A Request instance
|
Chris@0
|
57 *
|
Chris@0
|
58 * @throws \LogicException if the stack is already on the first page
|
Chris@0
|
59 */
|
Chris@0
|
60 public function back()
|
Chris@0
|
61 {
|
Chris@0
|
62 if ($this->position < 1) {
|
Chris@0
|
63 throw new \LogicException('You are already on the first page.');
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 return clone $this->stack[--$this->position];
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * Goes forward in the history.
|
Chris@0
|
71 *
|
Chris@0
|
72 * @return Request A Request instance
|
Chris@0
|
73 *
|
Chris@0
|
74 * @throws \LogicException if the stack is already on the last page
|
Chris@0
|
75 */
|
Chris@0
|
76 public function forward()
|
Chris@0
|
77 {
|
Chris@17
|
78 if ($this->position > \count($this->stack) - 2) {
|
Chris@0
|
79 throw new \LogicException('You are already on the last page.');
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 return clone $this->stack[++$this->position];
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 /**
|
Chris@0
|
86 * Returns the current element in the history.
|
Chris@0
|
87 *
|
Chris@0
|
88 * @return Request A Request instance
|
Chris@0
|
89 *
|
Chris@0
|
90 * @throws \LogicException if the stack is empty
|
Chris@0
|
91 */
|
Chris@0
|
92 public function current()
|
Chris@0
|
93 {
|
Chris@0
|
94 if (-1 == $this->position) {
|
Chris@0
|
95 throw new \LogicException('The page history is empty.');
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 return clone $this->stack[$this->position];
|
Chris@0
|
99 }
|
Chris@0
|
100 }
|