Skip to content

Instantly share code, notes, and snippets.

@davidcoallier
Created July 14, 2010 21:34
Show Gist options
  • Save davidcoallier/476122 to your computer and use it in GitHub Desktop.
Save davidcoallier/476122 to your computer and use it in GitHub Desktop.
<?php
class Vendor_View_Helper_AutoEscape
{
protected $_unescapedVars = null;
protected $_view;
public function setView(Zend_View_Interface $view)
{
$this->_view = $view;
}
public function autoEscape()
{
if ($this->_unescapedVars === null) {
$vars = $this->_view->getVars();
$this->_unescapedVars = $vars;
foreach ($vars as $k => $v) {
$this->_view->$k = $this->escapeDeep($v);
}
} else {
$this->_view->assign($this->_unescapedVars);
$this->_unescapedVars = null;
}
}
/**
* Escapes strings and recursively iterates over arrays to escape all entries.
*
* @param mixed $original
* @return mixed
*/
protected function escapeDeep(&$original) {
if (is_string($original))
return $this->_view->escape($original);
elseif (is_array($original)) {
foreach ($original as $k => $v) {
$original[$k] = $this->escapeDeep($v);
}
}
return $original;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment