HeadMeta.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ DM2KFGhyJjGoqYCfWghrv8gG1/c3mQiYRPKqQ+iPHkQ=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2014 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.1
  11. */
  12. /**
  13. * create head meta html tags
  14. */
  15. namespace Cube\View\Helper;
  16. class HeadMeta extends AbstractHelper
  17. {
  18. /**
  19. * title operation types
  20. */
  21. const SET = 'set';
  22. const APPEND = 'append';
  23. const PREPEND = 'prepend';
  24. /**
  25. *
  26. * page container container
  27. *
  28. * @var array
  29. */
  30. protected $_container = array();
  31. /**
  32. *
  33. * head meta method
  34. * only a proxy to the class - all calls are done through the magic method
  35. *
  36. * @return $this
  37. */
  38. public function headMeta()
  39. {
  40. return $this;
  41. }
  42. /**
  43. *
  44. * call magic method, enables the calling of the following methods:
  45. * append|prepend - Name|HttpEquiv|Charset
  46. *
  47. * @param string $method
  48. * @param array $args
  49. *
  50. * @throws \InvalidArgumentException
  51. * @return $this
  52. */
  53. public function __call($method, $args)
  54. {
  55. if (preg_match('/^(?P<action>set|(pre|ap)pend)(?P<type>Name|HttpEquiv|Property|Charset)$/', $method, $matches)
  56. ) {
  57. $action = '_' . $matches['action'];
  58. $type = strtolower(
  59. preg_replace("/([a-z])([A-Z])/", '$1-$2', $matches['type']));
  60. $nbArgs = count($args);
  61. if ($nbArgs < 1 || $nbArgs > 2) {
  62. throw new \InvalidArgumentException("Invalid number of arguments given in headMeta function call.");
  63. }
  64. if (!isset($args[1])) {
  65. $args[1] = null;
  66. }
  67. $data = $this->_createData($type, $args[0], $args[1]);
  68. $this->$action($data);
  69. }
  70. return $this;
  71. }
  72. /**
  73. *
  74. * append data to meta tags container
  75. *
  76. * @param array $data
  77. *
  78. * @return $this
  79. */
  80. protected function _append($data)
  81. {
  82. array_push($this->_container, $data);
  83. return $this;
  84. }
  85. /**
  86. *
  87. * prepend data to meta tags container
  88. *
  89. * @param string $data
  90. *
  91. * @return $this
  92. */
  93. protected function _prepend($data)
  94. {
  95. array_unshift($this->_container, $data);
  96. return $this;
  97. }
  98. /**
  99. *
  100. * set data to meta tags container - will first remove all tags that have the same key name
  101. *
  102. * @param array $data
  103. *
  104. * @return $this
  105. */
  106. protected function _set($data)
  107. {
  108. foreach ($this->_container as $key => $item) {
  109. if (strcmp($item['value'], $data['value']) === 0 && strcmp($item['key'], $data['key']) === 0) {
  110. unset($this->_container[$key]);
  111. }
  112. }
  113. return $this->_append($data);
  114. }
  115. /**
  116. *
  117. * clear meta tags container
  118. *
  119. * @return $this
  120. */
  121. public function clearContainer()
  122. {
  123. $this->_container = array();
  124. return $this;
  125. }
  126. /**
  127. *
  128. * format data to add to the container
  129. *
  130. * @param string $keyName
  131. * @param string $keyValue
  132. * @param string $content
  133. *
  134. * @return array
  135. */
  136. protected function _createData($keyName, $keyValue, $content)
  137. {
  138. return array(
  139. 'key' => $keyName,
  140. 'value' => $keyValue,
  141. 'content' => $content,
  142. );
  143. }
  144. /**
  145. *
  146. * to string magic method
  147. * only outputs meta tags with key/value/content if content is not empty
  148. *
  149. * enables <code>echo $this->headMeta(); </code>
  150. *
  151. * @return string
  152. */
  153. public function __toString()
  154. {
  155. $output = null;
  156. foreach ($this->_container as $item) {
  157. if (isset($item['content'])) {
  158. if (!empty($item['content'])) {
  159. $output .= sprintf('<meta %s="%s" content="%s">', $item['key'], $item['value'],
  160. str_ireplace('"', '', $item['content'])) . "\n";
  161. }
  162. }
  163. else {
  164. $output .= sprintf('<meta %s="%s">', $item['key'], $item['value']) . "\n";
  165. }
  166. }
  167. return strval($output);
  168. }
  169. /**
  170. *
  171. * normalize head meta type call
  172. *
  173. * @param string $type
  174. *
  175. * @return string
  176. * @throws \InvalidArgumentException
  177. */
  178. protected function _normalizeType($type)
  179. {
  180. switch ($type) {
  181. case 'Name':
  182. return 'name';
  183. case 'HttpEquiv':
  184. return 'http-equiv';
  185. default:
  186. throw new \InvalidArgumentException("Invalid headMeta type function called.");
  187. }
  188. }
  189. }