PageRenderTime 77ms CodeModel.GetById 64ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/demo/yii/utils/CFormatter.php

https://bitbucket.org/manuel_84/yii-bootstrap
PHP | 287 lines | 106 code | 21 blank | 160 comment | 7 complexity | cabc267a7dc46be42024c2bd7af7b2e1 MD5 | raw file
  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/**
 12 * CFormatter provides a set of commonly used data formatting methods.
 13 *
 14 * The formatting methods provided by CFormatter are all named in the form of <code>formatXyz</code>.
 15 * The behavior of some of them may be configured via the properties of CFormatter. For example,
 16 * by configuring {@link dateFormat}, one may control how {@link formatDate} formats the value into a date string.
 17 *
 18 * For convenience, CFormatter also implements the mechanism of calling formatting methods with their shortcuts (called types).
 19 * In particular, if a formatting method is named <code>formatXyz</code>, then its shortcut method is <code>xyz</code>
 20 * (case-insensitive). For example, calling <code>$formatter->date($value)</code> is equivalent to calling
 21 * <code>$formatter->formatDate($value)</code>.
 22 *
 23 * Currently, the following types are recognizable:
 24 * <ul>
 25 * <li>raw: the attribute value will not be changed at all.</li>
 26 * <li>text: the attribute value will be HTML-encoded when rendering.</li>
 27 * <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>
 28 * <li>html: the attribute value will be purified and then returned.</li>
 29 * <li>date: the {@link formatDate} method will be called to format the attribute value as a date.</li>
 30 * <li>time: the {@link formatTime} method will be called to format the attribute value as a time.</li>
 31 * <li>datetime: the {@link formatDatetime} method will be called to format the attribute value as a date with time.</li>
 32 * <li>boolean: the {@link formatBoolean} method will be called to format the attribute value as a boolean display.</li>
 33 * <li>number: the {@link formatNumber} method will be called to format the attribute value as a number display.</li>
 34 * <li>email: the {@link formatEmail} method will be called to format the attribute value as a mailto link.</li>
 35 * <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>
 36 * <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>
 37 * </ul>
 38 *
 39 * By default, {@link CApplication} registers {@link CFormatter} as an application component whose ID is 'format'.
 40 * Therefore, one may call <code>Yii::app()->format->boolean(1)</code>.
 41 *
 42 * @property CHtmlPurifier $htmlPurifier The HTML purifier instance.
 43 *
 44 * @author Qiang Xue <qiang.xue@gmail.com>
 45 * @version $Id$
 46 * @package system.utils
 47 * @since 1.1.0
 48 */
 49class CFormatter extends CApplicationComponent
 50{
 51	private $_htmlPurifier;
 52
 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". They
 68	 * 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	/**
 79	 * @var array the format used to format size (bytes). Two elements may be specified: "base" and "decimals".
 80	 * They correspond to the base at which KiloByte is calculated (1000 or 1024) bytes per KiloByte and
 81	 * the number of digits after decimal point.
 82	 */
 83	public $sizeFormat=array(
 84		'base'=>1024,
 85		'decimals'=>2,
 86	);
 87
 88	/**
 89	 * Calls the format method when its shortcut is invoked.
 90	 * This is a PHP magic method that we override to implement the shortcut format methods.
 91	 * @param string $name the method name
 92	 * @param array $parameters method parameters
 93	 * @return mixed the method return value
 94	 */
 95	public function __call($name,$parameters)
 96	{
 97		if(method_exists($this,'format'.$name))
 98			return call_user_func_array(array($this,'format'.$name),$parameters);
 99		else
100			return parent::__call($name,$parameters);
101	}
102
103	/**
104	 * Formats a value based on the given type.
105	 * @param mixed $value the value to be formatted
106	 * @param string $type the data type. This must correspond to a format method available in CFormatter.
107	 * For example, we can use 'text' here because there is method named {@link formatText}.
108	 * @return string the formatted data
109	 */
110	public function format($value,$type)
111	{
112		$method='format'.$type;
113		if(method_exists($this,$method))
114			return $this->$method($value);
115		else
116			throw new CException(Yii::t('yii','Unknown type "{type}".',array('{type}'=>$type)));
117	}
118
119	/**
120	 * Formats the value as is without any formatting.
121	 * This method simply returns back the parameter without any format.
122	 * @param mixed $value the value to be formatted
123	 * @return string the formatted result
124	 */
125	public function formatRaw($value)
126	{
127		return $value;
128	}
129
130	/**
131	 * Formats the value as a HTML-encoded plain text.
132	 * @param mixed $value the value to be formatted
133	 * @return string the formatted result
134	 */
135	public function formatText($value)
136	{
137		return CHtml::encode($value);
138	}
139
140	/**
141	 * Formats the value as a HTML-encoded plain text and converts newlines with HTML br tags.
142	 * @param mixed $value the value to be formatted
143	 * @return string the formatted result
144	 */
145	public function formatNtext($value)
146	{
147		return nl2br(CHtml::encode($value));
148	}
149
150	/**
151	 * Formats the value as HTML text without any encoding.
152	 * @param mixed $value the value to be formatted
153	 * @return string the formatted result
154	 */
155	public function formatHtml($value)
156	{
157		return $this->getHtmlPurifier()->purify($value);
158	}
159
160	/**
161	 * Formats the value as a date.
162	 * @param mixed $value the value to be formatted
163	 * @return string the formatted result
164	 * @see dateFormat
165	 */
166	public function formatDate($value)
167	{
168		return date($this->dateFormat,$value);
169	}
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,$value);
180	}
181
182	/**
183	 * Formats the value as a date and time.
184	 * @param mixed $value the value to be formatted
185	 * @return string the formatted result
186	 * @see datetimeFormat
187	 */
188	public function formatDatetime($value)
189	{
190		return date($this->datetimeFormat,$value);
191	}
192
193	/**
194	 * Formats the value as a boolean.
195	 * @param mixed $value the value to be formatted
196	 * @return string the formatted result
197	 * @see booleanFormat
198	 */
199	public function formatBoolean($value)
200	{
201		return $value ? $this->booleanFormat[1] : $this->booleanFormat[0];
202	}
203
204	/**
205	 * Formats the value as a mailto link.
206	 * @param mixed $value the value to be formatted
207	 * @return string the formatted result
208	 */
209	public function formatEmail($value)
210	{
211		return CHtml::mailto($value);
212	}
213
214	/**
215	 * Formats the value as an image tag.
216	 * @param mixed $value the value to be formatted
217	 * @return string the formatted result
218	 */
219	public function formatImage($value)
220	{
221		return CHtml::image($value);
222	}
223
224	/**
225	 * Formats the value as a hyperlink.
226	 * @param mixed $value the value to be formatted
227	 * @return string the formatted result
228	 */
229	public function formatUrl($value)
230	{
231		$url=$value;
232		if(strpos($url,'http://')!==0 && strpos($url,'https://')!==0)
233			$url='http://'.$url;
234		return CHtml::link(CHtml::encode($value),$url);
235	}
236
237	/**
238	 * Formats the value as a number using PHP number_format() function.
239	 * @param mixed $value the value to be formatted
240	 * @return string the formatted result
241	 * @see numberFormat
242	 */
243	public function formatNumber($value)
244	{
245		return number_format($value,$this->numberFormat['decimals'],$this->numberFormat['decimalSeparator'],$this->numberFormat['thousandSeparator']);
246	}
247
248	/**
249	 * @return CHtmlPurifier the HTML purifier instance
250	 */
251	public function getHtmlPurifier()
252	{
253		if($this->_htmlPurifier===null)
254			$this->_htmlPurifier=new CHtmlPurifier;
255		return $this->_htmlPurifier;
256	}
257
258	/**
259	 * Formats the value in bytes as a size in human readable form.
260	 * @param integer $value value in bytes to be formatted
261	 * @param boolean $verbose if full names should be used (e.g. Bytes, KiloBytes, ...).
262	 * Defaults to false meaning that short names will be used (e.g. B, KB, ...).
263	 * @return string the formatted result
264	 */
265	public function formatSize($value,$verbose=false)
266	{
267		$base=$this->sizeFormat['base'];
268		for($i=0; $base<=$value && $i<5; $i++)
269			$value=$value/$base;
270
271		$value=round($value, $this->sizeFormat['decimals']);
272
273		switch($i)
274		{
275			case 0:
276				return $verbose ? Yii::t('size_units', '{n} Bytes', $value) : Yii::t('size_units', '{n} B', $value);
277			case 1:
278				return $verbose ? Yii::t('size_units', '{n} KiloBytes', $value) : Yii::t('size_units', '{n} KB', $value);
279			case 2:
280				return $verbose ? Yii::t('size_units', '{n} MegaBytes', $value) : Yii::t('size_units', '{n} MB', $value);
281			case 3:
282				return $verbose ? Yii::t('size_units', '{n} GigaBytes', $value) : Yii::t('size_units', '{n} GB', $value);
283			default:
284				return $verbose ? Yii::t('size_units', '{n} TeraBytes', $value) : Yii::t('size_units', '{n} TB', $value);
285		}
286	}
287}