Skip to content

Commit

Permalink
Updated to php 7.2 and psr/container
Browse files Browse the repository at this point in the history
  • Loading branch information
Lansoweb committed Aug 25, 2020
1 parent ef8c513 commit c99677b
Show file tree
Hide file tree
Showing 17 changed files with 195 additions and 121 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 12 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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"
}
Expand Down
14 changes: 8 additions & 6 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?xml version="1.0"?>
<ruleset name="Zend Framework Coding Standard">
<rule ref="./vendor/zendframework/zend-coding-standard/ruleset.xml"/>

<!-- Paths to check -->
<file>config</file>
<file>src</file>

<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="160"/>
<property name="absoluteLineLimit" value="0"/>
</properties>
<rule ref="Doctrine">
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableParameterTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversablePropertyTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableReturnTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint"/>
<exclude name="Squiz.Commenting.FunctionComment.WrongStyle"/>
</rule>
</ruleset>
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
ignoreErrors:
12 changes: 11 additions & 1 deletion src/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
<?php

declare(strict_types=1);

namespace LosI18n;

class ConfigProvider
{
public function getDependenciesConfig()
public function __invoke(): array
{
return [
'dependencies' => $this->getDependencies(),
];
}

public function getDependencies(): array
{
return [
'factories' => [
Expand Down
8 changes: 0 additions & 8 deletions src/Formatter/AbstractFormatter.php

This file was deleted.

12 changes: 11 additions & 1 deletion src/Formatter/CsvFormatter.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
<?php

declare(strict_types=1);

namespace LosI18n\Formatter;

class CsvFormatter extends AbstractFormatter
use function fclose;
use function fopen;
use function fputcsv;
use function rewind;
use function stream_get_contents;

class CsvFormatter implements Formatter
{
public function format(array $data): string
{
Expand All @@ -16,6 +25,7 @@ public function format(array $data): string
$name,
]);
}

rewind($outstream);
$csv = stream_get_contents($outstream);
fclose($outstream);
Expand Down
12 changes: 12 additions & 0 deletions src/Formatter/Formatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace LosI18n\Formatter;

interface Formatter
{
public function format(array $data): string;

public function getExtension(): string;
}
7 changes: 0 additions & 7 deletions src/Formatter/FormatterInterface.php

This file was deleted.

7 changes: 6 additions & 1 deletion src/Formatter/JsonFormatter.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?php

declare(strict_types=1);

namespace LosI18n\Formatter;

class JsonFormatter extends AbstractFormatter
use function json_encode;

class JsonFormatter implements Formatter
{
public function format(array $data): string
{
Expand Down
11 changes: 10 additions & 1 deletion src/Formatter/PhpFormatter.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
<?php

declare(strict_types=1);

namespace LosI18n\Formatter;

class PhpFormatter extends AbstractFormatter
use function sprintf;
use function str_replace;
use function var_export;

use const PHP_EOL;

class PhpFormatter implements Formatter
{
public function format(array $data): string
{
Expand Down
57 changes: 30 additions & 27 deletions src/Service/CountryService.php
Original file line number Diff line number Diff line change
@@ -1,74 +1,77 @@
<?php

declare(strict_types=1);

namespace LosI18n\Service;

use InvalidArgumentException;

use function array_flip;
use function array_intersect_key;
use function array_key_exists;
use function file_exists;
use function is_array;
use function sprintf;
use function strtoupper;

final class CountryService
{
private $defaultLang;
private $path;
private string $defaultLang;
private string $path;

/**
* CountryService constructor.
* @param string $path
* @param string $defaultLang
*/
public function __construct(string $path, string $defaultLang)
{
$this->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];
Expand Down
16 changes: 7 additions & 9 deletions src/Service/CountryServiceFactory.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
<?php

declare(strict_types=1);

namespace LosI18n\Service;

use Interop\Container\ContainerInterface;
use Psr\Container\ContainerInterface;

class CountryServiceFactory
{

/**
* {@inheritDoc}
* @see \Zend\ServiceManager\Factory\FactoryInterface::__invoke()
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
public function __invoke(ContainerInterface $container): CountryService
{
$config = $container->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);
Expand Down
Loading

0 comments on commit c99677b

Please sign in to comment.