PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/yii/framework/utils/CFormatter.php

https://bitbucket.org/ddonthula/zurmoemail
PHP | 314 lines | 122 code | 21 blank | 171 comment | 11 complexity | 859d740f08ed40c23f9eb988e715618a MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0, LGPL-2.1, BSD-2-Clause, GPL-2.0, GPL-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. * @property CHtmlPurifier $htmlPurifier The HTML purifier instance.
  42. *
  43. * @author Qiang Xue <qiang.xue@gmail.com>
  44. * @package system.utils
  45. * @since 1.1.0
  46. */
  47. class CFormatter extends CApplicationComponent
  48. {
  49. /**
  50. * @var CHtmlPurifier
  51. */
  52. private $_htmlPurifier;
  53. /**
  54. * @var string the format string to be used to format a date using PHP date() function. Defaults to 'Y/m/d'.
  55. */
  56. public $dateFormat='Y/m/d';
  57. /**
  58. * @var string the format string to be used to format a time using PHP date() function. Defaults to 'h:i:s A'.
  59. */
  60. public $timeFormat='h:i:s A';
  61. /**
  62. * @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'.
  63. */
  64. public $datetimeFormat='Y/m/d h:i:s A';
  65. /**
  66. * @var array the format used to format a number with PHP number_format() function.
  67. * Three elements may be specified: "decimals", "decimalSeparator" and "thousandSeparator".
  68. * They correspond to the number of digits after the decimal point, the character displayed as the decimal point
  69. * and the thousands separator character.
  70. */
  71. public $numberFormat=array('decimals'=>null, 'decimalSeparator'=>null, 'thousandSeparator'=>null);
  72. /**
  73. * @var array the text to be displayed when formatting a boolean value. The first element corresponds
  74. * to the text display for false, the second element for true. Defaults to <code>array('No', 'Yes')</code>.
  75. */
  76. public $booleanFormat=array('No','Yes');
  77. /**
  78. * @var array the options to be passed to CHtmlPurifier instance used in this class. CHtmlPurifier is used
  79. * in {@link formatHtml} method, so this property could be useful to customize HTML filtering behavior.
  80. * @since 1.1.13
  81. */
  82. public $htmlPurifierOptions=array();
  83. /**
  84. * @var array the format used to format size (bytes). Three elements may be specified: "base", "decimals" and "decimalSeparator".
  85. * They correspond to the base at which a kilobyte is calculated (1000 or 1024 bytes per kilobyte, defaults to 1024),
  86. * the number of digits after the decimal point (defaults to 2) and the character displayed as the decimal point.
  87. * "decimalSeparator" is available since version 1.1.13
  88. * @since 1.1.11
  89. */
  90. public $sizeFormat=array(
  91. 'base'=>1024,
  92. 'decimals'=>2,
  93. 'decimalSeparator'=>null,
  94. );
  95. /**
  96. * Calls the format method when its shortcut is invoked.
  97. * This is a PHP magic method that we override to implement the shortcut format methods.
  98. * @param string $name the method name
  99. * @param array $parameters method parameters
  100. * @return mixed the method return value
  101. */
  102. public function __call($name,$parameters)
  103. {
  104. if(method_exists($this,'format'.$name))
  105. return call_user_func_array(array($this,'format'.$name),$parameters);
  106. else
  107. return parent::__call($name,$parameters);
  108. }
  109. /**
  110. * Formats a value based on the given type.
  111. * @param mixed $value the value to be formatted
  112. * @param string $type the data type. This must correspond to a format method available in CFormatter.
  113. * For example, we can use 'text' here because there is method named {@link formatText}.
  114. * @return string the formatted data
  115. */
  116. public function format($value,$type)
  117. {
  118. $method='format'.$type;
  119. if(method_exists($this,$method))
  120. return $this->$method($value);
  121. else
  122. throw new CException(Yii::t('yii','Unknown type "{type}".',array('{type}'=>$type)));
  123. }
  124. /**
  125. * Formats the value as is without any formatting.
  126. * This method simply returns back the parameter without any format.
  127. * @param mixed $value the value to be formatted
  128. * @return string the formatted result
  129. */
  130. public function formatRaw($value)
  131. {
  132. return $value;
  133. }
  134. /**
  135. * Formats the value as a HTML-encoded plain text.
  136. * @param mixed $value the value to be formatted
  137. * @return string the formatted result
  138. */
  139. public function formatText($value)
  140. {
  141. return CHtml::encode($value);
  142. }
  143. /**
  144. * Formats the value as a HTML-encoded plain text and converts newlines with HTML br tags.
  145. * @param mixed $value the value to be formatted
  146. * @return string the formatted result
  147. */
  148. public function formatNtext($value)
  149. {
  150. return nl2br(CHtml::encode($value));
  151. }
  152. /**
  153. * Formats the value as HTML text without any encoding.
  154. * @param mixed $value the value to be formatted
  155. * @return string the formatted result
  156. */
  157. public function formatHtml($value)
  158. {
  159. return $this->getHtmlPurifier()->purify($value);
  160. }
  161. /**
  162. * Formats the value as a date.
  163. * @param mixed $value the value to be formatted
  164. * @return string the formatted result
  165. * @see dateFormat
  166. */
  167. public function formatDate($value)
  168. {
  169. return date($this->dateFormat,$this->normalizeDateValue($value));
  170. }
  171. /**
  172. * Formats the value as a time.
  173. * @param mixed $value the value to be formatted
  174. * @return string the formatted result
  175. * @see timeFormat
  176. */
  177. public function formatTime($value)
  178. {
  179. return date($this->timeFormat,$this->normalizeDateValue($value));
  180. }
  181. /**
  182. * Formats the value as a date and time.
  183. * @param mixed $value the value to be formatted
  184. * @return string the formatted result
  185. * @see datetimeFormat
  186. */
  187. public function formatDatetime($value)
  188. {
  189. return date($this->datetimeFormat,$this->normalizeDateValue($value));
  190. }
  191. private function normalizeDateValue($time)
  192. {
  193. if(is_string($time))
  194. {
  195. if(ctype_digit($time) || ($time{0}=='-' && ctype_digit(substr($time, 1))))
  196. return (int)$time;
  197. else
  198. return strtotime($time);
  199. }
  200. return (int)$time;
  201. }
  202. /**
  203. * Formats the value as a boolean.
  204. * @param mixed $value the value to be formatted
  205. * @return string the formatted result
  206. * @see booleanFormat
  207. */
  208. public function formatBoolean($value)
  209. {
  210. return $value ? $this->booleanFormat[1] : $this->booleanFormat[0];
  211. }
  212. /**
  213. * Formats the value as a mailto link.
  214. * @param mixed $value the value to be formatted
  215. * @return string the formatted result
  216. */
  217. public function formatEmail($value)
  218. {
  219. return CHtml::mailto($value);
  220. }
  221. /**
  222. * Formats the value as an image tag.
  223. * @param mixed $value the value to be formatted
  224. * @return string the formatted result
  225. */
  226. public function formatImage($value)
  227. {
  228. return CHtml::image($value);
  229. }
  230. /**
  231. * Formats the value as a hyperlink.
  232. * @param mixed $value the value to be formatted
  233. * @return string the formatted result
  234. */
  235. public function formatUrl($value)
  236. {
  237. $url=$value;
  238. if(strpos($url,'http://')!==0 && strpos($url,'https://')!==0)
  239. $url='http://'.$url;
  240. return CHtml::link(CHtml::encode($value),$url);
  241. }
  242. /**
  243. * Formats the value as a number using PHP number_format() function.
  244. * @param mixed $value the value to be formatted
  245. * @return string the formatted result
  246. * @see numberFormat
  247. */
  248. public function formatNumber($value)
  249. {
  250. return number_format($value,$this->numberFormat['decimals'],$this->numberFormat['decimalSeparator'],$this->numberFormat['thousandSeparator']);
  251. }
  252. /**
  253. * @return CHtmlPurifier the HTML purifier instance
  254. */
  255. public function getHtmlPurifier()
  256. {
  257. if($this->_htmlPurifier===null)
  258. $this->_htmlPurifier=new CHtmlPurifier;
  259. $this->_htmlPurifier->options=$this->htmlPurifierOptions;
  260. return $this->_htmlPurifier;
  261. }
  262. /**
  263. * Formats the value in bytes as a size in human readable form.
  264. * @param integer $value value in bytes to be formatted
  265. * @param boolean $verbose if full names should be used (e.g. bytes, kilobytes, ...).
  266. * Defaults to false meaning that short names will be used (e.g. B, KB, ...).
  267. * @return string the formatted result
  268. * @see sizeFormat
  269. * @since 1.1.11
  270. */
  271. public function formatSize($value,$verbose=false)
  272. {
  273. $base=$this->sizeFormat['base'];
  274. for($i=0; $base<=$value && $i<5; $i++)
  275. $value=$value/$base;
  276. $value=round($value, $this->sizeFormat['decimals']);
  277. $formattedValue=isset($this->sizeFormat['decimalSeparator']) ? str_replace('.',$this->sizeFormat['decimalSeparator'],$value) : $value;
  278. $params=array($value,'{n}'=>$formattedValue);
  279. switch($i)
  280. {
  281. case 0:
  282. return $verbose ? Yii::t('yii','{n} byte|{n} bytes',$params) : Yii::t('yii', '{n} B',$params);
  283. case 1:
  284. return $verbose ? Yii::t('yii','{n} kilobyte|{n} kilobytes',$params) : Yii::t('yii','{n} KB',$params);
  285. case 2:
  286. return $verbose ? Yii::t('yii','{n} megabyte|{n} megabytes',$params) : Yii::t('yii','{n} MB',$params);
  287. case 3:
  288. return $verbose ? Yii::t('yii','{n} gigabyte|{n} gigabytes',$params) : Yii::t('yii','{n} GB',$params);
  289. default:
  290. return $verbose ? Yii::t('yii','{n} terabyte|{n} terabytes',$params) : Yii::t('yii','{n} TB',$params);
  291. }
  292. }
  293. }