PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/yii-1.1.10.r3566/framework/utils/CFormatter.php

https://bitbucket.org/zadoev/yii-skeleton
PHP | 247 lines | 82 code | 17 blank | 148 comment | 5 complexity | 33c30a1006e95570319341d8881f8434 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-2-Clause, BSD-3-Clause
  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. * @property CHtmlPurifier $htmlPurifier The HTML purifier instance.
  42. *
  43. * @author Qiang Xue <qiang.xue@gmail.com>
  44. * @version $Id: CFormatter.php 3553 2012-02-06 22:07:58Z alexander.makarow $
  45. * @package system.utils
  46. * @since 1.1.0
  47. */
  48. class CFormatter extends CApplicationComponent
  49. {
  50. private $_htmlPurifier;
  51. /**
  52. * @var string the format string to be used to format a date using PHP date() function. Defaults to 'Y/m/d'.
  53. */
  54. public $dateFormat='Y/m/d';
  55. /**
  56. * @var string the format string to be used to format a time using PHP date() function. Defaults to 'h:i:s A'.
  57. */
  58. public $timeFormat='h:i:s A';
  59. /**
  60. * @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'.
  61. */
  62. public $datetimeFormat='Y/m/d h:i:s A';
  63. /**
  64. * @var array the format used to format a number with PHP number_format() function.
  65. * Three elements may be specified: "decimals", "decimalSeparator" and "thousandSeparator". They
  66. * correspond to the number of digits after the decimal point, the character displayed as the decimal point,
  67. * and the thousands separator character.
  68. */
  69. public $numberFormat=array('decimals'=>null, 'decimalSeparator'=>null, 'thousandSeparator'=>null);
  70. /**
  71. * @var array the text to be displayed when formatting a boolean value. The first element corresponds
  72. * to the text display for false, the second element for true. Defaults to <code>array('No', 'Yes')</code>.
  73. */
  74. public $booleanFormat=array('No','Yes');
  75. /**
  76. * Calls the format method when its shortcut is invoked.
  77. * This is a PHP magic method that we override to implement the shortcut format methods.
  78. * @param string $name the method name
  79. * @param array $parameters method parameters
  80. * @return mixed the method return value
  81. */
  82. public function __call($name,$parameters)
  83. {
  84. if(method_exists($this,'format'.$name))
  85. return call_user_func_array(array($this,'format'.$name),$parameters);
  86. else
  87. return parent::__call($name,$parameters);
  88. }
  89. /**
  90. * Formats a value based on the given type.
  91. * @param mixed $value the value to be formatted
  92. * @param string $type the data type. This must correspond to a format method available in CFormatter.
  93. * For example, we can use 'text' here because there is method named {@link formatText}.
  94. * @return string the formatted data
  95. */
  96. public function format($value,$type)
  97. {
  98. $method='format'.$type;
  99. if(method_exists($this,$method))
  100. return $this->$method($value);
  101. else
  102. throw new CException(Yii::t('yii','Unknown type "{type}".',array('{type}'=>$type)));
  103. }
  104. /**
  105. * Formats the value as is without any formatting.
  106. * This method simply returns back the parameter without any format.
  107. * @param mixed $value the value to be formatted
  108. * @return string the formatted result
  109. */
  110. public function formatRaw($value)
  111. {
  112. return $value;
  113. }
  114. /**
  115. * Formats the value as a HTML-encoded plain text.
  116. * @param mixed $value the value to be formatted
  117. * @return string the formatted result
  118. */
  119. public function formatText($value)
  120. {
  121. return CHtml::encode($value);
  122. }
  123. /**
  124. * Formats the value as a HTML-encoded plain text and converts newlines with HTML br tags.
  125. * @param mixed $value the value to be formatted
  126. * @return string the formatted result
  127. */
  128. public function formatNtext($value)
  129. {
  130. return nl2br(CHtml::encode($value));
  131. }
  132. /**
  133. * Formats the value as HTML text without any encoding.
  134. * @param mixed $value the value to be formatted
  135. * @return string the formatted result
  136. */
  137. public function formatHtml($value)
  138. {
  139. return $this->getHtmlPurifier()->purify($value);
  140. }
  141. /**
  142. * Formats the value as a date.
  143. * @param mixed $value the value to be formatted
  144. * @return string the formatted result
  145. * @see dateFormat
  146. */
  147. public function formatDate($value)
  148. {
  149. return date($this->dateFormat,$value);
  150. }
  151. /**
  152. * Formats the value as a time.
  153. * @param mixed $value the value to be formatted
  154. * @return string the formatted result
  155. * @see timeFormat
  156. */
  157. public function formatTime($value)
  158. {
  159. return date($this->timeFormat,$value);
  160. }
  161. /**
  162. * Formats the value as a date and time.
  163. * @param mixed $value the value to be formatted
  164. * @return string the formatted result
  165. * @see datetimeFormat
  166. */
  167. public function formatDatetime($value)
  168. {
  169. return date($this->datetimeFormat,$value);
  170. }
  171. /**
  172. * Formats the value as a boolean.
  173. * @param mixed $value the value to be formatted
  174. * @return string the formatted result
  175. * @see booleanFormat
  176. */
  177. public function formatBoolean($value)
  178. {
  179. return $value ? $this->booleanFormat[1] : $this->booleanFormat[0];
  180. }
  181. /**
  182. * Formats the value as a mailto link.
  183. * @param mixed $value the value to be formatted
  184. * @return string the formatted result
  185. */
  186. public function formatEmail($value)
  187. {
  188. return CHtml::mailto($value);
  189. }
  190. /**
  191. * Formats the value as an image tag.
  192. * @param mixed $value the value to be formatted
  193. * @return string the formatted result
  194. */
  195. public function formatImage($value)
  196. {
  197. return CHtml::image($value);
  198. }
  199. /**
  200. * Formats the value as a hyperlink.
  201. * @param mixed $value the value to be formatted
  202. * @return string the formatted result
  203. */
  204. public function formatUrl($value)
  205. {
  206. $url=$value;
  207. if(strpos($url,'http://')!==0 && strpos($url,'https://')!==0)
  208. $url='http://'.$url;
  209. return CHtml::link(CHtml::encode($value),$url);
  210. }
  211. /**
  212. * Formats the value as a number using PHP number_format() function.
  213. * @param mixed $value the value to be formatted
  214. * @return string the formatted result
  215. * @see numberFormat
  216. */
  217. public function formatNumber($value)
  218. {
  219. return number_format($value,$this->numberFormat['decimals'],$this->numberFormat['decimalSeparator'],$this->numberFormat['thousandSeparator']);
  220. }
  221. /**
  222. * @return CHtmlPurifier the HTML purifier instance
  223. */
  224. public function getHtmlPurifier()
  225. {
  226. if($this->_htmlPurifier===null)
  227. $this->_htmlPurifier=new CHtmlPurifier;
  228. return $this->_htmlPurifier;
  229. }
  230. }