Complex classes like Zend_Currency often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Zend_Currency, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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 | * 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) |
||
| 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()) |
||
| 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) |
||
| 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()) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Returns the actual currency name |
||
| 465 | * |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | public function toString() |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Returns the currency name |
||
| 475 | * |
||
| 476 | * @return string |
||
| 477 | */ |
||
| 478 | public function __toString() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Returns the set cache |
||
| 485 | * |
||
| 486 | * @return Zend_Cache_Core The set cache |
||
| 487 | */ |
||
| 488 | public static function getCache() |
||
| 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) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Returns true when a cache is set |
||
| 506 | * |
||
| 507 | * @return boolean |
||
| 508 | */ |
||
| 509 | public static function hasCache() |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Removes any set cache |
||
| 516 | * |
||
| 517 | * @return void |
||
| 518 | */ |
||
| 519 | public static function removeCache() |
||
| 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) |
||
| 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) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Returns the actual set locale |
||
| 570 | * |
||
| 571 | * @return string |
||
| 572 | */ |
||
| 573 | public function getLocale() |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Returns the value |
||
| 580 | * |
||
| 581 | * @return float |
||
| 582 | */ |
||
| 583 | public function getValue() |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Returns the set service class |
||
| 776 | * |
||
| 777 | * @return Zend_Service |
||
| 778 | */ |
||
| 779 | public function getService() |
||
| 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) |
||
| 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()) |
||
| 894 | } |
||
| 895 |