Skip to content

Instantly share code, notes, and snippets.

@tomcorbett
Last active May 17, 2024 21:53
Show Gist options
  • Save tomcorbett/8a914e36be6b77eb5ae1df9fc62d22b8 to your computer and use it in GitHub Desktop.
Save tomcorbett/8a914e36be6b77eb5ae1df9fc62d22b8 to your computer and use it in GitHub Desktop.
Simple converter with hardcoded values for converting prices based on pricing parity data from ourworld in data
<?php
/**
* Using data from 2021 (latest) from here: https://ourworldindata.org/grapher/gdp-price-levels-relative-to-the-us?tab=table&time=latest
*/
class PricingParityConverter
{
const COUNTRY_MAP = [
'AF' => ['name' => 'Afghanistan', 'code3' => 'AFG'],
'AL' => ['name' => 'Albania', 'code3' => 'ALB'],
'DZ' => ['name' => 'Algeria', 'code3' => 'DZA'],
'AO' => ['name' => 'Angola', 'code3' => 'AGO'],
'AG' => ['name' => 'Antigua and Barbuda', 'code3' => 'ATG'],
'AR' => ['name' => 'Argentina', 'code3' => 'ARG'],
'AM' => ['name' => 'Armenia', 'code3' => 'ARM'],
'AW' => ['name' => 'Aruba', 'code3' => 'ABW'],
'AU' => ['name' => 'Australia', 'code3' => 'AUS'],
'AT' => ['name' => 'Austria', 'code3' => 'AUT'],
'AZ' => ['name' => 'Azerbaijan', 'code3' => 'AZE'],
'BS' => ['name' => 'Bahamas', 'code3' => 'BHS'],
'BH' => ['name' => 'Bahrain', 'code3' => 'BHR'],
'BD' => ['name' => 'Bangladesh', 'code3' => 'BGD'],
'BB' => ['name' => 'Barbados', 'code3' => 'BRB'],
'BY' => ['name' => 'Belarus', 'code3' => 'BLR'],
'BE' => ['name' => 'Belgium', 'code3' => 'BEL'],
'BZ' => ['name' => 'Belize', 'code3' => 'BLZ'],
'BJ' => ['name' => 'Benin', 'code3' => 'BEN'],
'BM' => ['name' => 'Bermuda', 'code3' => 'BMU'],
'BT' => ['name' => 'Bhutan', 'code3' => 'BTN'],
'BO' => ['name' => 'Bolivia', 'code3' => 'BOL'],
'BA' => ['name' => 'Bosnia and Herzegovina', 'code3' => 'BIH'],
'BW' => ['name' => 'Botswana', 'code3' => 'BWA'],
'BR' => ['name' => 'Brazil', 'code3' => 'BRA'],
'BN' => ['name' => 'Brunei Darussalam', 'code3' => 'BRN'],
'BG' => ['name' => 'Bulgaria', 'code3' => 'BGR'],
'BF' => ['name' => 'Burkina Faso', 'code3' => 'BFA'],
'BI' => ['name' => 'Burundi', 'code3' => 'BDI'],
'KH' => ['name' => 'Cambodia', 'code3' => 'KHM'],
'CM' => ['name' => 'Cameroon', 'code3' => 'CMR'],
'CA' => ['name' => 'Canada', 'code3' => 'CAN'],
'CV' => ['name' => 'Cape Verde', 'code3' => 'CPV'],
'KY' => ['name' => 'Cayman Islands', 'code3' => 'CYM'],
'CF' => ['name' => 'Central African Republic', 'code3' => 'CAF'],
'TD' => ['name' => 'Chad', 'code3' => 'TCD'],
'CL' => ['name' => 'Chile', 'code3' => 'CHL'],
'CN' => ['name' => 'China', 'code3' => 'CHN'],
'CO' => ['name' => 'Colombia', 'code3' => 'COL'],
'KM' => ['name' => 'Comoros', 'code3' => 'COM'],
'CG' => ['name' => 'Congo', 'code3' => 'COG'],
'CR' => ['name' => 'Costa Rica', 'code3' => 'CRI'],
'CI' => ['name' => "Côte d'Ivoire", 'code3' => 'CIV'],
'HR' => ['name' => 'Croatia', 'code3' => 'HRV'],
'CU' => ['name' => 'Cuba', 'code3' => 'CUB'],
'CW' => ['name' => 'Curaçao', 'code3' => 'CUW'],
'CY' => ['name' => 'Cyprus', 'code3' => 'CYP'],
'CZ' => ['name' => 'Czechia', 'code3' => 'CZE'],
'CD' => ['name' => 'Democratic Republic of the Congo', 'code3' => 'COD'],
'DK' => ['name' => 'Denmark', 'code3' => 'DNK'],
'DJ' => ['name' => 'Djibouti', 'code3' => 'DJI'],
'DM' => ['name' => 'Dominica', 'code3' => 'DMA'],
'DO' => ['name' => 'Dominican Republic', 'code3' => 'DOM'],
'EC' => ['name' => 'Ecuador', 'code3' => 'ECU'],
'EG' => ['name' => 'Egypt', 'code3' => 'EGY'],
'SV' => ['name' => 'El Salvador', 'code3' => 'SLV'],
'GQ' => ['name' => 'Equatorial Guinea', 'code3' => 'GNQ'],
'ER' => ['name' => 'Eritrea', 'code3' => 'ERI'],
'EE' => ['name' => 'Estonia', 'code3' => 'EST'],
'SZ' => ['name' => 'Eswatini', 'code3' => 'SWZ'],
'ET' => ['name' => 'Ethiopia', 'code3' => 'ETH'],
'FJ' => ['name' => 'Fiji', 'code3' => 'FJI'],
'FI' => ['name' => 'Finland', 'code3' => 'FIN'],
'FR' => ['name' => 'France', 'code3' => 'FRA'],
'GA' => ['name' => 'Gabon', 'code3' => 'GAB'],
'GM' => ['name' => 'Gambia', 'code3' => 'GMB'],
'GE' => ['name' => 'Georgia', 'code3' => 'GEO'],
'DE' => ['name' => 'Germany', 'code3' => 'DEU'],
'GH' => ['name' => 'Ghana', 'code3' => 'GHA'],
'GR' => ['name' => 'Greece', 'code3' => 'GRC'],
'GD' => ['name' => 'Grenada', 'code3' => 'GRD'],
'GT' => ['name' => 'Guatemala', 'code3' => 'GTM'],
'GN' => ['name' => 'Guinea', 'code3' => 'GIN'],
'GW' => ['name' => 'Guinea-Bissau', 'code3' => 'GNB'],
'GY' => ['name' => 'Guyana', 'code3' => 'GUY'],
'HT' => ['name' => 'Haiti', 'code3' => 'HTI'],
'HN' => ['name' => 'Honduras', 'code3' => 'HND'],
'HK' => ['name' => 'Hong Kong', 'code3' => 'HKG'],
'HU' => ['name' => 'Hungary', 'code3' => 'HUN'],
'IS' => ['name' => 'Iceland', 'code3' => 'ISL'],
'IN' => ['name' => 'India', 'code3' => 'IND'],
'ID' => ['name' => 'Indonesia', 'code3' => 'IDN'],
'IR' => ['name' => 'Iran', 'code3' => 'IRN'],
'IQ' => ['name' => 'Iraq', 'code3' => 'IRQ'],
'IE' => ['name' => 'Ireland', 'code3' => 'IRL'],
'IL' => ['name' => 'Israel', 'code3' => 'ISR'],
'IT' => ['name' => 'Italy', 'code3' => 'ITA'],
'JM' => ['name' => 'Jamaica', 'code3' => 'JAM'],
'JP' => ['name' => 'Japan', 'code3' => 'JPN'],
'JO' => ['name' => 'Jordan', 'code3' => 'JOR'],
'KZ' => ['name' => 'Kazakhstan', 'code3' => 'KAZ'],
'KE' => ['name' => 'Kenya', 'code3' => 'KEN'],
'KI' => ['name' => 'Kiribati', 'code3' => 'KIR'],
'XK' => ['name' => 'Kosovo', 'code3' => 'XKX'],
'KW' => ['name' => 'Kuwait', 'code3' => 'KWT'],
'KG' => ['name' => 'Kyrgyzstan', 'code3' => 'KGZ'],
'LA' => ['name' => 'Laos', 'code3' => 'LAO'],
'LV' => ['name' => 'Latvia', 'code3' => 'LVA'],
'LB' => ['name' => 'Lebanon', 'code3' => 'LBN'],
'LS' => ['name' => 'Lesotho', 'code3' => 'LSO'],
'LR' => ['name' => 'Liberia', 'code3' => 'LBR'],
'LY' => ['name' => 'Libya', 'code3' => 'LBY'],
'LT' => ['name' => 'Lithuania', 'code3' => 'LTU'],
'LU' => ['name' => 'Luxembourg', 'code3' => 'LUX'],
'MO' => ['name' => 'Macao', 'code3' => 'MAC'],
'MG' => ['name' => 'Madagascar', 'code3' => 'MDG'],
'MW' => ['name' => 'Malawi', 'code3' => 'MWI'],
'MY' => ['name' => 'Malaysia', 'code3' => 'MYS'],
'MV' => ['name' => 'Maldives', 'code3' => 'MDV'],
'ML' => ['name' => 'Mali', 'code3' => 'MLI'],
'MT' => ['name' => 'Malta', 'code3' => 'MLT'],
'MH' => ['name' => 'Marshall Islands', 'code3' => 'MHL'],
'MR' => ['name' => 'Mauritania', 'code3' => 'MRT'],
'MU' => ['name' => 'Mauritius', 'code3' => 'MUS'],
'MX' => ['name' => 'Mexico', 'code3' => 'MEX'],
'FM' => ['name' => 'Micronesia', 'code3' => 'FSM'],
'MD' => ['name' => 'Moldova', 'code3' => 'MDA'],
'MN' => ['name' => 'Mongolia', 'code3' => 'MNG'],
'ME' => ['name' => 'Montenegro', 'code3' => 'MNE'],
'MA' => ['name' => 'Morocco', 'code3' => 'MAR'],
'MZ' => ['name' => 'Mozambique', 'code3' => 'MOZ'],
'MM' => ['name' => 'Myanmar', 'code3' => 'MMR'],
'NA' => ['name' => 'Namibia', 'code3' => 'NAM'],
'NR' => ['name' => 'Nauru', 'code3' => 'NRU'],
'NP' => ['name' => 'Nepal', 'code3' => 'NPL'],
'NL' => ['name' => 'Netherlands', 'code3' => 'NLD'],
'NZ' => ['name' => 'New Zealand', 'code3' => 'NZL'],
'NI' => ['name' => 'Nicaragua', 'code3' => 'NIC'],
'NE' => ['name' => 'Niger', 'code3' => 'NER'],
'NG' => ['name' => 'Nigeria', 'code3' => 'NGA'],
'MK' => ['name' => 'North Macedonia', 'code3' => 'MKD'],
'NO' => ['name' => 'Norway', 'code3' => 'NOR'],
'OM' => ['name' => 'Oman', 'code3' => 'OMN'],
'PK' => ['name' => 'Pakistan', 'code3' => 'PAK'],
'PW' => ['name' => 'Palau', 'code3' => 'PLW'],
'PS' => ['name' => 'Palestine', 'code3' => 'PSE'],
'PA' => ['name' => 'Panama', 'code3' => 'PAN'],
'PG' => ['name' => 'Papua New Guinea', 'code3' => 'PNG'],
'PY' => ['name' => 'Paraguay', 'code3' => 'PRY'],
'PE' => ['name' => 'Peru', 'code3' => 'PER'],
'PH' => ['name' => 'Philippines', 'code3' => 'PHL'],
'PL' => ['name' => 'Poland', 'code3' => 'POL'],
'PT' => ['name' => 'Portugal', 'code3' => 'PRT'],
'PR' => ['name' => 'Puerto Rico', 'code3' => 'PRI'],
'QA' => ['name' => 'Qatar', 'code3' => 'QAT'],
'RO' => ['name' => 'Romania', 'code3' => 'ROU'],
'RU' => ['name' => 'Russia', 'code3' => 'RUS'],
'RW' => ['name' => 'Rwanda', 'code3' => 'RWA'],
'KN' => ['name' => 'Saint Kitts and Nevis', 'code3' => 'KNA'],
'LC' => ['name' => 'Saint Lucia', 'code3' => 'LCA'],
'VC' => ['name' => 'Saint Vincent and the Grenadines', 'code3' => 'VCT'],
'WS' => ['name' => 'Samoa', 'code3' => 'WSM'],
'SM' => ['name' => 'San Marino', 'code3' => 'SMR'],
'ST' => ['name' => 'Sao Tome and Principe', 'code3' => 'STP'],
'SA' => ['name' => 'Saudi Arabia', 'code3' => 'SAU'],
'SN' => ['name' => 'Senegal', 'code3' => 'SEN'],
'RS' => ['name' => 'Serbia', 'code3' => 'SRB'],
'SC' => ['name' => 'Seychelles', 'code3' => 'SYC'],
'SL' => ['name' => 'Sierra Leone', 'code3' => 'SLE'],
'SG' => ['name' => 'Singapore', 'code3' => 'SGP'],
'SX' => ['name' => 'Sint Maarten', 'code3' => 'SXM'],
'SK' => ['name' => 'Slovakia', 'code3' => 'SVK'],
'SI' => ['name' => 'Slovenia', 'code3' => 'SVN'],
'SB' => ['name' => 'Solomon Islands', 'code3' => 'SLB'],
'SO' => ['name' => 'Somalia', 'code3' => 'SOM'],
'ZA' => ['name' => 'South Africa', 'code3' => 'ZAF'],
'KR' => ['name' => 'South Korea', 'code3' => 'KOR'],
'SS' => ['name' => 'South Sudan', 'code3' => 'SSD'],
'ES' => ['name' => 'Spain', 'code3' => 'ESP'],
'LK' => ['name' => 'Sri Lanka', 'code3' => 'LKA'],
'SD' => ['name' => 'Sudan', 'code3' => 'SDN'],
'SR' => ['name' => 'Suriname', 'code3' => 'SUR'],
'SE' => ['name' => 'Sweden', 'code3' => 'SWE'],
'CH' => ['name' => 'Switzerland', 'code3' => 'CHE'],
'SY' => ['name' => 'Syria', 'code3' => 'SYR'],
'TW' => ['name' => 'Taiwan', 'code3' => 'TWN'],
'TJ' => ['name' => 'Tajikistan', 'code3' => 'TJK'],
'TZ' => ['name' => 'Tanzania', 'code3' => 'TZA'],
'TH' => ['name' => 'Thailand', 'code3' => 'THA'],
'TL' => ['name' => 'Timor-Leste', 'code3' => 'TLS'],
'TG' => ['name' => 'Togo', 'code3' => 'TGO'],
'TO' => ['name' => 'Tonga', 'code3' => 'TON'],
'TT' => ['name' => 'Trinidad and Tobago', 'code3' => 'TTO'],
'TN' => ['name' => 'Tunisia', 'code3' => 'TUN'],
'TR' => ['name' => 'Turkey', 'code3' => 'TUR'],
'TM' => ['name' => 'Turkmenistan', 'code3' => 'TKM'],
'TC' => ['name' => 'Turks and Caicos Islands', 'code3' => 'TCA'],
'TV' => ['name' => 'Tuvalu', 'code3' => 'TUV'],
'UG' => ['name' => 'Uganda', 'code3' => 'UGA'],
'UA' => ['name' => 'Ukraine', 'code3' => 'UKR'],
'AE' => ['name' => 'United Arab Emirates', 'code3' => 'ARE'],
'GB' => ['name' => 'United Kingdom', 'code3' => 'GBR'],
'US' => ['name' => 'United States', 'code3' => 'USA'],
'UY' => ['name' => 'Uruguay', 'code3' => 'URY'],
'UZ' => ['name' => 'Uzbekistan', 'code3' => 'UZB'],
'VU' => ['name' => 'Vanuatu', 'code3' => 'VUT'],
'VE' => ['name' => 'Venezuela', 'code3' => 'VEN'],
'VN' => ['name' => 'Vietnam', 'code3' => 'VNM'],
'YE' => ['name' => 'Yemen', 'code3' => 'YEM'],
'ZM' => ['name' => 'Zambia', 'code3' => 'ZMB'],
'ZW' => ['name' => 'Zimbabwe', 'code3' => 'ZWE']
];
const PPP_DATA = [
'AFG' => ['ppp' => 0.22],
'ALB' => ['ppp' => 0.41],
'DZA' => ['ppp' => 0.3],
'AGO' => ['ppp' => 0.3],
'ATG' => ['ppp' => 0.75],
'ARG' => ['ppp' => 0.45],
'ARM' => ['ppp' => 0.32],
'ABW' => ['ppp' => 0.69],
'AUS' => ['ppp' => 1.07],
'AUT' => ['ppp' => 0.91],
'AZE' => ['ppp' => 0.34],
'BHS' => ['ppp' => 0.83],
'BHR' => ['ppp' => 0.49],
'BGD' => ['ppp' => 0.38],
'BRB' => ['ppp' => 1.14],
'BLR' => ['ppp' => 0.34],
'BEL' => ['ppp' => 0.88],
'BLZ' => ['ppp' => 0.65],
'BEN' => ['ppp' => 0.36],
'BMU' => ['ppp' => 1.29],
'BTN' => ['ppp' => 0.27],
'BOL' => ['ppp' => 0.38],
'BIH' => ['ppp' => 0.41],
'BWA' => ['ppp' => 0.42],
'BRA' => ['ppp' => 0.47],
'BRN' => ['ppp' => 0.48],
'BGR' => ['ppp' => 0.44],
'BFA' => ['ppp' => 0.37],
'BDI' => ['ppp' => 0.29],
'KHM' => ['ppp' => 0.34],
'CMR' => ['ppp' => 0.41],
'CAN' => ['ppp' => 1.00],
'CPV' => ['ppp' => 0.49],
'CYM' => ['ppp' => 1.17],
'CAF' => ['ppp' => 0.5],
'TCD' => ['ppp' => 0.44],
'CHL' => ['ppp' => 0.57],
'CHN' => ['ppp' => 0.65],
'COL' => ['ppp' => 0.36],
'COM' => ['ppp' => 0.44],
'COG' => ['ppp' => null],
'CRI' => ['ppp' => 0.53],
'CIV' => ['ppp' => 0.44],
'HRV' => ['ppp' => 0.52],
'CUW' => ['ppp' => 0.78],
'CYP' => ['ppp' => 0.72],
'CZE' => ['ppp' => 0.6],
'COD' => ['ppp' => 0.49],
'DNK' => ['ppp' => 1.05],
'DJI' => ['ppp' => 0.58],
'DMA' => ['ppp' => 0.64],
'DOM' => ['ppp' => 0.41],
'TLS' => ['ppp' => 0.5],
'ECU' => ['ppp' => 0.51],
'EGY' => ['ppp' => 0.29],
'SLV' => ['ppp' => 0.46],
'GNQ' => ['ppp' => 0.47],
'ERI' => ['ppp' => 0.36],
'EST' => ['ppp' => 0.65],
'SWZ' => ['ppp' => 0.41],
'ETH' => ['ppp' => 0.36],
'FJI' => ['ppp' => 0.41],
'FIN' => ['ppp' => 0.98],
'FRA' => ['ppp' => 0.86],
'GAB' => ['ppp' => 0.57],
'GMB' => ['ppp' => 0.34],
'GEO' => ['ppp' => 0.3],
'DEU' => ['ppp' => 0.88],
'GHA' => ['ppp' => 0.4],
'GRC' => ['ppp' => 0.65],
'GRD' => ['ppp' => 0.6],
'GTM' => ['ppp' => 0.51],
'GIN' => ['ppp' => 0.41],
'GNB' => ['ppp' => 0.4],
'GUY' => ['ppp' => 0.42],
'HTI' => ['ppp' => 0.58],
'HND' => ['ppp' => 0.45],
'HKG' => ['ppp' => 0.76],
'HUN' => ['ppp' => 0.51],
'ISL' => ['ppp' => 1.19],
'IND' => ['ppp' => 0.31],
'IDN' => ['ppp' => 0.33],
'IRN' => ['ppp' => 0.25],
'IRQ' => ['ppp' => 0.49],
'IRL' => ['ppp' => 0.93],
'ISR' => ['ppp' => 1.18],
'ITA' => ['ppp' => 0.77],
'JAM' => ['ppp' => 0.49],
'JPN' => ['ppp' => 0.91],
'JOR' => ['ppp' => 0.4],
'KAZ' => ['ppp' => 0.36],
'KEN' => ['ppp' => 0.4],
'KIR' => ['ppp' => 0.75],
'KOS' => ['ppp' => 0.4],
'KWT' => ['ppp' => 0.52],
'KGZ' => ['ppp' => 0.24],
'LAO' => ['ppp' => 0.29],
'LVA' => ['ppp' => 0.6],
'LBN' => ['ppp' => 0.29],
'LSO' => ['ppp' => 0.43],
'LBR' => ['ppp' => 0.43],
'LBY' => ['ppp' => 0.26],
'LTU' => ['ppp' => 0.55],
'LUX' => ['ppp' => 1.01],
'MAC' => ['ppp' => 0.62],
'MDG' => ['ppp' => 0.31],
'MWI' => ['ppp' => 0.39],
'MYS' => ['ppp' => 0.38],
'MDV' => ['ppp' => 0.5],
'MLI' => ['ppp' => 0.38],
'MLT' => ['ppp' => 0.69],
'MHL' => ['ppp' => 0.94],
'MRT' => ['ppp' => 0.37],
'MUS' => ['ppp' => 0.4],
'MEX' => ['ppp' => 0.5],
'FSM' => ['ppp' => 0.98],
'MDA' => ['ppp' => 0.34],
'MNG' => ['ppp' => 0.36],
'MNE' => ['ppp' => 0.41],
'MAR' => ['ppp' => 0.43],
'MOZ' => ['ppp' => 0.36],
'MMR' => ['ppp' => 0.27],
'NAM' => ['ppp' => 0.48],
'NRU' => ['ppp' => 0.81],
'NPL' => ['ppp' => 0.29],
'NLD' => ['ppp' => 0.91],
'NZL' => ['ppp' => 1.05],
'NIC' => ['ppp' => 0.33],
'NER' => ['ppp' => 0.45],
'NGA' => ['ppp' => 0.38],
'MKD' => ['ppp' => 0.37],
'NOR' => ['ppp' => 1.13],
'OMN' => ['ppp' => 0.52],
'PAK' => ['ppp' => 0.26],
'PLW' => ['ppp' => 0.8],
'PSE' => ['ppp' => 0.59],
'PAN' => ['ppp' => 0.46],
'PNG' => ['ppp' => 0.66],
'PRY' => ['ppp' => 0.39],
'PER' => ['ppp' => 0.48],
'PHL' => ['ppp' => 0.39],
'POL' => ['ppp' => 0.48],
'PRT' => ['ppp' => 0.68],
'PRI' => ['ppp' => 0.91],
'QAT' => ['ppp' => 0.66],
'ROU' => ['ppp' => 0.41],
'RUS' => ['ppp' => 0.37],
'RWA' => ['ppp' => 0.33],
'KNA' => ['ppp' => 0.62],
'LCA' => ['ppp' => 0.66],
'VCT' => ['ppp' => 0.58],
'WSM' => ['ppp' => 0.63],
'SMR' => ['ppp' => 0.76],
'STP' => ['ppp' => 0.53],
'SAU' => ['ppp' => 0.48],
'SEN' => ['ppp' => 0.43],
'SRB' => ['ppp' => 0.43],
'SYC' => ['ppp' => 0.46],
'SLE' => ['ppp' => 0.27],
'SGP' => ['ppp' => 0.62],
'SXM' => ['ppp' => 0.81],
'SVK' => ['ppp' => 0.64],
'SVN' => ['ppp' => 0.67],
'SLB' => ['ppp' => 0.87],
'SOM' => ['ppp' => 0.36],
'ZAF' => ['ppp' => 0.48],
'KOR' => ['ppp' => 0.74],
'SSD' => ['ppp' => null], // No data provided
'ESP' => ['ppp' => 0.74],
'LKA' => ['ppp' => 0.27],
'SDN' => ['ppp' => 0.18],
'SUR' => ['ppp' => 0.3],
'SWE' => ['ppp' => 1.02],
'CHE' => ['ppp' => 1.21],
'TJK' => ['ppp' => 0.21],
'TZA' => ['ppp' => 0.39],
'THA' => ['ppp' => 0.38],
'TGO' => ['ppp' => 0.42],
'TON' => ['ppp' => 0.66],
'TTO' => ['ppp' => 0.63],
'TUN' => ['ppp' => 0.34],
'TUR' => ['ppp' => 0.31],
'TKM' => ['ppp' => 0.47],
'TCA' => ['ppp' => 1.03],
'TUV' => ['ppp' => 1.04],
'UGA' => ['ppp' => 0.36],
'UKR' => ['ppp' => 0.34],
'ARE' => ['ppp' => 0.58],
'GBR' => ['ppp' => 0.95],
'USA' => ['ppp' => 1.00],
'URY' => ['ppp' => 0.69],
'UZB' => ['ppp' => 0.23],
'VUT' => ['ppp' => 0.98],
'VEN' => ['ppp' => null], // No data provided
'VNM' => ['ppp' => 0.32],
'YEM' => ['ppp' => null], // No data provided
'ZMB' => ['ppp' => 0.32],
'ZWE' => ['ppp' => 0.76]
];
protected float $minValue = 0.5;
// for example LUX has PPP of 1.01, if this value is set to false, no increase in price will ever happen
protected bool $allowIncrease = false;
/**
* @param float|null $minValue
* @param bool|null $allowIncrease
*/
public function __construct(?float $minValue = null, ?bool $allowIncrease = false)
{
// override max value if set
if (!is_null($minValue) && $minValue > 0) {
$this->minValue = $minValue;
}
// same for allow increase
if (!is_null($allowIncrease)) {
$this->allowIncrease = $allowIncrease;
}
}
/**
* @param string $countryCode
* @param float $amount
* @return float
*/
public function returnAdjustedPrice(string $countryCode, float $amount): float
{
$pppRate = $this->getPPPValueForCountry($countryCode);
// if there isn't one, return the original amount
if (!$pppRate) {
return $amount;
}
return round($pppRate * $amount, 2);
}
/**
* @param string $countryCode
* @return float|null
*/
public function getPPPValueForCountry(string $countryCode): ?float
{
// first get 3 letter country code from 2 letter country code
$countryCode3 = self::COUNTRY_MAP[$countryCode]['code3'] ?? null;
// if we can't get a country code then just return nothing...
if (!$countryCode3) {
return null;
}
$pppRate = self::PPP_DATA[$countryCode3]['ppp'] ?? null;
// if we don't have a rate, then just return nothing
if (!$pppRate) {
return null;
}
// if we're not allowed to increase and the rate is > 1 then max it out at 1s
if ($pppRate > 1 && !$this->allowIncrease) {
return 1;
}
// if the amount is below the minimum, then set to the minimum
if ($pppRate < $this->minValue) {
$pppRate = $this->minValue;
}
return $pppRate;
}
}
@jcubic
Copy link

jcubic commented May 17, 2024

This a different approach using an API that code is more functional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment