From c99677b7adca9830bf75f3de6785fb481ee838eb Mon Sep 17 00:00:00 2001 From: Leandro Silva Date: Tue, 25 Aug 2020 08:58:14 -0300 Subject: [PATCH] Updated to php 7.2 and psr/container --- CHANGELOG.md | 25 +++++++++++ composer.json | 19 +++++---- phpcs.xml | 14 ++++--- phpstan.neon | 2 + src/ConfigProvider.php | 12 +++++- src/Formatter/AbstractFormatter.php | 8 ---- src/Formatter/CsvFormatter.php | 12 +++++- src/Formatter/Formatter.php | 12 ++++++ src/Formatter/FormatterInterface.php | 7 ---- src/Formatter/JsonFormatter.php | 7 +++- src/Formatter/PhpFormatter.php | 11 ++++- src/Service/CountryService.php | 57 ++++++++++++++------------ src/Service/CountryServiceFactory.php | 16 ++++---- src/Service/LanguageService.php | 49 +++++++++++----------- src/Service/LanguageServiceFactory.php | 16 ++++---- src/Service/RegionService.php | 33 ++++++++++----- src/Service/RegionServiceFactory.php | 16 ++++---- 17 files changed, 195 insertions(+), 121 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 phpstan.neon delete mode 100644 src/Formatter/AbstractFormatter.php create mode 100644 src/Formatter/Formatter.php delete mode 100644 src/Formatter/FormatterInterface.php diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..4386fd1 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,25 @@ +# Changelog + +All notable changes to this project will be documented in this file, in reverse chronological order by release. + +## 2.1.0 - 2020-08-25 + +### Added + +- Nothing. + +### Changed + +- Updated to php 7.2 and psr/container + +### Deprecated + +- Nothing. + +### Removed + +- Nothing. + +### Fixed + +- Nothing. diff --git a/composer.json b/composer.json index 043020e..d1d54e3 100644 --- a/composer.json +++ b/composer.json @@ -2,9 +2,14 @@ "name" : "los/losi18n", "description" : "I18N middleware providing route and country, region and language lists in all languages", "require" : { - "php" : "^7.1", + "php" : "^7.2", "ext-json": "*", - "container-interop/container-interop" : "^1.1" + "psr/container": "^1.0" + }, + "require-dev": { + "phpstan/phpstan": "^0.12.0", + "doctrine/coding-standard": "^8.1", + "squizlabs/php_codesniffer": "^3.5" }, "suggest": { "los/losi18n-data": "Data files for LosI18n." @@ -23,17 +28,17 @@ "homepage" : "http://leandrosilva.info" } ], "homepage" : "http://github.com/Lansoweb/LosI18n", - "require-dev": { - "phpstan/phpstan": "^0.6.0", - "squizlabs/php_codesniffer": "^2.7", - "zendframework/zend-coding-standard": "^1.0" + "extra": { + "laminas": { + "config-provider": "LosI18n\\ConfigProvider" + } }, "scripts": { "check": [ "@cs-check", "@phpstan" ], - "phpstan": "php -d memory_limit=-1 vendor/bin/phpstan analyse -l 4 src", + "phpstan": "phpstan analyse -l 4 src", "cs-check": "phpcs", "cs-fix": "phpcbf" } diff --git a/phpcs.xml b/phpcs.xml index 762ea7d..a5b604b 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,15 +1,17 @@ - config src - - - - - + + + + + + + + diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..096cae4 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,2 @@ +parameters: + ignoreErrors: diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 7116e2f..dfed6b2 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -1,9 +1,19 @@ $this->getDependencies(), + ]; + } + + public function getDependencies(): array { return [ 'factories' => [ diff --git a/src/Formatter/AbstractFormatter.php b/src/Formatter/AbstractFormatter.php deleted file mode 100644 index 307c3cc..0000000 --- a/src/Formatter/AbstractFormatter.php +++ /dev/null @@ -1,8 +0,0 @@ -path = $path; + $this->path = $path; $this->defaultLang = $defaultLang; } /** * Returns all countries. If needed it can return only officially assigned ones by passing second parameter as true. - * @see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements * - * @param string $translatedTo - * @param bool $filterOfficiallyAssigned - * @return array + * @see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements */ public function getAllCountries(string $translatedTo = '', bool $filterOfficiallyAssigned = true): array { - if ('' === $translatedTo) { + if ($translatedTo === '') { $translatedTo = $this->defaultLang; } - $fileName = $this->path.'/'.$translatedTo.'/countries.php'; + + $fileName = $this->path . '/' . $translatedTo . '/countries.php'; if (! file_exists($fileName)) { - throw new \InvalidArgumentException("Language $translatedTo not found."); + throw new InvalidArgumentException(sprintf('Language %s not found.', $translatedTo)); } $countries = include $fileName; if ($filterOfficiallyAssigned) { - $filterKeys = ['AF','AX','AL','DZ','AS','AD','AO','AI','AQ','AG','AR','AM','AW','AU','AT','AZ','BS','BH','BD','BB','BY','BE','BZ','BJ','BM','BT','BO','BQ','BA','BW','BV','BR','IO','VG','BN','BG','BF','BI','KH','CM','CA','CV','KY','CF','TD','CL','CN','CX','CC','CO','KM','CK','CR','HR','CU','CW','CY','CZ','CD','DK','DJ','DM','DO','TL','EC','EG','SV','GQ','ER','EE','ET','FK','FO','FJ','FI','FR','GF','PF','TF','GA','GM','GE','DE','GH','GI','GR','GL','GD','GP','GU','GT','GG','GN','GW','GY','HT','HM','HN','HK','HU','IS','IN','ID','IR','IQ','IE','IM','IL','IT','CI','JM','JP','JE','JO','KZ','KE','KI','XK','KW','KG','LA','LV','LB','LS','LR','LY','LI','LT','LU','MO','MK','MG','MW','MY','MV','ML','MT','MH','MQ','MR','MU','YT','MX','FM','MD','MC','MN','ME','MS','MA','MZ','MM','NA','NR','NP','NL','NC','NZ','NI','NE','NG','NU','NF','KP','MP','NO','OM','PK','PW','PS','PA','PG','PY','PE','PH','PN','PL','PT','PR','QA','CG','RE','RO','RU','RW','BL','SH','KN','LC','MF','PM','VC','WS','SM','ST','SA','SN','RS','SC','SL','SG','SX','SK','SI','SB','SO','ZA','GS','KR','SS','ES','LK','SD','SR','SJ','SZ','SE','CH','SY','TW','TJ','TZ','TH','TG','TK','TO','TT','TN','TR','TM','TC','TV','VI','UG','UA','AE','GB','US','UM','UY','UZ','VU','VA','VE','VN','WF','EH','YE','ZM','ZW']; // @ignoreCodingStandards + // phpcs:disable + $filterKeys = ['AF', 'AX', 'AL', 'DZ', 'AS', 'AD', 'AO', 'AI', 'AQ', 'AG', 'AR', 'AM', 'AW', 'AU', 'AT', 'AZ', 'BS', 'BH', 'BD', 'BB', 'BY', 'BE', 'BZ', 'BJ', 'BM', 'BT', 'BO', 'BQ', 'BA', 'BW', 'BV', 'BR', 'IO', 'VG', 'BN', 'BG', 'BF', 'BI', 'KH', 'CM', 'CA', 'CV', 'KY', 'CF', 'TD', 'CL', 'CN', 'CX', 'CC', 'CO', 'KM', 'CK', 'CR', 'HR', 'CU', 'CW', 'CY', 'CZ', 'CD', 'DK', 'DJ', 'DM', 'DO', 'TL', 'EC', 'EG', 'SV', 'GQ', 'ER', 'EE', 'ET', 'FK', 'FO', 'FJ', 'FI', 'FR', 'GF', 'PF', 'TF', 'GA', 'GM', 'GE', 'DE', 'GH', 'GI', 'GR', 'GL', 'GD', 'GP', 'GU', 'GT', 'GG', 'GN', 'GW', 'GY', 'HT', 'HM', 'HN', 'HK', 'HU', 'IS', 'IN', 'ID', 'IR', 'IQ', 'IE', 'IM', 'IL', 'IT', 'CI', 'JM', 'JP', 'JE', 'JO', 'KZ', 'KE', 'KI', 'XK', 'KW', 'KG', 'LA', 'LV', 'LB', 'LS', 'LR', 'LY', 'LI', 'LT', 'LU', 'MO', 'MK', 'MG', 'MW', 'MY', 'MV', 'ML', 'MT', 'MH', 'MQ', 'MR', 'MU', 'YT', 'MX', 'FM', 'MD', 'MC', 'MN', 'ME', 'MS', 'MA', 'MZ', 'MM', 'NA', 'NR', 'NP', 'NL', 'NC', 'NZ', 'NI', 'NE', 'NG', 'NU', 'NF', 'KP', 'MP', 'NO', 'OM', 'PK', 'PW', 'PS', 'PA', 'PG', 'PY', 'PE', 'PH', 'PN', 'PL', 'PT', 'PR', 'QA', 'CG', 'RE', 'RO', 'RU', 'RW', 'BL', 'SH', 'KN', 'LC', 'MF', 'PM', 'VC', 'WS', 'SM', 'ST', 'SA', 'SN', 'RS', 'SC', 'SL', 'SG', 'SX', 'SK', 'SI', 'SB', 'SO', 'ZA', 'GS', 'KR', 'SS', 'ES', 'LK', 'SD', 'SR', 'SJ', 'SZ', 'SE', 'CH', 'SY', 'TW', 'TJ', 'TZ', 'TH', 'TG', 'TK', 'TO', 'TT', 'TN', 'TR', 'TM', 'TC', 'TV', 'VI', 'UG', 'UA', 'AE', 'GB', 'US', 'UM', 'UY', 'UZ', 'VU', 'VA', 'VE', 'VN', 'WF', 'EH', 'YE', 'ZM', 'ZW']; + // phpcs:enable $countries = array_intersect_key($countries, array_flip($filterKeys)); } return $countries; } - /** - * @param string $country - * @param string $translatedTo - * @return mixed - */ - public function getCountry(string $country, string $translatedTo = '') + public function getCountry(string $country, string $translatedTo = ''): string { - if ('' === $translatedTo) { + if ($translatedTo === '') { $translatedTo = $this->defaultLang; } - $fileName = $this->path.'/'.$translatedTo.'/countries.php'; + + $fileName = $this->path . '/' . $translatedTo . '/countries.php'; if (! file_exists($fileName)) { - throw new \InvalidArgumentException("Language $translatedTo not found."); + throw new InvalidArgumentException(sprintf('Language %s not found.', $translatedTo)); } $list = include $fileName; if (! is_array($list)) { - throw new \InvalidArgumentException("Language $translatedTo not found."); + throw new InvalidArgumentException(sprintf('Language %s not found.', $translatedTo)); } + $country = strtoupper($country); if (! array_key_exists($country, $list)) { - throw new \InvalidArgumentException("Country $country not found for $translatedTo."); + throw new InvalidArgumentException(sprintf('Country %s not found for %s.', $country, $translatedTo)); } return $list[$country]; diff --git a/src/Service/CountryServiceFactory.php b/src/Service/CountryServiceFactory.php index 44964a7..b43c5a0 100644 --- a/src/Service/CountryServiceFactory.php +++ b/src/Service/CountryServiceFactory.php @@ -1,19 +1,17 @@ has('config') ? $container->get('config') : []; - $path = $config['los_i18n']['path'] ?? 'vendor/los/losi18n-data/data'; + $config = $container->has('config') ? $container->get('config') : []; + $path = $config['los_i18n']['path'] ?? 'vendor/los/losi18n-data/data'; $defaultLang = $config['los_i18n']['default_lang'] ?? 'en'; return new CountryService($path, $defaultLang); diff --git a/src/Service/LanguageService.php b/src/Service/LanguageService.php index 5a4382e..7bdfcad 100644 --- a/src/Service/LanguageService.php +++ b/src/Service/LanguageService.php @@ -1,36 +1,38 @@ path = $path; + $this->path = $path; $this->defaultLang = $defaultLang; } - /** - * @param string $translatedTo - * @return array - */ public function getAllLanguages(string $translatedTo = ''): array { - if ('' === $translatedTo) { + if ($translatedTo === '') { $translatedTo = $this->defaultLang; } - $fileName = $this->path.'/'.$translatedTo.'/languages.php'; + + $fileName = $this->path . '/' . $translatedTo . '/languages.php'; if (! file_exists($fileName)) { - throw new \InvalidArgumentException("Language $translatedTo not found."); + throw new InvalidArgumentException(sprintf('Language %s not found.', $translatedTo)); } return include $fileName; @@ -41,35 +43,32 @@ public function getAllLanguages(string $translatedTo = ''): array */ public function getNativeLanguages(): array { - $fileName = $this->path.'/natives/languages.php'; + $fileName = $this->path . '/natives/languages.php'; if (! file_exists($fileName)) { - throw new \InvalidArgumentException("Native language file not found."); + throw new InvalidArgumentException('Native language file not found.'); } return include $fileName; } - /** - * @param string $language - * @param string $translatedTo - * @return array - */ public function getLanguage(string $language, string $translatedTo = ''): array { - if (null === $translatedTo) { + if ($translatedTo === '') { $translatedTo = $this->defaultLang; } - $fileName = $this->path.'/'.$translatedTo.'/languages.php'; + + $fileName = $this->path . '/' . $translatedTo . '/languages.php'; if (! file_exists($fileName)) { - throw new \InvalidArgumentException("Language $translatedTo not found."); + throw new InvalidArgumentException(sprintf('Language %s not found.', $translatedTo)); } $list = include $fileName; if (! is_array($list)) { - throw new \InvalidArgumentException("Language $translatedTo not found."); + throw new InvalidArgumentException(sprintf('Language %s not found.', $translatedTo)); } + if (! array_key_exists($language, $list)) { - throw new \InvalidArgumentException("Language $language not found for $translatedTo."); + throw new InvalidArgumentException(sprintf('Language %s not found for %s.', $language, $translatedTo)); } return $list[$language]; diff --git a/src/Service/LanguageServiceFactory.php b/src/Service/LanguageServiceFactory.php index 3942d4d..36d15b1 100644 --- a/src/Service/LanguageServiceFactory.php +++ b/src/Service/LanguageServiceFactory.php @@ -1,19 +1,17 @@ has('config') ? $container->get('config') : []; - $path = $config['los_i18n']['path'] ?? 'vendor/los/losi18n-data/data'; + $config = $container->has('config') ? $container->get('config') : []; + $path = $config['los_i18n']['path'] ?? 'vendor/los/losi18n-data/data'; $defaultLang = $config['los_i18n']['default_lang'] ?? 'en'; return new LanguageService($path, $defaultLang); diff --git a/src/Service/RegionService.php b/src/Service/RegionService.php index 9af9ec5..4161629 100644 --- a/src/Service/RegionService.php +++ b/src/Service/RegionService.php @@ -1,48 +1,61 @@ path = $path; + $this->path = $path; $this->defaultLang = $defaultLang; } public function getAllRegions(string $translatedTo = ''): array { - if ('' === $translatedTo) { + if ($translatedTo === '') { $translatedTo = $this->defaultLang; } - $fileName = $this->path.'/'.$translatedTo.'/regions.php'; + + $fileName = $this->path . '/' . $translatedTo . '/regions.php'; if (! file_exists($fileName)) { - throw new \InvalidArgumentException("Language $translatedTo not found."); + throw new InvalidArgumentException(sprintf('Language %s not found.', $translatedTo)); } return include $fileName; } - public function getRegion(string $region, string $translatedTo = '') + public function getRegion(string $region, string $translatedTo = ''): string { - if ('' === $translatedTo) { + if ($translatedTo === '') { $translatedTo = $this->defaultLang; } - $fileName = $this->path.'/'.$translatedTo.'/regions.php'; + + $fileName = $this->path . '/' . $translatedTo . '/regions.php'; if (! file_exists($fileName)) { - throw new \InvalidArgumentException("Language $translatedTo not found."); + throw new InvalidArgumentException(sprintf('Language %s not found.', $translatedTo)); } $list = include $fileName; if (! is_array($list)) { - throw new \InvalidArgumentException("Language $translatedTo not found."); + throw new InvalidArgumentException(sprintf('Language %s not found.', $translatedTo)); } + if (! array_key_exists($region, $list)) { - throw new \InvalidArgumentException("Region $region not found for $translatedTo."); + throw new InvalidArgumentException(sprintf('Region %s not found for %s.', $region, $translatedTo)); } return $list[$region]; diff --git a/src/Service/RegionServiceFactory.php b/src/Service/RegionServiceFactory.php index 82000a5..cb9e20c 100644 --- a/src/Service/RegionServiceFactory.php +++ b/src/Service/RegionServiceFactory.php @@ -1,19 +1,17 @@ has('config') ? $container->get('config') : []; - $path = $config['los_i18n']['path'] ?? 'vendor/los/losi18n-data/data'; + $config = $container->has('config') ? $container->get('config') : []; + $path = $config['los_i18n']['path'] ?? 'vendor/los/losi18n-data/data'; $defaultLang = $config['los_i18n']['default_lang'] ?? 'en'; return new RegionService($path, $defaultLang);