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