Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\BrowserKit; Chris@0: Chris@0: /** Chris@0: * History. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class History Chris@0: { Chris@17: protected $stack = []; Chris@0: protected $position = -1; Chris@0: Chris@0: /** Chris@0: * Clears the history. Chris@0: */ Chris@0: public function clear() Chris@0: { Chris@17: $this->stack = []; Chris@0: $this->position = -1; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds a Request to the history. Chris@0: */ Chris@0: public function add(Request $request) Chris@0: { Chris@17: $this->stack = \array_slice($this->stack, 0, $this->position + 1); Chris@0: $this->stack[] = clone $request; Chris@17: $this->position = \count($this->stack) - 1; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true if the history is empty. Chris@0: * Chris@0: * @return bool true if the history is empty, false otherwise Chris@0: */ Chris@0: public function isEmpty() Chris@0: { Chris@17: return 0 == \count($this->stack); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Goes back in the history. Chris@0: * Chris@0: * @return Request A Request instance Chris@0: * Chris@0: * @throws \LogicException if the stack is already on the first page Chris@0: */ Chris@0: public function back() Chris@0: { Chris@0: if ($this->position < 1) { Chris@0: throw new \LogicException('You are already on the first page.'); Chris@0: } Chris@0: Chris@0: return clone $this->stack[--$this->position]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Goes forward in the history. Chris@0: * Chris@0: * @return Request A Request instance Chris@0: * Chris@0: * @throws \LogicException if the stack is already on the last page Chris@0: */ Chris@0: public function forward() Chris@0: { Chris@17: if ($this->position > \count($this->stack) - 2) { Chris@0: throw new \LogicException('You are already on the last page.'); Chris@0: } Chris@0: Chris@0: return clone $this->stack[++$this->position]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the current element in the history. Chris@0: * Chris@0: * @return Request A Request instance Chris@0: * Chris@0: * @throws \LogicException if the stack is empty Chris@0: */ Chris@0: public function current() Chris@0: { Chris@0: if (-1 == $this->position) { Chris@0: throw new \LogicException('The page history is empty.'); Chris@0: } Chris@0: Chris@0: return clone $this->stack[$this->position]; Chris@0: } Chris@0: }