<?php
/**
* Harmony
* Copyright (c) 2008 Maxime Bouroumeau-Fuseau
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @category Harmony
* @package Harmony_Parser
* @author Maxime Bouroumeau-Fuseau
* @copyright 2008 (c) Maxime Bouroumeau-Fuseau
* @license http://www.opensource.org/licenses/mit-license.php
* @link http://www.harmony-framework.com
*/
/** Harmony_Parser_Abstract */
require_once 'Harmony/Parser/Abstract.php';
/**
* @category Harmony
* @package Harmony_Parser
* @copyright 2008 (c) Maxime Bouroumeau-Fuseau
* @license http://www.opensource.org/licenses/mit-license.php
*/
abstract class Harmony_Parser_Context
{
/**
* Current parser
*
* @var Harmony_Parser_Abstract
*/
protected $_parser;
/**
* Tokens for the current context
*
* @var array
*/
protected $_tokens = array();
/**
* Params for the context
*
* @var array
*/
protected $_params;
/**
* Data returned to the parent context
*
* @var array
*/
protected $_exitData;
/**
* Whether to exit context after a token method
*
* @var bool
*/
protected $_exitContext;
/**
* Create a new context
*
* @param Harmony_Parser_Abstract $parser
*/
public function __construct(Harmony_Parser_Abstract $parser, $params = array())
{
$this->_parser = $parser;
$this->_params = $params;
$this->_exitData = '';
$this->_exitContext = false;
$this->init();
}
/**
* Context logic when initialized
*/
protected function init()
{
}
/**
* Current parser
*
* @return Harmony_Parser_Abstract
*/
public function getParser()
{
return $this->_parser;
}
/**
* Checks if a parameter exists
*
* @param string $name
* @return bool
*/
public function hasParam($name)
{
return array_key_exists($name, $this->_params);
}
/**
* Gets a parameter's value
*
* @param string $name
* @return mixed
*/
public function getParam($name)
{
if (!$this->hasParam($name)) {
require_once 'Harmony/Parser/Exception.php';
throw new Harmony_Parser_Exception('Missing parameter ' . $name .
' in context ' . get_class($this));
}
return $this->_params[$name];
}
/**
* Gets all params
*
* @return array
*/
public function getParams()
{
return $this->_params;
}
/**
* Exit the context at the end of the current action
*
* @param mixed $data OPTIONAL
*/
public function exitContext($data = array())
{
$this->_exitData = $data;
$this->_exitContext = true;
}
/**
* Get tokens for the current context
*
* @return array
*/
public function _getTokens()
{
return $this->_tokens;
}
/**
* Execute the action associated to a token
*
* @param string $token
* @param string $data
* @return mixed
*/
public function _doActionForToken($token, $data)
{
$method = 'token' . ucfirst($token);
if (!method_exists($this, $method) && !method_exists($this, '__call')) {
return null;
}
$this->beforeToken();
$this->{$method}($data);
$this->afterToken();
return $this->_exitContext;
}
/**
* Gets data returned by context
*
* @var mixed
*/
public function _getExitData()
{
return $this->_exitData;
}
/**
* Method called before a specific token method
*/
protected function beforeToken()
{
}
/**
* Method called after a specific token method
*/
protected function afterToken()
{
}
/**
* End of file reached by parser
*/
public function tokenEof($data)
{
$this->exitContext();
}
}