| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace EventEspresso\core\services\currency; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use EE_Error; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use EE_Organization_Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use EEH_File; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use EventEspresso\core\domain\values\currency\Currency; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use EventEspresso\core\entities\Label; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use EventEspresso\core\exceptions\InvalidDataTypeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use EventEspresso\core\exceptions\InvalidIdentifierException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use EventEspresso\core\exceptions\InvalidInterfaceException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | defined('EVENT_ESPRESSO_VERSION') || exit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * Class CurrencyFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * Factory class for creating Currency objects | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @package EventEspresso\core\services\currency | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @author  Brent Christensen | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @since   $VID:$ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | class CurrencyFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var array[] $country_currency_data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $country_currency_data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @var array[] $country_currencies_by_iso_code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     private $country_currencies_by_iso_code; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @var array[] $country_currencies_by_currency | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     private $country_currencies_by_currency; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @var string $site_country_iso | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     private $site_country_iso; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * CurrencyFactory constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param EE_Organization_Config $organization_config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     public function __construct(EE_Organization_Config  $organization_config) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->site_country_iso = $organization_config->CNT_ISO; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return array[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @throws EE_Error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     private function getCountryCurrencyData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if ($this->country_currency_data === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $country_currency_data = json_decode( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 EEH_File::get_file_contents(__DIR__ . DS . 'country-currencies.json'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $this->parseCountryCurrencyData($country_currency_data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return $this->country_currency_data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param array[] $country_currency_data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     private function parseCountryCurrencyData($country_currency_data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         foreach ($country_currency_data as $country_currency) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $this->country_currencies_by_iso_code[ $country_currency['CountryISO'] ] = $country_currency; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $this->country_currencies_by_currency[ $country_currency['CurrencyCode'] ] = $country_currency; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $this->country_currency_data = $country_currency_data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @param array $country_currency | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @return Currency | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     private function createCurrencyFromCountryCurrency(array $country_currency) | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         return new Currency( | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             $country_currency['CurrencyCode'], | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             new Label( | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |                 $country_currency['CurrencyNameSingle'], | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |                 $country_currency['CurrencyNamePlural'] | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |             ), | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |             $country_currency['CurrencySign'], | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |             $country_currency['CurrencySignB4'], | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |             $country_currency['CurrencyDecimalPlaces'], | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |             $country_currency['CurrencyDecimalMark'], | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |             $country_currency['CurrencyThousands'], | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |             $country_currency['CurrencySubunits'] | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * returns a Currency object for the supplied country code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @param string $CNT_ISO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @return Currency | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @throws InvalidIdentifierException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @throws InvalidDataTypeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @throws InvalidInterfaceException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @throws EE_Error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     public function createFromCountryCode($CNT_ISO = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $this->getCountryCurrencyData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $CNT_ISO = $CNT_ISO !== null ? $CNT_ISO : $this->site_country_iso; | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 131 |  | View Code Duplication |         if(! isset($this->country_currencies_by_iso_code[ $CNT_ISO ])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             throw new InvalidArgumentException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                     esc_html__( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                         'Valid country currency data could not be found for the "%1$s" country code;', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                         'event_espresso' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                     ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                     $CNT_ISO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         return $this->createCurrencyFromCountryCurrency( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             $this->country_currencies_by_iso_code[ $CNT_ISO ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * returns a Currency object for the supplied currency code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * PLZ NOTE: variations may exist between how different countries display the same currency, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * so it may be necessary to use CreateCurrency::fromCountryCode() to achieve the desired results | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * @param string $code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * @return Currency | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * @throws InvalidIdentifierException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      * @throws InvalidInterfaceException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * @throws InvalidDataTypeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * @throws EE_Error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     public function createFromCode($code) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         $this->getCountryCurrencyData(); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 165 |  | View Code Duplication |         if (! isset($this->country_currencies_by_currency[ $code ])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             throw new InvalidArgumentException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |                 sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |                     esc_html__( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |                         'A valid currency could not be found for the "%1$s" currency code;', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                         'event_espresso' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |                     ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |                     $code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         return $this->createCurrencyFromCountryCurrency( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             $this->country_currencies_by_currency[ $code ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 181 |  |  | // Location: CreateCurrency.php | 
            
                                                        
            
                                    
            
            
                | 182 |  |  |  |