This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Components/BrowserKit/History.php

113 lines
2.6 KiB
PHP
Raw Normal View History

2010-04-19 13:12:42 +01:00
<?php
namespace Symfony\Components\BrowserKit;
/*
2010-04-24 00:22:16 +01:00
* This file is part of the Symfony package.
2010-04-19 13:12:42 +01:00
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* History.
*
* @package Symfony
* @subpackage Components_BrowserKit
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class History
{
protected $stack = array();
protected $position = -1;
2010-04-19 13:12:42 +01:00
/**
* Constructor.
*/
public function __construct()
{
$this->clear();
}
2010-04-19 13:12:42 +01:00
/**
* Clears the history.
*/
public function clear()
2010-04-19 13:12:42 +01:00
{
$this->stack = array();
$this->position = -1;
2010-04-19 13:12:42 +01:00
}
/**
* Adds a Request to the history.
*
* @param Symfony\Components\BrowserKit\Request $request A Request instance
*/
public function add(Request $request)
{
$this->stack = array_slice($this->stack, 0, $this->position + 1);
$this->stack[] = clone $request;
$this->position = count($this->stack) - 1;
}
2010-04-19 13:12:42 +01:00
/**
* Returns true if the history is empty.
*
* @return Boolean true if the history is empty, false otherwise
*/
public function isEmpty()
2010-04-19 13:12:42 +01:00
{
return count($this->stack) == 0;
2010-04-19 13:12:42 +01:00
}
/**
* Goes back in the history.
*
* @return Symfony\Components\BrowserKit\Request A Request instance
*
* @throws \LogicException if the stack is already on the first page
*/
public function back()
{
if ($this->position < 1) {
throw new \LogicException('You are already on the first page.');
}
return clone $this->stack[--$this->position];
}
2010-04-19 13:12:42 +01:00
/**
* Goes forward in the history.
*
* @return Symfony\Components\BrowserKit\Request A Request instance
*
* @throws \LogicException if the stack is already on the last page
*/
public function forward()
2010-04-19 13:12:42 +01:00
{
if ($this->position > count($this->stack) - 2) {
throw new \LogicException('You are already on the last page.');
}
return clone $this->stack[++$this->position];
2010-04-19 13:12:42 +01:00
}
/**
* Returns the current element in the history.
*
* @return Symfony\Components\BrowserKit\Request A Request instance
*
* @throws \LogicException if the stack is empty
*/
public function current()
{
if (-1 == $this->position) {
throw new \LogicException('The page history is empty.');
}
return clone $this->stack[$this->position];
}
2010-04-19 13:12:42 +01:00
}