PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/yii/framework/utils/CFormatter.php

https://bitbucket.org/Amit_Ashckenazi/unmatched-archives
PHP | 287 lines | 106 code | 21 blank | 160 comment | 7 complexity | cabc267a7dc46be42024c2bd7af7b2e1 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, LGPL-3.0, LGPL-2.1, BSD-2-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$
  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. * @var array the format used to format size (bytes). Two elements may be specified: "base" and "decimals".
  77. * They correspond to the base at which KiloByte is calculated (1000 or 1024) bytes per KiloByte and
  78. * the number of digits after decimal point.
  79. */
  80. public $sizeFormat=array(
  81. 'base'=>1024,
  82. 'decimals'=>2,
  83. );
  84. /**
  85. * Calls the format method when its shortcut is invoked.
  86. * This is a PHP magic method that we override to implement the shortcut format methods.
  87. * @param string $name the method name
  88. * @param array $parameters method parameters
  89. * @return mixed the method return value
  90. */
  91. public function __call($name,$parameters)
  92. {
  93. if(method_exists($this,'format'.$name))
  94. return call_user_func_array(array($this,'format'.$name),$parameters);
  95. else
  96. return parent::__call($name,$parameters);
  97. }
  98. /**
  99. * Formats a value based on the given type.
  100. * @param mixed $value the value to be formatted
  101. * @param string $type the data type. This must correspond to a format method available in CFormatter.
  102. * For example, we can use 'text' here because there is method named {@link formatText}.
  103. * @return string the formatted data
  104. */
  105. public function format($value,$type)
  106. {
  107. $method='format'.$type;
  108. if(method_exists($this,$method))
  109. return $this->$method($value);
  110. else
  111. throw new CException(Yii::t('yii','Unknown type "{type}".',array('{type}'=>$type)));
  112. }
  113. /**
  114. * Formats the value as is without any formatting.
  115. * This method simply returns back the parameter without any format.
  116. * @param mixed $value the value to be formatted
  117. * @return string the formatted result
  118. */
  119. public function formatRaw($value)
  120. {
  121. return $value;
  122. }
  123. /**
  124. * Formats the value as a HTML-encoded plain text.
  125. * @param mixed $value the value to be formatted
  126. * @return string the formatted result
  127. */
  128. public function formatText($value)
  129. {
  130. return CHtml::encode($value);
  131. }
  132. /**
  133. * Formats the value as a HTML-encoded plain text and converts newlines with HTML br tags.
  134. * @param mixed $value the value to be formatted
  135. * @return string the formatted result
  136. */
  137. public function formatNtext($value)
  138. {
  139. return nl2br(CHtml::encode($value));
  140. }
  141. /**
  142. * Formats the value as HTML text without any encoding.
  143. * @param mixed $value the value to be formatted
  144. * @return string the formatted result
  145. */
  146. public function formatHtml($value)
  147. {
  148. return $this->getHtmlPurifier()->purify($value);
  149. }
  150. /**
  151. * Formats the value as a date.
  152. * @param mixed $value the value to be formatted
  153. * @return string the formatted result
  154. * @see dateFormat
  155. */
  156. public function formatDate($value)
  157. {
  158. return date($this->dateFormat,$value);
  159. }
  160. /**
  161. * Formats the value as a time.
  162. * @param mixed $value the value to be formatted
  163. * @return string the formatted result
  164. * @see timeFormat
  165. */
  166. public function formatTime($value)
  167. {
  168. return date($this->timeFormat,$value);
  169. }
  170. /**
  171. * Formats the value as a date and time.
  172. * @param mixed $value the value to be formatted
  173. * @return string the formatted result
  174. * @see datetimeFormat
  175. */
  176. public function formatDatetime($value)
  177. {
  178. return date($this->datetimeFormat,$value);
  179. }
  180. /**
  181. * Formats the value as a boolean.
  182. * @param mixed $value the value to be formatted
  183. * @return string the formatted result
  184. * @see booleanFormat
  185. */
  186. public function formatBoolean($value)
  187. {
  188. return $value ? $this->booleanFormat[1] : $this->booleanFormat[0];
  189. }
  190. /**
  191. * Formats the value as a mailto link.
  192. * @param mixed $value the value to be formatted
  193. * @return string the formatted result
  194. */
  195. public function formatEmail($value)
  196. {
  197. return CHtml::mailto($value);
  198. }
  199. /**
  200. * Formats the value as an image tag.
  201. * @param mixed $value the value to be formatted
  202. * @return string the formatted result
  203. */
  204. public function formatImage($value)
  205. {
  206. return CHtml::image($value);
  207. }
  208. /**
  209. * Formats the value as a hyperlink.
  210. * @param mixed $value the value to be formatted
  211. * @return string the formatted result
  212. */
  213. public function formatUrl($value)
  214. {
  215. $url=$value;
  216. if(strpos($url,'http://')!==0 && strpos($url,'https://')!==0)
  217. $url='http://'.$url;
  218. return CHtml::link(CHtml::encode($value),$url);
  219. }
  220. /**
  221. * Formats the value as a number using PHP number_format() function.
  222. * @param mixed $value the value to be formatted
  223. * @return string the formatted result
  224. * @see numberFormat
  225. */
  226. public function formatNumber($value)
  227. {
  228. return number_format($value,$this->numberFormat['decimals'],$this->numberFormat['decimalSeparator'],$this->numberFormat['thousandSeparator']);
  229. }
  230. /**
  231. * @return CHtmlPurifier the HTML purifier instance
  232. */
  233. public function getHtmlPurifier()
  234. {
  235. if($this->_htmlPurifier===null)
  236. $this->_htmlPurifier=new CHtmlPurifier;
  237. return $this->_htmlPurifier;
  238. }
  239. /**
  240. * Formats the value in bytes as a size in human readable form.
  241. * @param integer $value value in bytes to be formatted
  242. * @param boolean $verbose if full names should be used (e.g. Bytes, KiloBytes, ...).
  243. * Defaults to false meaning that short names will be used (e.g. B, KB, ...).
  244. * @return string the formatted result
  245. */
  246. public function formatSize($value,$verbose=false)
  247. {
  248. $base=$this->sizeFormat['base'];
  249. for($i=0; $base<=$value && $i<5; $i++)
  250. $value=$value/$base;
  251. $value=round($value, $this->sizeFormat['decimals']);
  252. switch($i)
  253. {
  254. case 0:
  255. return $verbose ? Yii::t('size_units', '{n} Bytes', $value) : Yii::t('size_units', '{n} B', $value);
  256. case 1:
  257. return $verbose ? Yii::t('size_units', '{n} KiloBytes', $value) : Yii::t('size_units', '{n} KB', $value);
  258. case 2:
  259. return $verbose ? Yii::t('size_units', '{n} MegaBytes', $value) : Yii::t('size_units', '{n} MB', $value);
  260. case 3:
  261. return $verbose ? Yii::t('size_units', '{n} GigaBytes', $value) : Yii::t('size_units', '{n} GB', $value);
  262. default:
  263. return $verbose ? Yii::t('size_units', '{n} TeraBytes', $value) : Yii::t('size_units', '{n} TB', $value);
  264. }
  265. }
  266. }