PageRenderTime 212ms CodeModel.GetById 61ms app.highlight 76ms RepoModel.GetById 45ms app.codeStats 0ms

/demo/yii/utils/CFormatter.php

https://bitbucket.org/santosahw/yii-bootstrap
PHP | 247 lines | 82 code | 17 blank | 148 comment | 5 complexity | 33c30a1006e95570319341d8881f8434 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: CFormatter.php 3553 2012-02-06 22:07:58Z alexander.makarow $
 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	 * Calls the format method when its shortcut is invoked.
 80	 * This is a PHP magic method that we override to implement the shortcut format methods.
 81	 * @param string $name the method name
 82	 * @param array $parameters method parameters
 83	 * @return mixed the method return value
 84	 */
 85	public function __call($name,$parameters)
 86	{
 87		if(method_exists($this,'format'.$name))
 88			return call_user_func_array(array($this,'format'.$name),$parameters);
 89		else
 90			return parent::__call($name,$parameters);
 91	}
 92
 93	/**
 94	 * Formats a value based on the given type.
 95	 * @param mixed $value the value to be formatted
 96	 * @param string $type the data type. This must correspond to a format method available in CFormatter.
 97	 * For example, we can use 'text' here because there is method named {@link formatText}.
 98	 * @return string the formatted data
 99	 */
100	public function format($value,$type)
101	{
102		$method='format'.$type;
103		if(method_exists($this,$method))
104			return $this->$method($value);
105		else
106			throw new CException(Yii::t('yii','Unknown type "{type}".',array('{type}'=>$type)));
107	}
108
109	/**
110	 * Formats the value as is without any formatting.
111	 * This method simply returns back the parameter without any format.
112	 * @param mixed $value the value to be formatted
113	 * @return string the formatted result
114	 */
115	public function formatRaw($value)
116	{
117		return $value;
118	}
119
120	/**
121	 * Formats the value as a HTML-encoded plain text.
122	 * @param mixed $value the value to be formatted
123	 * @return string the formatted result
124	 */
125	public function formatText($value)
126	{
127		return CHtml::encode($value);
128	}
129
130	/**
131	 * Formats the value as a HTML-encoded plain text and converts newlines with HTML br tags.
132	 * @param mixed $value the value to be formatted
133	 * @return string the formatted result
134	 */
135	public function formatNtext($value)
136	{
137		return nl2br(CHtml::encode($value));
138	}
139
140	/**
141	 * Formats the value as HTML text without any encoding.
142	 * @param mixed $value the value to be formatted
143	 * @return string the formatted result
144	 */
145	public function formatHtml($value)
146	{
147		return $this->getHtmlPurifier()->purify($value);
148	}
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	/**
162	 * Formats the value as a time.
163	 * @param mixed $value the value to be formatted
164	 * @return string the formatted result
165	 * @see timeFormat
166	 */
167	public function formatTime($value)
168	{
169		return date($this->timeFormat,$value);
170	}
171
172	/**
173	 * Formats the value as a date and time.
174	 * @param mixed $value the value to be formatted
175	 * @return string the formatted result
176	 * @see datetimeFormat
177	 */
178	public function formatDatetime($value)
179	{
180		return date($this->datetimeFormat,$value);
181	}
182
183	/**
184	 * Formats the value as a boolean.
185	 * @param mixed $value the value to be formatted
186	 * @return string the formatted result
187	 * @see booleanFormat
188	 */
189	public function formatBoolean($value)
190	{
191		return $value ? $this->booleanFormat[1] : $this->booleanFormat[0];
192	}
193
194	/**
195	 * Formats the value as a mailto link.
196	 * @param mixed $value the value to be formatted
197	 * @return string the formatted result
198	 */
199	public function formatEmail($value)
200	{
201		return CHtml::mailto($value);
202	}
203
204	/**
205	 * Formats the value as an image tag.
206	 * @param mixed $value the value to be formatted
207	 * @return string the formatted result
208	 */
209	public function formatImage($value)
210	{
211		return CHtml::image($value);
212	}
213
214	/**
215	 * Formats the value as a hyperlink.
216	 * @param mixed $value the value to be formatted
217	 * @return string the formatted result
218	 */
219	public function formatUrl($value)
220	{
221		$url=$value;
222		if(strpos($url,'http://')!==0 && strpos($url,'https://')!==0)
223			$url='http://'.$url;
224		return CHtml::link(CHtml::encode($value),$url);
225	}
226
227	/**
228	 * Formats the value as a number using PHP number_format() function.
229	 * @param mixed $value the value to be formatted
230	 * @return string the formatted result
231	 * @see numberFormat
232	 */
233	public function formatNumber($value)
234	{
235		return number_format($value,$this->numberFormat['decimals'],$this->numberFormat['decimalSeparator'],$this->numberFormat['thousandSeparator']);
236	}
237
238	/**
239	 * @return CHtmlPurifier the HTML purifier instance
240	 */
241	public function getHtmlPurifier()
242	{
243		if($this->_htmlPurifier===null)
244			$this->_htmlPurifier=new CHtmlPurifier;
245		return $this->_htmlPurifier;
246	}
247}