00001 <?php 00024 require_once 'Zend/View/Helper/Placeholder/Container/Standalone.php'; 00025 00036 class Zend_View_Helper_HeadMeta extends Zend_View_Helper_Placeholder_Container_Standalone 00037 { 00042 protected $_typeKeys = array('name', 'http-equiv'); 00043 protected $_requiredKeys = array('content'); 00044 protected $_modifierKeys = array('lang', 'scheme'); 00045 00049 protected $_regKey = 'Zend_View_Helper_HeadMeta'; 00050 00058 public function __construct() 00059 { 00060 parent::__construct(); 00061 $this->setSeparator(PHP_EOL); 00062 } 00063 00074 public function headMeta($content = null, $keyValue = null, $keyType = 'name', $modifiers = array(), $placement = Zend_View_Helper_Placeholder_Container_Abstract::APPEND) 00075 { 00076 if ((null !== $content) && (null !== $keyValue)) { 00077 $item = $this->createData($keyType, $keyValue, $content, $modifiers); 00078 $action = strtolower($placement); 00079 switch ($action) { 00080 case 'append': 00081 case 'prepend': 00082 case 'set': 00083 $this->$action($item); 00084 break; 00085 default: 00086 $this->append($item); 00087 break; 00088 } 00089 } 00090 00091 return $this; 00092 } 00093 00094 protected function _normalizeType($type) 00095 { 00096 switch ($type) { 00097 case 'Name': 00098 return 'name'; 00099 case 'HttpEquiv': 00100 return 'http-equiv'; 00101 default: 00102 require_once 'Zend/View/Exception.php'; 00103 throw new Zend_View_Exception(sprintf('Invalid type "%s" passed to _normalizeType', $type)); 00104 } 00105 } 00106 00124 public function __call($method, $args) 00125 { 00126 if (preg_match('/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv)$/', $method, $matches)) { 00127 $action = $matches['action']; 00128 $type = $this->_normalizeType($matches['type']); 00129 $argc = count($args); 00130 $index = null; 00131 00132 if ('offsetSet' == $action) { 00133 if (0 < $argc) { 00134 $index = array_shift($args); 00135 --$argc; 00136 } 00137 } 00138 00139 if (2 > $argc) { 00140 require_once 'Zend/View/Exception.php'; 00141 throw new Zend_View_Exception('Too few arguments provided; requires key value, and content'); 00142 } 00143 00144 if (3 > $argc) { 00145 $args[] = array(); 00146 } 00147 00148 $item = $this->createData($type, $args[0], $args[1], $args[2]); 00149 00150 if ('offsetSet' == $action) { 00151 return $this->offsetSet($index, $item); 00152 } 00153 00154 $this->$action($item); 00155 return $this; 00156 } 00157 00158 return parent::__call($method, $args); 00159 } 00160 00167 protected function _isValid($item) 00168 { 00169 if ((!$item instanceof stdClass) 00170 || !isset($item->type) 00171 || !isset($item->content) 00172 || !isset($item->modifiers)) 00173 { 00174 return false; 00175 } 00176 00177 return true; 00178 } 00179 00187 public function append($value) 00188 { 00189 if (!$this->_isValid($value)) { 00190 require_once 'Zend/View/Exception.php'; 00191 throw new Zend_View_Exception('Invalid value passed to append; please use appendMeta()'); 00192 } 00193 00194 return $this->getContainer()->append($value); 00195 } 00196 00205 public function offsetSet($index, $value) 00206 { 00207 if (!$this->_isValid($value)) { 00208 require_once 'Zend/View/Exception.php'; 00209 throw new Zend_View_Exception('Invalid value passed to offsetSet; please use offsetSetName() or offsetSetHttpEquiv()'); 00210 } 00211 00212 return $this->getContainer()->offsetSet($index, $value); 00213 } 00214 00222 public function offsetUnset($index) 00223 { 00224 if (!in_array($index, $this->getContainer()->getKeys())) { 00225 require_once 'Zend/View/Exception.php'; 00226 throw new Zend_View_Exception('Invalid index passed to offsetUnset.'); 00227 } 00228 00229 return $this->getContainer()->offsetUnset($index); 00230 } 00231 00239 public function prepend($value) 00240 { 00241 if (!$this->_isValid($value)) { 00242 require_once 'Zend/View/Exception.php'; 00243 throw new Zend_View_Exception('Invalid value passed to prepend; please use prependMeta()'); 00244 } 00245 00246 return $this->getContainer()->prepend($value); 00247 } 00248 00256 public function set($value) 00257 { 00258 if (!$this->_isValid($value)) { 00259 require_once 'Zend/View/Exception.php'; 00260 throw new Zend_View_Exception('Invalid value passed to set; please use setMeta()'); 00261 } 00262 00263 $container = $this->getContainer(); 00264 foreach ($container->getArrayCopy() as $index => $item) { 00265 if ($item->type == $value->type && $item->{$item->type} == $value->{$value->type}) { 00266 $this->offsetUnset($index); 00267 } 00268 } 00269 00270 return $this->append($value); 00271 } 00272 00282 public function itemToString(stdClass $item) 00283 { 00284 if (!in_array($item->type, $this->_typeKeys)) { 00285 require_once 'Zend/View/Exception.php'; 00286 throw new Zend_View_Exception(sprintf('Invalid type "%s" provided for meta', $item->type)); 00287 } 00288 $type = $item->type; 00289 00290 $modifiersString = ''; 00291 foreach ($item->modifiers as $key => $value) { 00292 if (!in_array($key, $this->_modifierKeys)) { 00293 continue; 00294 } 00295 $modifiersString .= $key . '="' . $this->_escape($value) . '" '; 00296 } 00297 00298 if ($this->view instanceof Zend_View_Abstract) { 00299 $tpl = ($this->view->doctype()->isXhtml()) 00300 ? '<meta %s="%s" content="%s" %s/>' 00301 : '<meta %s="%s" content="%s" %s>'; 00302 } else { 00303 $tpl = '<meta %s="%s" content="%s" %s/>'; 00304 } 00305 00306 $meta = sprintf( 00307 $tpl, 00308 $type, 00309 $this->_escape($item->$type), 00310 $this->_escape($item->content), 00311 $modifiersString 00312 ); 00313 return $meta; 00314 } 00315 00322 public function toString($indent = null) 00323 { 00324 $indent = (null !== $indent) 00325 ? $this->getWhitespace($indent) 00326 : $this->getIndent(); 00327 00328 $items = array(); 00329 $this->getContainer()->ksort(); 00330 try { 00331 foreach ($this as $item) { 00332 $items[] = $this->itemToString($item); 00333 } 00334 } catch (Zend_View_Exception $e) { 00335 trigger_error($e->getMessage(), E_USER_WARNING); 00336 return ''; 00337 } 00338 return $indent . implode($this->_escape($this->getSeparator()) . $indent, $items); 00339 } 00340 00350 public function createData($type, $typeValue, $content, array $modifiers) 00351 { 00352 $data = new stdClass; 00353 $data->type = $type; 00354 $data->$type = $typeValue; 00355 $data->content = $content; 00356 $data->modifiers = $modifiers; 00357 return $data; 00358 } 00359 }