PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/utils/CFormatter.php

https://bitbucket.org/dinhtrung/yiicorecms/
PHP | 246 lines | 82 code | 17 blank | 147 comment | 5 complexity | 8e26e330022117fe235c7852ddd1fbfd MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, CC0-1.0, BSD-2-Clause, GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * CFormatter class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2011 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CFormatter provides a set of commonly used data formatting methods.
  12. *
  13. * The formatting methods provided by CFormatter are all named in the form of <code>formatXyz</code>.
  14. * The behavior of some of them may be configured via the properties of CFormatter. For example,
  15. * by configuring {@link dateFormat}, one may control how {@link formatDate} formats the value into a date string.
  16. *
  17. * For convenience, CFormatter also implements the mechanism of calling formatting methods with their shortcuts (called types).
  18. * In particular, if a formatting method is named <code>formatXyz</code>, then its shortcut method is <code>xyz</code>
  19. * (case-insensitive). For example, calling <code>$formatter->date($value)</code> is equivalent to calling
  20. * <code>$formatter->formatDate($value)</code>.
  21. *
  22. * Currently, the following types are recognizable:
  23. * <ul>
  24. * <li>raw: the attribute value will not be changed at all.</li>
  25. * <li>text: the attribute value will be HTML-encoded when rendering.</li>
  26. * <li>ntext: the {@link formatNtext} method will be called to format the attribute value as a HTML-encoded plain text with newlines converted as the HTML &lt;br /&gt; tags.</li>
  27. * <li>html: the attribute value will be purified and then returned.</li>
  28. * <li>date: the {@link formatDate} method will be called to format the attribute value as a date.</li>
  29. * <li>time: the {@link formatTime} method will be called to format the attribute value as a time.</li>
  30. * <li>datetime: the {@link formatDatetime} method will be called to format the attribute value as a date with time.</li>
  31. * <li>boolean: the {@link formatBoolean} method will be called to format the attribute value as a boolean display.</li>
  32. * <li>number: the {@link formatNumber} method will be called to format the attribute value as a number display.</li>
  33. * <li>email: the {@link formatEmail} method will be called to format the attribute value as a mailto link.</li>
  34. * <li>image: the {@link formatImage} method will be called to format the attribute value as an image tag where the attribute value is the image URL.</li>
  35. * <li>url: the {@link formatUrl} method will be called to format the attribute value as a hyperlink where the attribute value is the URL.</li>
  36. * </ul>
  37. *
  38. * By default, {@link CApplication} registers {@link CFormatter} as an application component whose ID is 'format'.
  39. * Therefore, one may call <code>Yii::app()->format->boolean(1)</code>.
  40. *
  41. * @author Qiang Xue <qiang.xue@gmail.com>
  42. * @version $Id: CFormatter.php 2799 2011-01-01 19:31:13Z qiang.xue $
  43. * @package system.utils
  44. * @since 1.1.0
  45. */
  46. class CFormatter extends CApplicationComponent
  47. {
  48. private $_htmlPurifier;
  49. /**
  50. * @var string the format string to be used to format a date using PHP date() function. Defaults to 'Y/m/d'.
  51. */
  52. public $dateFormat='Y/m/d';
  53. /**
  54. * @var string the format string to be used to format a time using PHP date() function. Defaults to 'h:i:s A'.
  55. */
  56. public $timeFormat='h:i:s A';
  57. /**
  58. * @var string the format string to be used to format a date and time using PHP date() function. Defaults to 'Y/m/d h:i:s A'.
  59. */
  60. public $datetimeFormat='Y/m/d h:i:s A';
  61. /**
  62. * @var array the format used to format a number with PHP number_format() function.
  63. * Three elements may be specified: "decimals", "decimalSeparator" and "thousandSeparator". They
  64. * correspond to the number of digits after the decimal point, the character displayed as the decimal point,
  65. * and the thousands separator character.
  66. */
  67. public $numberFormat=array('decimals'=>null, 'decimalSeparator'=>null, 'thousandSeparator'=>null);
  68. /**
  69. * @var array the text to be displayed when formatting a boolean value. The first element corresponds
  70. * to the text display for false, the second element for true. Defaults to <code>array('No', 'Yes')</code>.
  71. */
  72. public $booleanFormat=array('No','Yes');
  73. /**
  74. * Calls the format method when its shortcut is invoked.
  75. * This is a PHP magic method that we override to implement the shortcut format methods.
  76. * @param string $name the method name
  77. * @param array $parameters method parameters
  78. * @return mixed the method return value
  79. */
  80. public function __call($name,$parameters)
  81. {
  82. if(method_exists($this,'format'.$name))
  83. return call_user_func_array(array($this,'format'.$name),$parameters);
  84. else
  85. return parent::__call($name,$parameters);
  86. }
  87. /**
  88. * Formats a value based on the given type.
  89. * @param mixed $value the value to be formatted
  90. * @param string $type the data type. This must correspond to a format method available in CFormatter.
  91. * For example, we can use 'text' here because there is method named {@link formatText}.
  92. * @return string the formatted data
  93. */
  94. public function format($value,$type)
  95. {
  96. $method='format'.$type;
  97. if(method_exists($this,$method))
  98. return $this->$method($value);
  99. else
  100. throw new CException(Yii::t('yii','Unknown type "{type}".',array('{type}'=>$type)));
  101. }
  102. /**
  103. * Formats the value as is without any formatting.
  104. * This method simply returns back the parameter without any format.
  105. * @param mixed $value the value to be formatted
  106. * @return string the formatted result
  107. */
  108. public function formatRaw($value)
  109. {
  110. return $value;
  111. }
  112. /**
  113. * Formats the value as a HTML-encoded plain text.
  114. * @param mixed $value the value to be formatted
  115. * @return string the formatted result
  116. */
  117. public function formatText($value)
  118. {
  119. return CHtml::encode($value);
  120. }
  121. /**
  122. * Formats the value as a HTML-encoded plain text and converts newlines with HTML br tags.
  123. * @param mixed $value the value to be formatted
  124. * @return string the formatted result
  125. */
  126. public function formatNtext($value)
  127. {
  128. return nl2br(CHtml::encode($value));
  129. }
  130. /**
  131. * Formats the value as HTML text without any encoding.
  132. * @param mixed $value the value to be formatted
  133. * @return string the formatted result
  134. */
  135. public function formatHtml($value)
  136. {
  137. return $this->getHtmlPurifier()->purify($value);
  138. }
  139. /**
  140. * Formats the value as a date.
  141. * @param mixed $value the value to be formatted
  142. * @return string the formatted result
  143. * @see dateFormat
  144. */
  145. public function formatDate($value)
  146. {
  147. return date($this->dateFormat,$value);
  148. }
  149. /**
  150. * Formats the value as a time.
  151. * @param mixed $value the value to be formatted
  152. * @return string the formatted result
  153. * @see timeFormat
  154. */
  155. public function formatTime($value)
  156. {
  157. return date($this->timeFormat,$value);
  158. }
  159. /**
  160. * Formats the value as a date and time.
  161. * @param mixed $value the value to be formatted
  162. * @return string the formatted result
  163. * @see datetimeFormat
  164. */
  165. public function formatDatetime($value)
  166. {
  167. return date($this->datetimeFormat,$value);
  168. }
  169. /**
  170. * Formats the value as a boolean.
  171. * @param mixed $value the value to be formatted
  172. * @return string the formatted result
  173. * @see trueText
  174. * @see falseText
  175. */
  176. public function formatBoolean($value)
  177. {
  178. return $value ? $this->booleanFormat[1] : $this->booleanFormat[0];
  179. }
  180. /**
  181. * Formats the value as a mailto link.
  182. * @param mixed $value the value to be formatted
  183. * @return string the formatted result
  184. */
  185. public function formatEmail($value)
  186. {
  187. return CHtml::mailto($value);
  188. }
  189. /**
  190. * Formats the value as an image tag.
  191. * @param mixed $value the value to be formatted
  192. * @return string the formatted result
  193. */
  194. public function formatImage($value)
  195. {
  196. return CHtml::image($value);
  197. }
  198. /**
  199. * Formats the value as a hyperlink.
  200. * @param mixed $value the value to be formatted
  201. * @return string the formatted result
  202. */
  203. public function formatUrl($value)
  204. {
  205. $url=$value;
  206. if(strpos($url,'http://')!==0 && strpos($url,'https://')!==0)
  207. $url='http://'.$url;
  208. return CHtml::link(CHtml::encode($value),$url);
  209. }
  210. /**
  211. * Formats the value as a number using PHP number_format() function.
  212. * @param mixed $value the value to be formatted
  213. * @return string the formatted result
  214. * @see numberFormat
  215. */
  216. public function formatNumber($value)
  217. {
  218. return number_format($value,$this->numberFormat['decimals'],$this->numberFormat['decimalSeparator'],$this->numberFormat['thousandSeparator']);
  219. }
  220. /**
  221. * @return CHtmlPurifier the HTML purifier instance
  222. */
  223. public function getHtmlPurifier()
  224. {
  225. if($this->_htmlPurifier===null)
  226. $this->_htmlPurifier=new CHtmlPurifier;
  227. return $this->_htmlPurifier;
  228. }
  229. }