HeadTitle.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ suicBAfwCUlOBFGfhVGUjHWDU1zF1nn+6pBi+ho1ZlA=
  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.0
  11. */
  12. /**
  13. * create head title html tag
  14. */
  15. namespace Cube\View\Helper;
  16. class HeadTitle extends AbstractHelper
  17. {
  18. /**
  19. * default separator
  20. */
  21. const DEFAULT_SEPARATOR = ' / ';
  22. /**
  23. * title operation types
  24. */
  25. const SET = 'set';
  26. const APPEND = 'append';
  27. const PREPEND = 'prepend';
  28. /**
  29. *
  30. * separator
  31. *
  32. * @var string
  33. */
  34. protected $_separator = self::DEFAULT_SEPARATOR;
  35. /**
  36. *
  37. * page container container
  38. *
  39. * @var array
  40. */
  41. protected $_container = array();
  42. /**
  43. *
  44. * head title method
  45. *
  46. * @param string $title
  47. * @param string $type
  48. *
  49. * @return $this
  50. */
  51. public function headTitle($title = null, $type = null)
  52. {
  53. if ($title !== null && !empty($title)) {
  54. switch ($type) {
  55. case self::PREPEND:
  56. $this->prepend($title);
  57. break;
  58. case self::SET:
  59. $this->set($title);
  60. break;
  61. default:
  62. $this->append($title);
  63. break;
  64. }
  65. }
  66. return $this;
  67. }
  68. /**
  69. *
  70. * append string to title container
  71. *
  72. * @param string $title
  73. *
  74. * @return $this
  75. */
  76. public function append($title)
  77. {
  78. array_push($this->_container, $title);
  79. return $this;
  80. }
  81. /**
  82. *
  83. * prepend string to title container
  84. *
  85. * @param string $title
  86. *
  87. * @return $this
  88. */
  89. public function prepend($title)
  90. {
  91. array_unshift($this->_container, $title);
  92. return $this;
  93. }
  94. /**
  95. *
  96. * set new title
  97. *
  98. * @param $title
  99. *
  100. * @return $this
  101. */
  102. public function set($title)
  103. {
  104. $this->clearContainer()
  105. ->append($title);
  106. return $this;
  107. }
  108. /**
  109. *
  110. * clear title container
  111. *
  112. * @return $this
  113. */
  114. public function clearContainer()
  115. {
  116. $this->_container = array();
  117. return $this;
  118. }
  119. /**
  120. *
  121. * set title separator
  122. *
  123. * @param string $separator
  124. *
  125. * @return $this
  126. */
  127. public function setSeparator($separator)
  128. {
  129. $this->_separator = $separator;
  130. return $this;
  131. }
  132. /**
  133. *
  134. * get title separator
  135. *
  136. * @return string
  137. */
  138. public function getSeparator()
  139. {
  140. return $this->_separator;
  141. }
  142. /**
  143. *
  144. * to string magic method
  145. *
  146. * enables <code>echo $this->headTitle(); </code>
  147. *
  148. * @return string
  149. */
  150. public function __toString()
  151. {
  152. return '<title>' . implode($this->_separator, $this->_container) . '</title>';
  153. }
  154. }