summaryrefslogtreecommitdiff
path: root/framework/I18N/core/NumberFormatInfo.php
blob: 136a20eb7072a16a2da9cd86265c09eeed833f8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
<?php

/**
 * NumberFormatInfo class file.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the BSD License.
 *
 * Copyright(c) 2004 by Qiang Xue. All rights reserved.
 *
 * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
 * The latest version of PRADO can be obtained from:
 * {@link http://prado.sourceforge.net/}
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version $Revision: 1.3 $  $Date: 2005/08/04 05:27:19 $
 * @package System.I18N.core
 */

/**
 * Get the CultureInfo class file.
 */
require_once(dirname(__FILE__).'/CultureInfo.php');

/**
 * NumberFormatInfo class
 *
 * Defines how numeric values are formatted and displayed,
 * depending on the culture. Numeric values are formatted using
 * standard or custom patterns stored in the properties of a
 * NumberFormatInfo.
 *
 * This class contains information, such as currency, decimal
 * separators, and other numeric symbols.
 *
 * To create a NumberFormatInfo for a specific culture,
 * create a CultureInfo for that culture and retrieve the
 * CultureInfo->NumberFormat property. Or use
 * NumberFormatInfo::getInstance($culture).
 * To create a NumberFormatInfo for the invariant culture, use the
 * InvariantInfo::getInvariantInfo().
 *
 *
 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version v1.0, last update on Sun Dec 05 14:48:26 EST 2004
 * @package System.I18N.core
 */
class NumberFormatInfo
{

	/**
	 * ICU number formatting data.
	 * @var array
	 */
	private $data = array();

	/**
	 * A list of properties that are accessable/writable.
	 * @var array
	 */
	protected $properties = array();

	/**
	 * The number pattern.
	 * @var array
	 */
	protected $pattern = array();

	const DECIMAL = 0;
	const CURRENCY = 1;
	const PERCENTAGE = 2;
	const SCIENTIFIC = 3;

	/**
	 * Allow functions that begins with 'set' to be called directly
	 * as an attribute/property to retrieve the value.
	 * @return mixed
	 */
	public function __get($name)
	{
		$getProperty = 'get'.$name;
		if(in_array($getProperty, $this->properties))
			return $this->$getProperty();
		else
			throw new Exception('Property '.$name.' does not exists.');
	}

	/**
	 * Allow functions that begins with 'set' to be called directly
	 * as an attribute/property to set the value.
	 */
	public function __set($name, $value)
	{
		$setProperty = 'set'.$name;
		if(in_array($setProperty, $this->properties))
			$this->$setProperty($value);
		else
			throw new Exception('Property '.$name.' can not be set.');
	}

	/**
	 * Initializes a new writable instance of the NumberFormatInfo class
	 * that is dependent on the ICU data for number, decimal, and currency
	 * formatting information. <b>N.B.</b>You should not initialize this
	 * class directly unless you know what you are doing. Please use use
	 * NumberFormatInfo::getInstance() to create an instance.
	 * @param array ICU data for date time formatting.
	 * @see getInstance()
	 */
	public function __construct($data=array(), $type=NumberFormatInfo::DECIMAL)
	{
		$this->properties = get_class_methods($this);

		if(empty($data))
			throw new Exception('Please provide the ICU data to initialize.');

		$this->data = $data;

		$this->setPattern($type);
	}

	/**
	 * Set the pattern for a specific number pattern. The validate patterns
	 * NumberFormatInfo::DECIMAL, NumberFormatInfo::CURRENCY,
     * NumberFormatInfo::PERCENTAGE, or NumberFormatInfo::SCIENTIFIC
	 * @param int pattern type.
	 */
	public function setPattern($type=NumberFormatInfo::DECIMAL)
	{
		if(is_int($type))
			$this->pattern =
				$this->parsePattern($this->data['NumberPatterns'][$type]);
		else
			$this->pattern = $this->parsePattern($type);

		$this->pattern['negInfty'] =
			$this->data['NumberElements'][6].
			$this->data['NumberElements'][9];

		$this->pattern['posInfty'] =
			$this->data['NumberElements'][11].
			$this->data['NumberElements'][9];
	}

	public function getPattern()
	{
		return $this->pattern;
	}

	/**
	 * Gets the default NumberFormatInfo that is culture-independent
	 * (invariant).
	 * @return NumberFormatInfo default NumberFormatInfo.
	 */
    public static function getInvariantInfo($type=NumberFormatInfo::DECIMAL)
    {
        static $invariant;
		if($invariant === null)
        {
            $culture = CultureInfo::getInvariantCulture();
            $invariant = $culture->NumberFormat;
            $invariant->setPattern($type);
        }
		return $invariant;
    }

    /**
     * Returns the NumberFormatInfo associated with the specified culture.
     * @param CultureInfo the culture that gets the NumberFormat property.
     * @param int the number formatting type, it should be
     * NumberFormatInfo::DECIMAL, NumberFormatInfo::CURRENCY,
     * NumberFormatInfo::PERCENTAGE, or NumberFormatInfo::SCIENTIFIC
     * @return NumberFormatInfo NumberFormatInfo for the specified
     * culture.
     * @see getCurrencyInstance();
     * @see getPercentageInstance();
     * @see getScientificInstance();
     */
    public static function getInstance($culture=null,
    								   $type=NumberFormatInfo::DECIMAL)
    {
   		if ($culture instanceof CultureInfo)
   		{
            $formatInfo = $culture->NumberFormat;
            $formatInfo->setPattern($type);
            return $formatInfo;
   		}
       	else if(is_string($culture))
       	{
       		$cultureInfo = new CultureInfo($culture);
       		$formatInfo = $cultureInfo->NumberFormat;
       		$formatInfo->setPattern($type);
       		return $formatInfo;
       	}
       	else
       	{
            $cultureInfo = new CultureInfo();
       		$formatInfo = $cultureInfo->NumberFormat;
       		$formatInfo->setPattern($type);
       		return $formatInfo;
       	}
    }

    /**
     * Returns the currency format info associated with the specified culture.
     * @param CultureInfo the culture that gets the NumberFormat property.
     * @return NumberFormatInfo NumberFormatInfo for the specified
     * culture.
     */
    public static function getCurrencyInstance($culture=null)
    {
        return self::getInstance($culture, self::CURRENCY);
    }

    /**
     * Returns the percentage format info associated with the specified culture.
     * @param CultureInfo the culture that gets the NumberFormat property.
     * @return NumberFormatInfo NumberFormatInfo for the specified
     * culture.
     */
    public static function getPercentageInstance($culture=null)
    {
        return self::getInstance($culture, self::PERCENTAGE);
    }

    /**
     * Returns the scientific format info associated with the specified culture.
     * @param CultureInfo the culture that gets the NumberFormat property.
     * @return NumberFormatInfo NumberFormatInfo for the specified
     * culture.
     */
    public static function getScientificInstance($culture=null)
    {
        return self::getInstance($culture, self::SCIENTIFIC);
    }

    /**
     * Parse the given pattern and return a list of known properties.
     * @param string a number pattern.
     * @return array list of pattern properties.
     */
    protected function parsePattern($pattern)
	{
		$pattern = explode(';',$pattern);

		$negative = null;
		if(count($pattern) > 1)
			$negative = $pattern[1];
		$pattern = $pattern[0];

		$comma = ',';
		$dot = '.';
		$digit = '0';
		$hash = '#';

		//find the first group point, and decimal point
		$groupPos1 = strrpos($pattern,$comma);
		$decimalPos = strrpos($pattern,$dot);

		$groupPos2 = false;
		$groupSize1 = false;
		$groupSize2 = false;
		$decimalPoints = is_int($decimalPos)?-1:false;

		$info['negPref'] = $this->data['NumberElements'][6];
		$info['negPost'] = '';

		$info['negative'] = $negative;
		$info['positive'] = $pattern;

		//find the negative prefix and postfix
		if($negative)
		{
			$prefixPostfix = $this->getPrePostfix($negative);
			$info['negPref'] = $prefixPostfix[0];
			$info['negPost'] = $prefixPostfix[1];
		}

		$posfix = $this->getPrePostfix($pattern);
		$info['posPref'] = $posfix[0];
		$info['posPost'] = $posfix[1];

		//var_dump($pattern);
		//var_dump($decimalPos);
		if(is_int($groupPos1))
		{
			//get the second group
			$groupPos2 = strrpos(substr($pattern,0,$groupPos1),$comma);

			//get the number of decimal digits
			if(is_int($decimalPos))
			{
				$groupSize1 = $decimalPos - $groupPos1-1;

			}
			else
			{
				//no decimal point, so traverse from the back
				//to find the groupsize 1.
				for($i=strlen($pattern)-1; $i>=0; $i--)
				{
					if($pattern{$i} == $digit || $pattern{$i}==$hash)
					{
						$groupSize1 = $i - $groupPos1;
						break;
					}
				}
			}

			//get the second group size
			if(is_int($groupPos2))
				$groupSize2 = $groupPos1 - $groupPos2-1;
		}

		if(is_int($decimalPos))
		{
			for($i=strlen($pattern)-1; $i>=0; $i--)
			{
				if($pattern{$i} == $dot) break;
				if($pattern{$i} == $digit)
				{
					$decimalPoints = $i - $decimalPos;
					break;
				}
			}
		}

		if(is_int($decimalPos))
			$digitPattern = substr($pattern,0,$decimalPos);
		else
			$digitPattern  = $pattern;

		$digitPattern  = preg_replace('/[^0]/','',$digitPattern);

		$info['groupPos1'] = $groupPos1;
		$info['groupSize1'] = $groupSize1;
		$info['groupPos2'] = $groupPos2;
		$info['groupSize2'] = $groupSize2;
		$info['decimalPos'] = $decimalPos;
		$info['decimalPoints'] = $decimalPoints;
		$info['digitSize'] = strlen($digitPattern);
		return $info;
	}

	/**
	 * Get the prefix and postfix of a pattern.
	 * @param string pattern
	 * @return array of prefix and postfix, array(prefix,postfix).
	 */
	protected function getPrePostfix($pattern)
	{
		$regexp = '/[#,\.0]+/';
		$result = preg_split($regexp, $pattern);
		return array($result[0],$result[1]);
	}


    /**
     * Indicates the number of decimal places.
     * @return int number of decimal places.
     */
    function getDecimalDigits()
    {
    	return $this->pattern['decimalPoints'];
    }

    /**
     * Set the number of decimal places.
     * @param int number of decimal places.
     */
    function setDecimalDigits($value)
    {
    	return $this->pattern['decimalPoints'] = $value;
    }

    function getDigitSize()
    {
    	return $this->pattern['digitSize'];
    }

    function setDigitSize($value)
    {
    	$this->pattern['digitSize'] = $value;
    }

    /**
     * Gets the string to use as the decimal separator.
     * @return string decimal separator.
     */
    function getDecimalSeparator()
    {
    	return $this->data['NumberElements'][0];
    }

    /**
     * Set the string to use as the decimal separator.
     * @param string the decimal point
     */
    function setDecimalSeparator($value)
    {
    	return $this->data['NumberElements'][0] = $value;
    }

    /**
     * Gets the string that separates groups of digits to the left
     * of the decimal in currency values.
     * @param parameter
     * @return string currency group separator.
     */
    function getGroupSeparator()
    {
    	return $this->data['NumberElements'][1];
    }

    /**
     * Set the string to use as the group separator.
     * @param string the group separator.
     */
    function setGroupSeparator($value)
    {
    	return $this->data['NumberElements'][1] = $value;
    }

    /**
     * Gets the number of digits in each group to the left of the decimal
     * There can be two grouping sizes, this fucntion
     * returns <b>array(group1, group2)</b>, if there is only 1 grouping size,
     * group2 will be false.
     * @return array grouping size(s).
     */
    function getGroupSizes()
    {
    	$group1 = $this->pattern['groupSize1'];
    	$group2 = $this->pattern['groupSize2'];

    	return array($group1, $group2);
    }

    /**
     * Set the number of digits in each group to the left of the decimal.
     * There can be two grouping sizes, the value should
     * be an <b>array(group1, group2)</b>, if there is only 1 grouping size,
     * group2 should be false.
     * @param array grouping size(s).
     */
    function setGroupSizes($groupSize)
    {
   		$this->pattern['groupSize1'] = $groupSize[0];
   		$this->pattern['groupSize2'] = $groupSize[1];
    }

    /**
     * Gets the format pattern for negative values.
     * The negative pattern is composed of a prefix, and postfix.
     * This function returns <b>array(prefix, postfix)</b>.
     * @return arary negative pattern.
     */
    function getNegativePattern()
    {
    	$prefix = $this->pattern['negPref'];
    	$postfix = $this->pattern['negPost'];
    	return array($prefix, $postfix);
    }

    /**
     * Set the format pattern for negative values.
     * The negative pattern is composed of a prefix, and postfix in the form
     * <b>array(prefix, postfix)</b>.
     * @param arary negative pattern.
     */
    function setNegativePattern($pattern)
    {
    	$this->pattern['negPref'] = $pattern[0];
    	$this->pattern['negPost'] = $pattern[1];
    }

    /**
     * Gets the format pattern for positive values.
     * The positive pattern is composed of a prefix, and postfix.
     * This function returns <b>array(prefix, postfix)</b>.
     * @return arary positive pattern.
     */
    function getPositivePattern()
    {
    	$prefix = $this->pattern['posPref'];
    	$postfix = $this->pattern['posPost'];
    	return array($prefix, $postfix);
    }

    /**
     * Set the format pattern for positive values.
     * The positive pattern is composed of a prefix, and postfix in the form
     * <b>array(prefix, postfix)</b>.
     * @param arary positive pattern.
     */
    function setPositivePattern($pattern)
    {
    	$this->pattern['posPref'] = $pattern[0];
    	$this->pattern['posPost'] = $pattern[1];
    }

    /**
     * Gets the string to use as the currency symbol.
     * @return string currency symbol.
     */
    function getCurrencySymbol($currency='USD')
    {
    	if(isset($this->pattern['symbol']))
			return $this->pattern['symbol'];
    	else
    		return $this->data['Currencies'][$currency][0];
    }


    /**
     * Set the string to use as the currency symbol.
     * @param string currency symbol.
     */
    function setCurrencySymbol($symbol)
    {
    	$this->pattern['symbol'] = $symbol;
    }

    /**
     * Gets the string that represents negative infinity.
     * @return string negative infinity.
     */
    function getNegativeInfinitySymbol()
    {
		return $this->pattern['negInfty'];
    }

    /**
     * Set the string that represents negative infinity.
     * @param string negative infinity.
     */
    function setNegativeInfinitySymbol($value)
    {
		$this->pattern['negInfty'] = $value;
    }

    /**
     * Gets the string that represents positive infinity.
     * @return string positive infinity.
     */
    function getPositiveInfinitySymbol()
    {
		return $this->pattern['posInfty'];
    }

    /**
     * Set the string that represents positive infinity.
     * @param string positive infinity.
     */
    function setPositiveInfinitySymbol($value)
    {
		$this->pattern['posInfty'] = $value;
    }

    /**
     * Gets the string that denotes that the associated number is negative.
     * @return string negative sign.
     */
    function getNegativeSign()
    {
    	return $this->data['NumberElements'][6];
    }

    /**
     * Set the string that denotes that the associated number is negative.
     * @param string negative sign.
     */
    function setNegativeSign($value)
    {
    	$this->data['NumberElements'][6] = $value;
    }

    /**
     * Gets the string that denotes that the associated number is positive.
     * @return string positive sign.
     */
    function getPositiveSign()
    {
    	return $this->data['NumberElements'][11];
    }

    /**
     * Set the string that denotes that the associated number is positive.
     * @param string positive sign.
     */
    function setPositiveSign($value)
    {
    	$this->data['NumberElements'][11] = $value;
    }

    /**
     * Gets the string that represents the IEEE NaN (not a number) value.
     * @return string NaN symbol.
     */
    function getNaNSymbol()
    {
    	return $this->data['NumberElements'][10];
    }

    /**
     * Set the string that represents the IEEE NaN (not a number) value.
     * @param string NaN symbol.
     */
    function setNaNSymbol($value)
    {
    	$this->data['NumberElements'][10] = $value;
    }

    /**
     * Gets the string to use as the percent symbol.
     * @return string percent symbol.
     */
    function getPercentSymbol()
    {
    	return $this->data['NumberElements'][3];
    }

    /**
     * Set the string to use as the percent symbol.
     * @param string percent symbol.
     */
    function setPercentSymbol($value)
    {
    	$this->data['NumberElements'][3] = $value;
    }

    /**
     * Gets the string to use as the per mille symbol.
     * @return string percent symbol.
     */
    function getPerMilleSymbol()
    {
    	return $this->data['NumberElements'][8];
    }

    /**
     * Set the string to use as the per mille symbol.
     * @param string percent symbol.
     */
    function setPerMilleSymbol($value)
    {
    	$this->data['NumberElements'][8] = $value;
    }
}

?>