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