~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Currency.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Zend Framework
 
4
 *
 
5
 * LICENSE
 
6
 *
 
7
 * This source file is subject to the new BSD license that is bundled
 
8
 * with this package in the file LICENSE.txt.
 
9
 * It is also available through the world-wide-web at this URL:
 
10
 * http://framework.zend.com/license/new-bsd
 
11
 * If you did not receive a copy of the license and are unable to
 
12
 * obtain it through the world-wide-web, please send an email
 
13
 * to license@zend.com so we can send you a copy immediately.
 
14
 *
 
15
 * @category  Zend
 
16
 * @package   Zend_Currency
 
17
 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
18
 * @license   http://framework.zend.com/license/new-bsd     New BSD License
 
19
 * @version   $Id: Currency.php 6137 2007-08-19 14:55:27Z shreef $
 
20
 */
 
21
 
 
22
/**
 
23
 * include needed classes
 
24
 */
 
25
require_once 'Zend/Locale.php';
 
26
require_once 'Zend/Locale/Data.php';
 
27
require_once 'Zend/Locale/Format.php';
 
28
 
 
29
/**
 
30
 * Class for handling currency notations
 
31
 *
 
32
 * @category  Zend
 
33
 * @package   Zend_Currency
 
34
 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
35
 * @license   http://framework.zend.com/license/new-bsd     New BSD License
 
36
 */
 
37
class Zend_Currency
 
38
{
 
39
    // Constants for defining what currency symbol should be displayed
 
40
    const NO_SYMBOL     = 1;
 
41
    const USE_SYMBOL    = 2;
 
42
    const USE_SHORTNAME = 3;
 
43
    const USE_NAME      = 4;
 
44
 
 
45
    // Constants for defining the position of the currencysign
 
46
    const STANDARD = 8;
 
47
    const RIGHT    = 16;
 
48
    const LEFT     = 32;
 
49
 
 
50
    /**
 
51
     * Locale for this currency
 
52
     *
 
53
     * @var string
 
54
     */
 
55
    private $_locale = null;
 
56
 
 
57
    /**
 
58
     * Options array
 
59
     *
 
60
     * The following options are available
 
61
     * 'position'  => Position for the currency sign
 
62
     * 'script'    => Script for the output
 
63
     * 'format'    => Locale for numeric output
 
64
     * 'display'   => Currency detail to show
 
65
     * 'precision' => Precision for the currency
 
66
     * 'name'      => Name for this currency
 
67
     * 'currency'  => 3 lettered international abbreviation
 
68
     * 'symbol'    => Currency symbol
 
69
     *
 
70
     * @var array
 
71
     * @see Zend_Locale
 
72
     */
 
73
    protected $_options = array(
 
74
        'position'  => self::STANDARD,
 
75
        'script'    => null,
 
76
        'format'    => null,
 
77
        'display'   => self::NO_SYMBOL,
 
78
        'precision' => 2,
 
79
        'name'      => null,
 
80
        'currency'  => null,
 
81
        'symbol'    => null
 
82
    );
 
83
 
 
84
    /**
 
85
     * Creates a currency instance. Every supressed parameter is used from the actual or the given locale.
 
86
     *
 
87
     * @param  string             $currency OPTIONAL currency short name
 
88
     * @param  string|Zend_Locale $locale   OPTIONAL locale name
 
89
     * @throws Zend_Currency_Exception When currency is invalid
 
90
     */
 
91
    public function __construct($currency = null, $locale = null)
 
92
    {
 
93
        if (Zend_Locale::isLocale($currency, true, false)) {
 
94
            $temp     = $locale;
 
95
            $locale   = $currency;
 
96
            $currency = $temp;
 
97
        }
 
98
 
 
99
        if (empty($locale)) {
 
100
            require_once 'Zend/Registry.php';
 
101
            if (Zend_Registry::isRegistered('Zend_Locale') === true) {
 
102
                $locale = Zend_Registry::get('Zend_Locale');
 
103
            }
 
104
        }
 
105
 
 
106
        $this->setLocale($locale);
 
107
 
 
108
        // Get currency details
 
109
        $this->_options['currency'] = self::getShortName($currency, $this->_locale);
 
110
        $this->_options['name']     = self::getName($currency, $this->_locale);
 
111
        $this->_options['symbol']   = self::getSymbol($currency, $this->_locale);
 
112
 
 
113
        if (($this->_options['currency'] === null) and ($this->_options['name'] === null)) {
 
114
            require_once 'Zend/Currency/Exception.php';
 
115
            throw new Zend_Currency_Exception("Currency '$currency' not found");
 
116
        }
 
117
 
 
118
        // Get the format
 
119
        $this->_options['position'] = $this->_updateFormat();
 
120
        $this->_options['display']  = self::NO_SYMBOL;
 
121
        if (empty($this->_options['symbol']) === false) {
 
122
            $this->_options['display'] = self::USE_SYMBOL;
 
123
        } else if (empty($this->_options['currency']) === false) {
 
124
            $this->_options['display'] = self::USE_SHORTNAME;
 
125
        }
 
126
    }
 
127
 
 
128
    /**
 
129
     * Gets the information required for formating the currency from Zend_Locale
 
130
     *
 
131
     * @return Zend_Currency
 
132
     */
 
133
    protected function _updateFormat()
 
134
    {
 
135
        $locale = (empty($this->_options['format']) === true) ? $this->_locale : $this->_options['format'];
 
136
 
 
137
        // Getting the format information of the currency
 
138
        $format = Zend_Locale_Data::getContent($locale, 'currencynumber');
 
139
 
 
140
        iconv_set_encoding('internal_encoding', 'UTF-8');
 
141
        if (iconv_strpos($format, ';') !== false) {
 
142
            $format = iconv_substr($format, 0, iconv_strpos($format, ';'));
 
143
        }
 
144
 
 
145
        // Knowing the sign positioning information
 
146
        if (iconv_strpos($format, '¤') === 0) {
 
147
            $position = self::LEFT;
 
148
        } else if (iconv_strpos($format, '¤') === (iconv_strlen($format) - 1)) {
 
149
            $position = self::RIGHT;
 
150
        }
 
151
 
 
152
        return $position;
 
153
    }
 
154
 
 
155
    /**
 
156
     * Returns a localized currency string
 
157
     *
 
158
     * @param  integer|float $value   Currency value
 
159
     * @param  array         $options OPTIONAL options to set temporary
 
160
     * @throws Zend_Currency_Exception When the value is not a number
 
161
     * @return string
 
162
     */
 
163
    public function toCurrency($value, array $options = array())
 
164
    {
 
165
        // Validate the passed number
 
166
        if ((isset($value) === false) or (is_numeric($value) === false)) {
 
167
            require_once 'Zend/Currency/Exception.php';
 
168
            throw new Zend_Currency_Exception("Value '$value' has to be numeric");
 
169
        }
 
170
 
 
171
        $options = $this->_checkOptions($options) + $this->_options;
 
172
 
 
173
        // Format the number
 
174
        if (empty($options['format']) === true) {
 
175
            $options['format'] = $this->_locale;
 
176
        }
 
177
 
 
178
        $value = Zend_Locale_Format::toNumber($value, array('locale' => $options['format'],
 
179
                                                            'precision' => $options['precision']));
 
180
 
 
181
        // Localize the number digits
 
182
        if (empty($options['script']) === false) {
 
183
            $value = Zend_Locale_Format::convertNumerals($value, 'Latn', $options['script']);
 
184
        }
 
185
 
 
186
        // Get the sign to be placed next to the number
 
187
        if (is_numeric($options['display']) === false) {
 
188
            $sign = ' ' . $options['display'] . ' ';
 
189
        } else {
 
190
            switch($options['display']) {
 
191
                case self::USE_SYMBOL:
 
192
                    $sign = ' ' . $options['symbol'] . ' ';
 
193
                    break;
 
194
 
 
195
                case self::USE_SHORTNAME:
 
196
                    $sign = ' ' . $options['currency'] . ' ';
 
197
                    break;
 
198
 
 
199
                case self::USE_NAME:
 
200
                    $sign = ' ' . $options['name'] . ' ';
 
201
                    break;
 
202
 
 
203
                default:
 
204
                    $sign = '';
 
205
                    break;
 
206
            }
 
207
        }
 
208
 
 
209
        // Place the sign next to the number
 
210
        if ($options['position'] === self::RIGHT) {
 
211
            $value = $value . $sign;
 
212
        } else if ($options['position'] === self::LEFT) {
 
213
            $value = $sign . $value;
 
214
        }
 
215
 
 
216
        return trim($value);
 
217
    }
 
218
 
 
219
    /**
 
220
     * Sets the formating options of the localized currency string
 
221
     * If no parameter is passed, the standard setting of the
 
222
     * actual set locale will be used
 
223
     *
 
224
     * @param  array $options (Optional) Options to set
 
225
     * @return Zend_Currency
 
226
     */
 
227
    public function setFormat(array $options = array())
 
228
    {
 
229
        $this->_options = $this->_checkOptions($options) + $this->_options;
 
230
        return $this;
 
231
    }
 
232
 
 
233
    /**
 
234
     * Internal function for checking static given locale parameter
 
235
     *
 
236
     * @param  string             $currency (Optional) Currency name
 
237
     * @param  string|Zend_Locale $locale   (Optional) Locale to display informations
 
238
     * @throws Zend_Currency_Exception When locale contains no region
 
239
     * @return string The extracted locale representation as string
 
240
     */
 
241
    private function _checkParams($currency = null, $locale = null)
 
242
    {
 
243
        // Manage the params
 
244
        if ((empty($locale)) and (!empty($currency)) and
 
245
            (Zend_Locale::isLocale($currency, true, false))) {
 
246
            $locale   = $currency;
 
247
            $currency = null;
 
248
        }
 
249
 
 
250
        // Validate the locale and get the country short name
 
251
        $country = null;
 
252
        if ((Zend_Locale::isLocale($locale, true, false)) and (strlen($locale) > 4)) {
 
253
            $country = substr($locale, (strpos($locale, '_') + 1));
 
254
        } else {
 
255
            require_once 'Zend/Currency/Exception.php';
 
256
            throw new Zend_Currency_Exception("No region found within the locale '" . (string) $locale . "'");
 
257
        }
 
258
 
 
259
        // Get the available currencies for this country
 
260
        $data = Zend_Locale_Data::getContent($locale, 'currencytoregion', $country);
 
261
        if ((empty($currency) === false) and (empty($data) === false)) {
 
262
            $abbreviation = $currency;
 
263
        } else {
 
264
            $abbreviation = $data;
 
265
        }
 
266
 
 
267
        return array('locale' => $locale, 'currency' => $currency, 'name' => $abbreviation, 'country' => $country);
 
268
    }
 
269
 
 
270
    /**
 
271
     * Returns the actual or details of other currency symbols,
 
272
     * when no symbol is available it returns the currency shortname (f.e. FIM for Finnian Mark)
 
273
     *
 
274
     * @param  string             $currency (Optional) Currency name
 
275
     * @param  string|Zend_Locale $locale   (Optional) Locale to display informations
 
276
     * @return string
 
277
     */
 
278
    public function getSymbol($currency = null, $locale = null)
 
279
    {
 
280
        if (($currency === null) and ($locale === null)) {
 
281
            return $this->_options['symbol'];
 
282
        }
 
283
 
 
284
        $params = self::_checkParams($currency, $locale);
 
285
 
 
286
        // Get the symbol
 
287
        $symbol = Zend_Locale_Data::getContent($params['locale'], 'currencysymbol', $params['currency']);
 
288
        if (empty($symbol) === true) {
 
289
            $symbol = Zend_Locale_Data::getContent($params['locale'], 'currencysymbol', $params['name']);
 
290
        }
 
291
 
 
292
        if (empty($symbol) === true) {
 
293
            return null;
 
294
        }
 
295
 
 
296
        return $symbol;
 
297
    }
 
298
 
 
299
    /**
 
300
     * Returns the actual or details of other currency shortnames
 
301
     *
 
302
     * @param  string             $currency OPTIONAL Currency's name
 
303
     * @param  string|Zend_Locale $locale   OPTIONAL The locale
 
304
     * @return string
 
305
     */
 
306
    public function getShortName($currency = null, $locale = null)
 
307
    {
 
308
        if (($currency === null) and ($locale === null)) {
 
309
            return $this->_options['currency'];
 
310
        }
 
311
 
 
312
        $params = self::_checkParams($currency, $locale);
 
313
 
 
314
        // Get the shortname
 
315
        if (empty($params['currency']) === true) {
 
316
            return $params['name'];
 
317
        }
 
318
 
 
319
        $list = Zend_Locale_Data::getContent($params['locale'], 'currencytoname', $params['currency']);
 
320
        if (empty($list) === true) {
 
321
            $list = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['currency']);
 
322
            if (empty($list) === false) {
 
323
                $list = $params['currency'];
 
324
            }
 
325
        }
 
326
 
 
327
        if (empty($list) === true) {
 
328
            return null;
 
329
        }
 
330
 
 
331
        return $list;
 
332
    }
 
333
 
 
334
    /**
 
335
     * Returns the actual or details of other currency names
 
336
     *
 
337
     * @param  string             $currency (Optional) Currency's short name
 
338
     * @param  string|Zend_Locale $locale   (Optional) The locale
 
339
     * @return string
 
340
     */
 
341
    public function getName($currency = null, $locale = null)
 
342
    {
 
343
        if (($currency === null) and ($locale === null)) {
 
344
            return $this->_options['name'];
 
345
        }
 
346
 
 
347
        $params = self::_checkParams($currency, $locale);
 
348
 
 
349
        // Get the name
 
350
        $name = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['currency']);
 
351
        if (empty($name) === true) {
 
352
            $name = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['name']);
 
353
        }
 
354
 
 
355
        if (empty($name) === true) {
 
356
            return null;
 
357
        }
 
358
 
 
359
        return $name;
 
360
    }
 
361
 
 
362
    /**
 
363
     * Returns a list of regions where this currency is or was known
 
364
     *
 
365
     * @param  string $currency OPTIONAL Currency's short name
 
366
     * @throws Zend_Currency_Exception When no currency was defined
 
367
     * @return array List of regions
 
368
     */
 
369
    public function getRegionList($currency = null)
 
370
    {
 
371
        if ($currency === null) {
 
372
            $currency = $this->_options['currency'];
 
373
        }
 
374
 
 
375
        if (empty($currency) === true) {
 
376
            require_once 'Zend/Currency/Exception.php';
 
377
            throw new Zend_Currency_Exception('No currency defined');
 
378
        }
 
379
 
 
380
        $data = Zend_Locale_Data::getContent('', 'regiontocurrency', $currency);
 
381
 
 
382
        $result = explode(' ', $data);
 
383
        return $result;
 
384
    }
 
385
 
 
386
    /**
 
387
     * Returns a list of currencies which are used in this region
 
388
     * a region name should be 2 charachters only (f.e. EG, DE, US)
 
389
     * If no region is given, the actual region is used
 
390
     *
 
391
     * @param  string $region OPTIONAL Region to return the currencies for
 
392
     * @return array List of currencies
 
393
     */
 
394
    public function getCurrencyList($region = null)
 
395
    {
 
396
        if (empty($region) === true) {
 
397
            if (strlen($this->_locale) > 4) {
 
398
                $region = substr($this->_locale, (strpos($this->_locale, '_') + 1));
 
399
            }
 
400
        }
 
401
 
 
402
        return Zend_Locale_Data::getList('', 'regiontocurrency', $region);
 
403
    }
 
404
 
 
405
    /**
 
406
     * Returns the actual currency name
 
407
     *
 
408
     * @return string
 
409
     */
 
410
    public function toString()
 
411
    {
 
412
        return (empty($this->_options['name']) === false) ? $this->_options['name'] : $this->_options['currency'];
 
413
    }
 
414
 
 
415
    /**
 
416
     * Returns the currency name
 
417
     *
 
418
     * @return string
 
419
     */
 
420
    public function __toString()
 
421
    {
 
422
        return $this->toString();
 
423
    }
 
424
 
 
425
    /**
 
426
     * Returns the set cache
 
427
     *
 
428
     * @return Zend_Cache_Core The set cache
 
429
     */
 
430
    public static function getCache()
 
431
    {
 
432
        $cache = Zend_Locale_Data::getCache();
 
433
        return $cache;
 
434
    }
 
435
 
 
436
    /**
 
437
     * Sets a cache for Zend_Currency
 
438
     *
 
439
     * @param  Zend_Cache_Core $cache Cache to set
 
440
     * @return void
 
441
     */
 
442
    public static function setCache(Zend_Cache_Core $cache)
 
443
    {
 
444
        Zend_Locale_Data::setCache($cache);
 
445
    }
 
446
 
 
447
    /**
 
448
     * Returns true when a cache is set
 
449
     *
 
450
     * @return boolean
 
451
     */
 
452
    public static function hasCache()
 
453
    {
 
454
        return Zend_Locale_Data::hasCache();
 
455
    }
 
456
 
 
457
    /**
 
458
     * Removes any set cache
 
459
     *
 
460
     * @return void
 
461
     */
 
462
    public static function removeCache()
 
463
    {
 
464
        Zend_Locale_Data::removeCache();
 
465
    }
 
466
 
 
467
    /**
 
468
     * Clears all set cache data
 
469
     *
 
470
     * @return void
 
471
     */
 
472
    public static function clearCache()
 
473
    {
 
474
        Zend_Locale_Data::clearCache();
 
475
    }
 
476
 
 
477
    /**
 
478
     * Sets a new locale for data retreivement
 
479
     * Example: 'de_XX' will be set to 'de' because 'de_XX' does not exist
 
480
     * 'xx_YY' will be set to 'root' because 'xx' does not exist
 
481
     *
 
482
     * @param  string|Zend_Locale $locale (Optional) Locale for parsing input
 
483
     * @throws Zend_Currency_Exception When the given locale does not exist
 
484
     * @return Zend_Currency Provides fluent interface
 
485
     */
 
486
    public function setLocale($locale = null)
 
487
    {
 
488
        if (!Zend_Locale::isLocale($locale, false, false)) {
 
489
            if (!Zend_Locale::isLocale($locale, true, false)) {
 
490
                require_once 'Zend/Currency/Exception.php';
 
491
                throw new Zend_Currency_Exception("Given locale (" . (string) $locale . ") does not exist");
 
492
            } else {
 
493
                $locale = new Zend_Locale();
 
494
            }
 
495
        }
 
496
 
 
497
        $this->_locale = (string) $locale;
 
498
 
 
499
        // Get currency details
 
500
        $this->_options['currency'] = $this->getShortName(null, $this->_locale);
 
501
        $this->_options['name']     = $this->getName(null, $this->_locale);
 
502
        $this->_options['symbol']   = $this->getSymbol(null, $this->_locale);
 
503
 
 
504
        return $this;
 
505
    }
 
506
 
 
507
    /**
 
508
     * Returns the actual set locale
 
509
     *
 
510
     * @return string
 
511
     */
 
512
    public function getLocale()
 
513
    {
 
514
        return $this->_locale;
 
515
    }
 
516
 
 
517
    /**
 
518
     * Internal method for checking the options array
 
519
     *
 
520
     * @param  array $options Options to check
 
521
     * @throws Zend_Currency_Exception On unknown position
 
522
     * @throws Zend_Currency_Exception On unknown locale
 
523
     * @throws Zend_Currency_Exception On unknown display
 
524
     * @throws Zend_Currency_Exception On precision not between -1 and 30
 
525
     * @throws Zend_Currency_Exception On problem with script conversion
 
526
     * @throws Zend_Currency_Exception On unknown options
 
527
     * @return array
 
528
     */
 
529
    private function _checkOptions(array $options = array())
 
530
    {
 
531
        if (count($options) === 0) {
 
532
            return $this->_options;
 
533
        }
 
534
 
 
535
        foreach ($options as $name => $value) {
 
536
            $name = strtolower($name);
 
537
            if ($name !== 'format') {
 
538
                if (gettype($value) === 'string') {
 
539
                    $value = strtolower($value);
 
540
                }
 
541
            }
 
542
 
 
543
            switch($name) {
 
544
                case 'position':
 
545
                    if (($value !== self::STANDARD) and ($value !== self::RIGHT) and ($value !== self::LEFT)) {
 
546
                        require_once 'Zend/Currency/Exception.php';
 
547
                        throw new Zend_Currency_Exception("Unknown position '" . $value . "'");
 
548
                    }
 
549
 
 
550
                    if ($value === self::STANDARD) {
 
551
                        $options['position'] = $this->_updateFormat();
 
552
                    }
 
553
                    break;
 
554
 
 
555
                case 'format':
 
556
                    if ((empty($value) === false) and (Zend_Locale::isLocale($value, null, false) === false)) {
 
557
                        require_once 'Zend/Currency/Exception.php';
 
558
                        throw new Zend_Currency_Exception("'" .
 
559
                            ((gettype($value) === 'object') ? get_class($value) : $value)
 
560
                            . "' is not a known locale.");
 
561
                    }
 
562
                    break;
 
563
 
 
564
                case 'display':
 
565
                    if (is_numeric($value) and ($value !== self::NO_SYMBOL) and ($value !== self::USE_SYMBOL) and
 
566
                        ($value !== self::USE_SHORTNAME) and ($value !== self::USE_NAME)) {
 
567
                        require_once 'Zend/Currency/Exception.php';
 
568
                        throw new Zend_Currency_Exception("Unknown display '$value'");
 
569
                    }
 
570
                    break;
 
571
 
 
572
                case 'precision':
 
573
                    if ($value === null) {
 
574
                        $value = -1;
 
575
                    }
 
576
 
 
577
                    if (($value < -1) or ($value > 30)) {
 
578
                        require_once 'Zend/Currency/Exception.php';
 
579
                        throw new Zend_Currency_Exception("'$value' precision has to be between -1 and 30.");
 
580
                    }
 
581
                    break;
 
582
 
 
583
                case 'script':
 
584
                    try {
 
585
                        Zend_Locale_Format::convertNumerals(0, $options['script']);
 
586
                    } catch (Zend_Locale_Exception $e) {
 
587
                        require_once 'Zend/Currency/Exception.php';
 
588
                        throw new Zend_Currency_Exception($e->getMessage());
 
589
                    }
 
590
                    break;
 
591
 
 
592
                case 'name':
 
593
                    // Break intentionally omitted
 
594
                case 'currency':
 
595
                    // Break intentionally omitted
 
596
                case 'symbol':
 
597
                    // Unchecked options
 
598
                    break;
 
599
 
 
600
                default:
 
601
                    require_once 'Zend/Currency/Exception.php';
 
602
                    throw new Zend_Currency_Exception("Unknown option: '$name' = '$value'");
 
603
                    break;
 
604
            }
 
605
        }
 
606
 
 
607
        return $options;
 
608
    }
 
609
}