. */ namespace Volkszaehler\View; use Volkszaehler\Interpreter; use Volkszaehler\Iterator; use Volkszaehler\Model; use Volkszaehler\View\HTTP; use Volkszaehler\Util; /** * Base class for all view formats * * @package default * @author Steffen Vogel * */ abstract class View { /** * @var integer round all values to x decimals */ const PRECISSION = 5; /** * @var HTTP\Request * @todo do we need this? why public? not via getter? */ public $request; /** * @var HTTP\Response */ protected $response; /** * Constructor * * @param HTTP\Request $request * @param HTTP\Response $response */ public function __construct(HTTP\Request $request, HTTP\Response $response) { $this->request = $request; $this->response = $response; // error & exception handling by view set_exception_handler(array($this, 'exceptionHandler')); set_error_handler(array($this, 'errorHandler')); } public function __desctruct() { restore_exception_handler(); restore_error_handler(); } /** * error & exception handling */ final public function errorHandler($errno, $errstr, $errfile, $errline) { $this->exceptionHandler(new \ErrorException($errstr, 0, $errno, $errfile, $errline)); } final public function exceptionHandler(\Exception $exception) { $this->addException($exception, Util\Debug::isActivated()); $code = ($exception->getCode() == 0 && HTTP\Response::getCodeDescription($exception->getCode())) ? 400 : $exception->getCode(); $this->response->setCode($code); $this->send(); die(); } public function send() { if (Util\Debug::isActivated()) { $this->addDebug(Util\Debug::getInstance()); } $this->render(); $this->response->send(); } /** * * @param mixed $data */ public function add($data) { if (isset($data)) { if ($data instanceof Interpreter\InterpreterInterface) { $this->addData($data); } elseif ($data instanceof Model\Entity) { $this->addEntity($data); } elseif ($data instanceof \Exception) { $this->addException($data); } elseif ($data instanceof Util\Debug) { $this->addDebug($data); } else { throw new \Exception('Can\'t show ' . get_class($data)); } } } protected abstract function render(); protected abstract function addData(Interpreter\InterpreterInterface $data); protected abstract function addEntity(Model\Entity $entity); protected abstract function addException(\Exception $exception); protected abstract function addDebug(Util\Debug $debug); } ?>