PageRenderTime 169ms CodeModel.GetById 80ms app.highlight 55ms RepoModel.GetById 26ms app.codeStats 0ms

/src/application/libraries/Zend/Currency.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 894 lines | 492 code | 105 blank | 297 comment | 86 complexity | 464ed8ada52f10ca8c2bd2c28fcdebee MD5 | raw file
  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-2011 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 23775 2011-03-01 17:25:24Z ralph $
 20 */
 21
 22/**
 23 * include needed classes
 24 */
 25require_once 'Zend/Locale.php';
 26require_once 'Zend/Locale/Data.php';
 27require_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-2011 Zend Technologies USA Inc. (http://www.zend.com)
 35 * @license   http://framework.zend.com/license/new-bsd     New BSD License
 36 */
 37class 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     * Options array
 52     *
 53     * The following options are available
 54     * 'position'  => Position for the currency sign
 55     * 'script'    => Script for the output
 56     * 'format'    => Locale for numeric output
 57     * 'display'   => Currency detail to show
 58     * 'precision' => Precision for the currency
 59     * 'name'      => Name for this currency
 60     * 'currency'  => 3 lettered international abbreviation
 61     * 'symbol'    => Currency symbol
 62     * 'locale'    => Locale for this currency
 63     * 'value'     => Money value
 64     * 'service'   => Exchange service to use
 65     *
 66     * @var array
 67     * @see Zend_Locale
 68     */
 69    protected $_options = array(
 70        'position'  => self::STANDARD,
 71        'script'    => null,
 72        'format'    => null,
 73        'display'   => self::NO_SYMBOL,
 74        'precision' => 2,
 75        'name'      => null,
 76        'currency'  => null,
 77        'symbol'    => null,
 78        'locale'    => null,
 79        'value'     => 0,
 80        'service'   => null,
 81        'tag'       => 'Zend_Locale'
 82    );
 83
 84    /**
 85     * Creates a currency instance. Every supressed parameter is used from the actual or the given locale.
 86     *
 87     * @param  string|array       $options OPTIONAL Options array or currency short name
 88     *                                              when string is given
 89     * @param  string|Zend_Locale $locale  OPTIONAL locale name
 90     * @throws Zend_Currency_Exception When currency is invalid
 91     */
 92    public function __construct($options = null, $locale = null)
 93    {
 94        if (is_array($options)) {
 95            $this->setLocale($locale);
 96            $this->setFormat($options);
 97        } else if (Zend_Locale::isLocale($options, false, false)) {
 98            $this->setLocale($options);
 99            $options = $locale;
100        } else {
101            $this->setLocale($locale);
102        }
103
104        // Get currency details
105        if (!isset($this->_options['currency']) || !is_array($options)) {
106            $this->_options['currency'] = self::getShortName($options, $this->_options['locale']);
107        }
108
109        if (!isset($this->_options['name']) || !is_array($options)) {
110            $this->_options['name']     = self::getName($options, $this->_options['locale']);
111        }
112
113        if (!isset($this->_options['symbol']) || !is_array($options)) {
114            $this->_options['symbol']   = self::getSymbol($options, $this->_options['locale']);
115        }
116
117        if (($this->_options['currency'] === null) and ($this->_options['name'] === null)) {
118            require_once 'Zend/Currency/Exception.php';
119            throw new Zend_Currency_Exception("Currency '$options' not found");
120        }
121
122        // Get the format
123        if (!empty($this->_options['symbol'])) {
124            $this->_options['display'] = self::USE_SYMBOL;
125        } else if (!empty($this->_options['currency'])) {
126            $this->_options['display'] = self::USE_SHORTNAME;
127        }
128    }
129
130    /**
131     * Returns a localized currency string
132     *
133     * @param  integer|float $value   OPTIONAL Currency value
134     * @param  array         $options OPTIONAL options to set temporary
135     * @throws Zend_Currency_Exception When the value is not a number
136     * @return string
137     */
138    public function toCurrency($value = null, array $options = array())
139    {
140        if ($value === null) {
141            if (is_array($options) && isset($options['value'])) {
142                $value = $options['value'];
143            } else {
144                $value = $this->_options['value'];
145            }
146        }
147
148        if (is_array($value)) {
149            $options += $value;
150            if (isset($options['value'])) {
151                $value = $options['value'];
152            }
153        }
154
155        // Validate the passed number
156        if (!(isset($value)) or (is_numeric($value) === false)) {
157            require_once 'Zend/Currency/Exception.php';
158            throw new Zend_Currency_Exception("Value '$value' has to be numeric");
159        }
160
161        if (isset($options['currency'])) {
162            if (!isset($options['locale'])) {
163                $options['locale'] = $this->_options['locale'];
164            }
165
166            $options['currency'] = self::getShortName($options['currency'], $options['locale']);
167            $options['name']     = self::getName($options['currency'], $options['locale']);
168            $options['symbol']   = self::getSymbol($options['currency'], $options['locale']);
169        }
170
171        $options = $this->_checkOptions($options) + $this->_options;
172
173        // Format the number
174        $format = $options['format'];
175        $locale = $options['locale'];
176        if (empty($format)) {
177            $format = Zend_Locale_Data::getContent($locale, 'currencynumber');
178        } else if (Zend_Locale::isLocale($format, true, false)) {
179            $locale = $format;
180            $format = Zend_Locale_Data::getContent($format, 'currencynumber');
181        }
182
183        $original = $value;
184        $value    = Zend_Locale_Format::toNumber($value, array('locale'        => $locale,
185                                                               'number_format' => $format,
186                                                               'precision'     => $options['precision']));
187
188        if ($options['position'] !== self::STANDARD) {
189            $value = str_replace('¤', '', $value);
190            $space = '';
191            if (iconv_strpos($value, ' ') !== false) {
192                $value = str_replace(' ', '', $value);
193                $space = ' ';
194            }
195
196            if ($options['position'] == self::LEFT) {
197                $value = '¤' . $space . $value;
198            } else {
199                $value = $value . $space . '¤';
200            }
201        }
202
203        // Localize the number digits
204        if (empty($options['script']) === false) {
205            $value = Zend_Locale_Format::convertNumerals($value, 'Latn', $options['script']);
206        }
207
208        // Get the sign to be placed next to the number
209        if (is_numeric($options['display']) === false) {
210            $sign = $options['display'];
211        } else {
212            switch($options['display']) {
213                case self::USE_SYMBOL:
214                    $sign = $this->_extractPattern($options['symbol'], $original);
215                    break;
216
217                case self::USE_SHORTNAME:
218                    $sign = $options['currency'];
219                    break;
220
221                case self::USE_NAME:
222                    $sign = $options['name'];
223                    break;
224
225                default:
226                    $sign = '';
227                    $value = str_replace(' ', '', $value);
228                    break;
229            }
230        }
231
232        $value = str_replace('¤', $sign, $value);
233        return $value;
234    }
235
236    /**
237     * Internal method to extract the currency pattern
238     * when a choice is given based on the given value
239     *
240     * @param  string $pattern
241     * @param  float|integer $value
242     * @return string
243     */
244    private function _extractPattern($pattern, $value)
245    {
246        if (strpos($pattern, '|') === false) {
247            return $pattern;
248        }
249
250        $patterns = explode('|', $pattern);
251        $token    = $pattern;
252        $value    = trim(str_replace('¤', '', $value));
253        krsort($patterns);
254        foreach($patterns as $content) {
255            if (strpos($content, '<') !== false) {
256                $check = iconv_substr($content, 0, iconv_strpos($content, '<'));
257                $token = iconv_substr($content, iconv_strpos($content, '<') + 1);
258                if ($check < $value) {
259                    return $token;
260                }
261            } else {
262                $check = iconv_substr($content, 0, iconv_strpos($content, '≤'));
263                $token = iconv_substr($content, iconv_strpos($content, '≤') + 1);
264                if ($check <= $value) {
265                    return $token;
266                }
267            }
268
269        }
270
271        return $token;
272    }
273
274    /**
275     * Sets the formating options of the localized currency string
276     * If no parameter is passed, the standard setting of the
277     * actual set locale will be used
278     *
279     * @param  array $options (Optional) Options to set
280     * @return Zend_Currency
281     */
282    public function setFormat(array $options = array())
283    {
284        $this->_options = $this->_checkOptions($options) + $this->_options;
285        return $this;
286    }
287
288    /**
289     * Internal function for checking static given locale parameter
290     *
291     * @param  string             $currency (Optional) Currency name
292     * @param  string|Zend_Locale $locale   (Optional) Locale to display informations
293     * @throws Zend_Currency_Exception When locale contains no region
294     * @return string The extracted locale representation as string
295     */
296    private function _checkParams($currency = null, $locale = null)
297    {
298        // Manage the params
299        if ((empty($locale)) and (!empty($currency)) and
300            (Zend_Locale::isLocale($currency, true, false))) {
301            $locale   = $currency;
302            $currency = null;
303        }
304
305        // Validate the locale and get the country short name
306        $country = null;
307        if ((Zend_Locale::isLocale($locale, true, false)) and (strlen($locale) > 4)) {
308            $country = substr($locale, (strpos($locale, '_') + 1));
309        } else {
310            require_once 'Zend/Currency/Exception.php';
311            throw new Zend_Currency_Exception("No region found within the locale '" . (string) $locale . "'");
312        }
313
314        // Get the available currencies for this country
315        $data = Zend_Locale_Data::getContent($locale, 'currencytoregion', $country);
316        if ((empty($currency) === false) and (empty($data) === false)) {
317            $abbreviation = $currency;
318        } else {
319            $abbreviation = $data;
320        }
321
322        return array('locale' => $locale, 'currency' => $currency, 'name' => $abbreviation, 'country' => $country);
323    }
324
325    /**
326     * Returns the actual or details of other currency symbols,
327     * when no symbol is available it returns the currency shortname (f.e. FIM for Finnian Mark)
328     *
329     * @param  string             $currency (Optional) Currency name
330     * @param  string|Zend_Locale $locale   (Optional) Locale to display informations
331     * @return string
332     */
333    public function getSymbol($currency = null, $locale = null)
334    {
335        if (($currency === null) and ($locale === null)) {
336            return $this->_options['symbol'];
337        }
338
339        $params = self::_checkParams($currency, $locale);
340
341        // Get the symbol
342        $symbol = Zend_Locale_Data::getContent($params['locale'], 'currencysymbol', $params['currency']);
343        if (empty($symbol) === true) {
344            $symbol = Zend_Locale_Data::getContent($params['locale'], 'currencysymbol', $params['name']);
345        }
346
347        if (empty($symbol) === true) {
348            return null;
349        }
350
351        return $symbol;
352    }
353
354    /**
355     * Returns the actual or details of other currency shortnames
356     *
357     * @param  string             $currency OPTIONAL Currency's name
358     * @param  string|Zend_Locale $locale   OPTIONAL The locale
359     * @return string
360     */
361    public function getShortName($currency = null, $locale = null)
362    {
363        if (($currency === null) and ($locale === null)) {
364            return $this->_options['currency'];
365        }
366
367        $params = self::_checkParams($currency, $locale);
368
369        // Get the shortname
370        if (empty($params['currency']) === true) {
371            return $params['name'];
372        }
373
374        $list = Zend_Locale_Data::getContent($params['locale'], 'currencytoname', $params['currency']);
375        if (empty($list) === true) {
376            $list = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['currency']);
377            if (empty($list) === false) {
378                $list = $params['currency'];
379            }
380        }
381
382        if (empty($list) === true) {
383            return null;
384        }
385
386        return $list;
387    }
388
389    /**
390     * Returns the actual or details of other currency names
391     *
392     * @param  string             $currency (Optional) Currency's short name
393     * @param  string|Zend_Locale $locale   (Optional) The locale
394     * @return string
395     */
396    public function getName($currency = null, $locale = null)
397    {
398        if (($currency === null) and ($locale === null)) {
399            return $this->_options['name'];
400        }
401
402        $params = self::_checkParams($currency, $locale);
403
404        // Get the name
405        $name = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['currency']);
406        if (empty($name) === true) {
407            $name = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['name']);
408        }
409
410        if (empty($name) === true) {
411            return null;
412        }
413
414        return $name;
415    }
416
417    /**
418     * Returns a list of regions where this currency is or was known
419     *
420     * @param  string $currency OPTIONAL Currency's short name
421     * @throws Zend_Currency_Exception When no currency was defined
422     * @return array List of regions
423     */
424    public function getRegionList($currency = null)
425    {
426        if ($currency === null) {
427            $currency = $this->_options['currency'];
428        }
429
430        if (empty($currency) === true) {
431            require_once 'Zend/Currency/Exception.php';
432            throw new Zend_Currency_Exception('No currency defined');
433        }
434
435        $data = Zend_Locale_Data::getContent($this->_options['locale'], 'regiontocurrency', $currency);
436
437        $result = explode(' ', $data);
438        return $result;
439    }
440
441    /**
442     * Returns a list of currencies which are used in this region
443     * a region name should be 2 charachters only (f.e. EG, DE, US)
444     * If no region is given, the actual region is used
445     *
446     * @param  string $region OPTIONAL Region to return the currencies for
447     * @return array List of currencies
448     */
449    public function getCurrencyList($region = null)
450    {
451        if (empty($region) === true) {
452            if (strlen($this->_options['locale']) > 4) {
453                $region = substr($this->_options['locale'], (strpos($this->_options['locale'], '_') + 1));
454            }
455        }
456
457        $data = Zend_Locale_Data::getContent($this->_options['locale'], 'currencytoregion', $region);
458
459        $result = explode(' ', $data);
460        return $result;
461    }
462
463    /**
464     * Returns the actual currency name
465     *
466     * @return string
467     */
468    public function toString()
469    {
470        return $this->toCurrency();
471    }
472
473    /**
474     * Returns the currency name
475     *
476     * @return string
477     */
478    public function __toString()
479    {
480        return $this->toString();
481    }
482
483    /**
484     * Returns the set cache
485     *
486     * @return Zend_Cache_Core The set cache
487     */
488    public static function getCache()
489    {
490        return Zend_Locale_Data::getCache();
491    }
492
493    /**
494     * Sets a cache for Zend_Currency
495     *
496     * @param  Zend_Cache_Core $cache Cache to set
497     * @return void
498     */
499    public static function setCache(Zend_Cache_Core $cache)
500    {
501        Zend_Locale_Data::setCache($cache);
502    }
503
504    /**
505     * Returns true when a cache is set
506     *
507     * @return boolean
508     */
509    public static function hasCache()
510    {
511        return Zend_Locale_Data::hasCache();
512    }
513
514    /**
515     * Removes any set cache
516     *
517     * @return void
518     */
519    public static function removeCache()
520    {
521        Zend_Locale_Data::removeCache();
522    }
523
524    /**
525     * Clears all set cache data
526     *
527     * @param string $tag Tag to clear when the default tag name is not used
528     * @return void
529     */
530    public static function clearCache($tag = null)
531    {
532        Zend_Locale_Data::clearCache($tag);
533    }
534
535    /**
536     * Sets a new locale for data retreivement
537     * Example: 'de_XX' will be set to 'de' because 'de_XX' does not exist
538     * 'xx_YY' will be set to 'root' because 'xx' does not exist
539     *
540     * @param  string|Zend_Locale $locale (Optional) Locale for parsing input
541     * @throws Zend_Currency_Exception When the given locale does not exist
542     * @return Zend_Currency Provides fluent interface
543     */
544    public function setLocale($locale = null)
545    {
546        require_once 'Zend/Locale.php';
547        try {
548            $locale = Zend_Locale::findLocale($locale);
549            if (strlen($locale) > 4) {
550                $this->_options['locale'] = $locale;
551            } else {
552                require_once 'Zend/Currency/Exception.php';
553                throw new Zend_Currency_Exception("No region found within the locale '" . (string) $locale . "'");
554            }
555        } catch (Zend_Locale_Exception $e) {
556            require_once 'Zend/Currency/Exception.php';
557            throw new Zend_Currency_Exception($e->getMessage());
558        }
559
560        // Get currency details
561        $this->_options['currency'] = $this->getShortName(null, $this->_options['locale']);
562        $this->_options['name']     = $this->getName(null, $this->_options['locale']);
563        $this->_options['symbol']   = $this->getSymbol(null, $this->_options['locale']);
564
565        return $this;
566    }
567
568    /**
569     * Returns the actual set locale
570     *
571     * @return string
572     */
573    public function getLocale()
574    {
575        return $this->_options['locale'];
576    }
577
578    /**
579     * Returns the value
580     *
581     * @return float
582     */
583    public function getValue()
584    {
585        return $this->_options['value'];
586    }
587
588    /**
589     * Adds a currency
590     *
591     * @param float|integer|Zend_Currency $value    Add this value to currency
592     * @param string|Zend_Currency        $currency The currency to add
593     * @return Zend_Currency
594     */
595    public function setValue($value, $currency = null)
596    {
597        $this->_options['value'] = $this->_exchangeCurrency($value, $currency);
598        return $this;
599    }
600
601    /**
602     * Adds a currency
603     *
604     * @param float|integer|Zend_Currency $value    Add this value to currency
605     * @param string|Zend_Currency        $currency The currency to add
606     * @return Zend_Currency
607     */
608    public function add($value, $currency = null)
609    {
610        $value = $this->_exchangeCurrency($value, $currency);
611        $this->_options['value'] += (float) $value;
612        return $this;
613    }
614
615    /**
616     * Substracts a currency
617     *
618     * @param float|integer|Zend_Currency $value    Substracts this value from currency
619     * @param string|Zend_Currency        $currency The currency to substract
620     * @return Zend_Currency
621     */
622    public function sub($value, $currency = null)
623    {
624        $value = $this->_exchangeCurrency($value, $currency);
625        $this->_options['value'] -= (float) $value;
626        return $this;
627    }
628
629    /**
630     * Divides a currency
631     *
632     * @param float|integer|Zend_Currency $value    Divides this value from currency
633     * @param string|Zend_Currency        $currency The currency to divide
634     * @return Zend_Currency
635     */
636    public function div($value, $currency = null)
637    {
638        $value = $this->_exchangeCurrency($value, $currency);
639        $this->_options['value'] /= (float) $value;
640        return $this;
641    }
642
643    /**
644     * Multiplies a currency
645     *
646     * @param float|integer|Zend_Currency $value    Multiplies this value from currency
647     * @param string|Zend_Currency        $currency The currency to multiply
648     * @return Zend_Currency
649     */
650    public function mul($value, $currency = null)
651    {
652        $value = $this->_exchangeCurrency($value, $currency);
653        $this->_options['value'] *= (float) $value;
654        return $this;
655    }
656
657    /**
658     * Calculates the modulo from a currency
659     *
660     * @param float|integer|Zend_Currency $value    Calculate modulo from this value
661     * @param string|Zend_Currency        $currency The currency to calculate the modulo
662     * @return Zend_Currency
663     */
664    public function mod($value, $currency = null)
665    {
666        $value = $this->_exchangeCurrency($value, $currency);
667        $this->_options['value'] %= (float) $value;
668        return $this;
669    }
670
671    /**
672     * Compares two currencies
673     *
674     * @param float|integer|Zend_Currency $value    Compares the currency with this value
675     * @param string|Zend_Currency        $currency The currency to compare this value from
676     * @return Zend_Currency
677     */
678    public function compare($value, $currency = null)
679    {
680        $value = $this->_exchangeCurrency($value, $currency);
681        $value = $this->_options['value'] - $value;
682        if ($value < 0) {
683            return -1;
684        } else if ($value > 0) {
685            return 1;
686        }
687
688        return 0;
689    }
690
691    /**
692     * Returns true when the two currencies are equal
693     *
694     * @param float|integer|Zend_Currency $value    Compares the currency with this value
695     * @param string|Zend_Currency        $currency The currency to compare this value from
696     * @return boolean
697     */
698    public function equals($value, $currency = null)
699    {
700        $value = $this->_exchangeCurrency($value, $currency);
701        if ($this->_options['value'] == $value) {
702            return true;
703        }
704
705        return false;
706    }
707
708    /**
709     * Returns true when the currency is more than the given value
710     *
711     * @param float|integer|Zend_Currency $value    Compares the currency with this value
712     * @param string|Zend_Currency        $currency The currency to compare this value from
713     * @return boolean
714     */
715    public function isMore($value, $currency = null)
716    {
717        $value = $this->_exchangeCurrency($value, $currency);
718        if ($this->_options['value'] > $value) {
719            return true;
720        }
721
722        return false;
723    }
724
725    /**
726     * Returns true when the currency is less than the given value
727     *
728     * @param float|integer|Zend_Currency $value    Compares the currency with this value
729     * @param string|Zend_Currency        $currency The currency to compare this value from
730     * @return boolean
731     */
732    public function isLess($value, $currency = null)
733    {
734        $value = $this->_exchangeCurrency($value, $currency);
735        if ($this->_options['value'] < $value) {
736            return true;
737        }
738
739        return false;
740
741    }
742
743    /**
744     * Internal method which calculates the exchanges currency
745     *
746     * @param float|integer|Zend_Currency $value    Compares the currency with this value
747     * @param string|Zend_Currency        $currency The currency to compare this value from
748     * @return unknown
749     */
750    protected function _exchangeCurrency($value, $currency)
751    {
752        if ($value instanceof Zend_Currency) {
753            $currency = $value->getShortName();
754            $value    = $value->getValue();
755        } else {
756            $currency = $this->getShortName($currency, $this->getLocale());
757        }
758
759        $rate = 1;
760        if ($currency !== $this->getShortName()) {
761            $service = $this->getService();
762            if (!($service instanceof Zend_Currency_CurrencyInterface)) {
763                require_once 'Zend/Currency/Exception.php';
764                throw new Zend_Currency_Exception('No exchange service applied');
765            }
766
767            $rate = $service->getRate($currency, $this->getShortName());
768        }
769
770        $value *= $rate;
771        return $value;
772    }
773
774    /**
775     * Returns the set service class
776     *
777     * @return Zend_Service
778     */
779    public function getService()
780    {
781        return $this->_options['service'];
782    }
783
784    /**
785     * Sets a new exchange service
786     *
787     * @param string|Zend_Currency_CurrencyInterface $service Service class
788     * @return Zend_Currency
789     */
790    public function setService($service)
791    {
792        if (is_string($service)) {
793            require_once 'Zend/Loader.php';
794            if (!class_exists($service)) {
795                $file = str_replace('_', DIRECTORY_SEPARATOR, $service) . '.php';
796                if (Zend_Loader::isReadable($file)) {
797                    Zend_Loader::loadClass($service);
798                }
799            }
800
801            $service = new $service;
802        }
803
804        if (!($service instanceof Zend_Currency_CurrencyInterface)) {
805            require_once 'Zend/Currency/Exception.php';
806            throw new Zend_Currency_Exception('A currency service must implement Zend_Currency_CurrencyInterface');
807        }
808
809        $this->_options['service'] = $service;
810        return $this;
811    }
812
813    /**
814     * Internal method for checking the options array
815     *
816     * @param  array $options Options to check
817     * @throws Zend_Currency_Exception On unknown position
818     * @throws Zend_Currency_Exception On unknown locale
819     * @throws Zend_Currency_Exception On unknown display
820     * @throws Zend_Currency_Exception On precision not between -1 and 30
821     * @throws Zend_Currency_Exception On problem with script conversion
822     * @throws Zend_Currency_Exception On unknown options
823     * @return array
824     */
825    protected function _checkOptions(array $options = array())
826    {
827        if (count($options) === 0) {
828            return $this->_options;
829        }
830
831        foreach ($options as $name => $value) {
832            $name = strtolower($name);
833            if ($name !== 'format') {
834                if (gettype($value) === 'string') {
835                    $value = strtolower($value);
836                }
837            }
838
839            switch($name) {
840                case 'position':
841                    if (($value !== self::STANDARD) and ($value !== self::RIGHT) and ($value !== self::LEFT)) {
842                        require_once 'Zend/Currency/Exception.php';
843                        throw new Zend_Currency_Exception("Unknown position '" . $value . "'");
844                    }
845
846                    break;
847
848                case 'format':
849                    if ((empty($value) === false) and (Zend_Locale::isLocale($value, null, false) === false)) {
850                        if (!is_string($value) || (strpos($value, '0') === false)) {
851                            require_once 'Zend/Currency/Exception.php';
852                            throw new Zend_Currency_Exception("'" .
853                                ((gettype($value) === 'object') ? get_class($value) : $value)
854                                . "' is no format token");
855                        }
856                    }
857                    break;
858
859                case 'display':
860                    if (is_numeric($value) and ($value !== self::NO_SYMBOL) and ($value !== self::USE_SYMBOL) and
861                        ($value !== self::USE_SHORTNAME) and ($value !== self::USE_NAME)) {
862                        require_once 'Zend/Currency/Exception.php';
863                        throw new Zend_Currency_Exception("Unknown display '$value'");
864                    }
865                    break;
866
867                case 'precision':
868                    if ($value === null) {
869                        $value = -1;
870                    }
871
872                    if (($value < -1) or ($value > 30)) {
873                        require_once 'Zend/Currency/Exception.php';
874                        throw new Zend_Currency_Exception("'$value' precision has to be between -1 and 30.");
875                    }
876                    break;
877
878                case 'script':
879                    try {
880                        Zend_Locale_Format::convertNumerals(0, $options['script']);
881                    } catch (Zend_Locale_Exception $e) {
882                        require_once 'Zend/Currency/Exception.php';
883                        throw new Zend_Currency_Exception($e->getMessage());
884                    }
885                    break;
886
887                default:
888                    break;
889            }
890        }
891
892        return $options;
893    }
894}