From b36043cb92f0daf3ec09763344aa0fd1d251c79a Mon Sep 17 00:00:00 2001 From: robmachado Date: Wed, 13 Oct 2021 15:09:24 -0300 Subject: [PATCH] melhorias e actions --- .coverall.yml | 0 .editorconfig | 0 .github/workflows/ci.yml | 45 ++++++ .gitignore | 0 .scrutinizer.yml | 0 .styleci.yml | 0 .travis.yml | 17 -- CHANGELOG.md | 0 CONDUCT.md | 0 CONTRIBUTING.md | 0 Estados.md | 18 +++ LICENSE | 0 Municipios.md | 29 +++- README.md | 3 +- VERSION.txt | 0 bootstrap.php | 0 composer.json | 21 +-- examples/testMun.php | 15 ++ examples/testUF.php | 0 phpstan-baseline.neon | 2 + phpstan.neon | 8 + phpunit.xml.dist | 0 src/Ibge.php | 266 ++++++++++++++++++++++++------- src/Rest.php | 27 +++- storage/municipios_11.json | 0 storage/municipios_12.json | 0 storage/municipios_13.json | 0 storage/municipios_14.json | 0 storage/municipios_15.json | 0 storage/municipios_16.json | 2 +- storage/municipios_17.json | 0 storage/municipios_21.json | 0 storage/municipios_22.json | 0 storage/municipios_23.json | 0 storage/municipios_24.json | 0 storage/municipios_25.json | 0 storage/municipios_26.json | 0 storage/municipios_27.json | 0 storage/municipios_28.json | 0 storage/municipios_29.json | 0 storage/municipios_31.json | 0 storage/municipios_32.json | 0 storage/municipios_33.json | 0 storage/municipios_41.json | 0 storage/municipios_42.json | 0 storage/municipios_43.json | 0 storage/municipios_50.json | 0 storage/municipios_51.json | 0 storage/municipios_52.json | 0 storage/municipios_53.json | 0 storage/municipios_99.json | 1 - tests/IbgeTest.php | 138 ++++++++++++++-- tests/fixtures/municipios_16.csv | 0 tests/fixtures/municipios_16.sql | 0 54 files changed, 488 insertions(+), 104 deletions(-) mode change 100644 => 100755 .coverall.yml mode change 100644 => 100755 .editorconfig create mode 100644 .github/workflows/ci.yml mode change 100644 => 100755 .gitignore mode change 100644 => 100755 .scrutinizer.yml mode change 100644 => 100755 .styleci.yml delete mode 100644 .travis.yml mode change 100644 => 100755 CHANGELOG.md mode change 100644 => 100755 CONDUCT.md mode change 100644 => 100755 CONTRIBUTING.md mode change 100644 => 100755 Estados.md mode change 100644 => 100755 LICENSE mode change 100644 => 100755 Municipios.md mode change 100644 => 100755 README.md mode change 100644 => 100755 VERSION.txt mode change 100644 => 100755 bootstrap.php mode change 100644 => 100755 composer.json mode change 100644 => 100755 examples/testMun.php mode change 100644 => 100755 examples/testUF.php create mode 100644 phpstan-baseline.neon create mode 100644 phpstan.neon mode change 100644 => 100755 phpunit.xml.dist mode change 100644 => 100755 src/Ibge.php mode change 100644 => 100755 src/Rest.php mode change 100644 => 100755 storage/municipios_11.json mode change 100644 => 100755 storage/municipios_12.json mode change 100644 => 100755 storage/municipios_13.json mode change 100644 => 100755 storage/municipios_14.json mode change 100644 => 100755 storage/municipios_15.json mode change 100644 => 100755 storage/municipios_16.json mode change 100644 => 100755 storage/municipios_17.json mode change 100644 => 100755 storage/municipios_21.json mode change 100644 => 100755 storage/municipios_22.json mode change 100644 => 100755 storage/municipios_23.json mode change 100644 => 100755 storage/municipios_24.json mode change 100644 => 100755 storage/municipios_25.json mode change 100644 => 100755 storage/municipios_26.json mode change 100644 => 100755 storage/municipios_27.json mode change 100644 => 100755 storage/municipios_28.json mode change 100644 => 100755 storage/municipios_29.json mode change 100644 => 100755 storage/municipios_31.json mode change 100644 => 100755 storage/municipios_32.json mode change 100644 => 100755 storage/municipios_33.json mode change 100644 => 100755 storage/municipios_41.json mode change 100644 => 100755 storage/municipios_42.json mode change 100644 => 100755 storage/municipios_43.json mode change 100644 => 100755 storage/municipios_50.json mode change 100644 => 100755 storage/municipios_51.json mode change 100644 => 100755 storage/municipios_52.json mode change 100644 => 100755 storage/municipios_53.json delete mode 100644 storage/municipios_99.json mode change 100644 => 100755 tests/IbgeTest.php mode change 100644 => 100755 tests/fixtures/municipios_16.csv mode change 100644 => 100755 tests/fixtures/municipios_16.sql diff --git a/.coverall.yml b/.coverall.yml old mode 100644 new mode 100755 diff --git a/.editorconfig b/.editorconfig old mode 100644 new mode 100755 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..1ea3da7 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,45 @@ +name: Pipeline +on: + push: + branches: + - master + + pull_request: + branches: + - master + +jobs: + testsuite: + name: Testes Automatizados + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + php-version: ['7.1', '7.2', '7.3', '7.4'] + + steps: + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + extensions: json, curl + + - name: checkout + uses: actions/checkout@v2 + + - name: Set phpunit version + if: matrix.php-version == '7.1' + run: composer require "phpunit/phpunit:^6.0" -W --dev + + - name: Composer Install + run: | + composer install --no-progress -o --no-ansi --no-interaction + + - name: Análises Estáticas + if: matrix.php-version != '7.1' + run: | + composer stan + + - name: Rodanto PHPUnit + run: | + composer test diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/.scrutinizer.yml b/.scrutinizer.yml old mode 100644 new mode 100755 diff --git a/.styleci.yml b/.styleci.yml old mode 100644 new mode 100755 diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 1434aa2..0000000 --- a/.travis.yml +++ /dev/null @@ -1,17 +0,0 @@ -language: php - -php: - - 7.0 - - 7.1 - - 7.2 - -dist: precise -sudo: false - -before_script: - - travis_retry composer self-update - - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source - -script: - - vendor/bin/phpcs --standard=psr2 src/ - - vendor/bin/phpunit diff --git a/CHANGELOG.md b/CHANGELOG.md old mode 100644 new mode 100755 diff --git a/CONDUCT.md b/CONDUCT.md old mode 100644 new mode 100755 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md old mode 100644 new mode 100755 diff --git a/Estados.md b/Estados.md old mode 100644 new mode 100755 index 99f440f..5c0cd90 --- a/Estados.md +++ b/Estados.md @@ -118,4 +118,22 @@ try { } ``` +## Estaticamente + +A classe também pode ser invocada estaticamente. + +```php +use NFePHP\Ibge\Ibge; + +try { + $resp = Ibge::uf()->estados()->get(); + echo "
";
+    print_r($resp);
+    echo "
"; +} catch (\Exception $e) { + echo $e->getMessage(); +} +``` + + diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/Municipios.md b/Municipios.md old mode 100644 new mode 100755 index 9accaa8..80d33ea --- a/Municipios.md +++ b/Municipios.md @@ -14,7 +14,7 @@ use NFePHP\Ibge\Ibge; try { $uf = 'SP'; $ibge = new Ibge(); - $resp = $ibge->municipios($uf)->get(); + $resp = $ibge->cidades($uf)->get(); echo "
";
     print_r($resp);
     echo "
"; @@ -33,7 +33,7 @@ use NFePHP\Ibge\Ibge; try { $uf = 35; $ibge = new Ibge(); - $resp = $ibge->municipios($uf)->toStd(); + $resp = $ibge->cidades($uf)->toStd(); echo "
";
     print_r($resp);
     echo "
"; @@ -52,7 +52,7 @@ use NFePHP\Ibge\Ibge; try { $uf = 'AM'; $ibge = new Ibge(); - $resp = $ibge->municipios($uf)->toArray(); + $resp = $ibge->cidades($uf)->toArray(); echo "
";
     print_r($resp);
     echo "
"; @@ -74,7 +74,7 @@ try { $uf = 'RJ'; $separador = '|'; $ibge = new Ibge(); - $resp = $ibge->municipios($uf)->toCSV($separador); + $resp = $ibge->cidades($uf)->toCSV($separador); echo "
";
     print_r($resp);
     echo "
"; @@ -98,7 +98,7 @@ try { $uf = 'all'; $tabela = 'estados'; $ibge = new Ibge(); - $resp = $ibge->municipios($uf)->toSQL($tabela); + $resp = $ibge->cidades($uf)->toSQL($tabela); echo "
";
     print_r($resp);
     echo "
"; @@ -118,7 +118,7 @@ use NFePHP\Ibge\Ibge; try { $ibge = new Ibge(); - $resp = $ibge->municipios('all')->refresh()->get(); + $resp = $ibge->cidades('all')->refresh()->get(); echo "
";
     print_r($resp);
     echo "
"; @@ -126,3 +126,20 @@ try { echo $e->getMessage(); } ``` + +## Estaticamente + +A classe também pode ser invocada estaticamente. + +```php +use NFePHP\Ibge\Ibge; + +try { + $resp = Ibge::uf('AP')->cidades()->get(); + echo "
";
+    print_r($resp);
+    echo "
"; +} catch (\Exception $e) { + echo $e->getMessage(); +} +``` \ No newline at end of file diff --git a/README.md b/README.md old mode 100644 new mode 100755 index 065bec5..405bac5 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Esta classe possui 2 métodos principais e vários modificadores. ## [function estados()](Estados.md) -## [function municipios($uf)](Municipios.md) +## [function cidades($uf)](Municipios.md) > *IMPORTANTE: a pasta "storage" deve ter permissões de escrita pelo usuário que executa o PHP.* @@ -86,7 +86,6 @@ Para que este pacote possa funcionar são necessários os seguintes requisitos d [ico-version]: https://img.shields.io/packagist/v/nfephp-org/sped-ibge.svg - [link-packagist]: https://packagist.org/packages/nfephp-org/sped-ibge [link-travis]: https://travis-ci.org/nfephp-org/sped-ibge [link-scrutinizer]: https://scrutinizer-ci.com/g/nfephp-org/sped-ibge/code-structure diff --git a/VERSION.txt b/VERSION.txt old mode 100644 new mode 100755 diff --git a/bootstrap.php b/bootstrap.php old mode 100644 new mode 100755 diff --git a/composer.json b/composer.json old mode 100644 new mode 100755 index ea8b161..be68d62 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "nfephp-org/sped-ibge", "type": "library", - "description": "Librery to access the services of IBGE to obtain the codes used by the system SPED", + "description": "Library to access the services of IBGE to obtain the codes used by the system SPED", "homepage": "https://github.com/nfephp-org/sped-ibge", "keywords": ["Sped", "IBGE", "Municipios", "Estatos"], "license": ["LGPL-3.0-or-later", "GPL-3.0-or-later", "MIT"], @@ -9,20 +9,21 @@ { "name": "Roberto L. Machado", "email": "linux.rlm@gmail.com", + "homepage": "https://github.com/nfephp-org", "role": "Developer" } ], "require": { "php" : ">=7.0", "ext-curl": "*", - "ext-json": "*", - "league/flysystem": "^1.0" + "ext-json": "*" }, "require-dev": { - "squizlabs/php_codesniffer": "^2.8", - "phpunit/phpunit": "^5.7", + "squizlabs/php_codesniffer": "dev-master", + "phpunit/phpunit": "^6", "scrutinizer/ocular": "^1.3", - "phpstan/phpstan": "^0.9.1" + "phpstan/phpstan": "^0.12.99", + "phpcompatibility/php-compatibility": "^9.3" }, "autoload": { "psr-4": { @@ -35,10 +36,10 @@ } }, "scripts": { - "test": "phpunit", - "phpcbf": "vendor/bin/phpcbf --standard=psr2 src/", - "phpcs": "vendor/bin/phpcs --standard=psr2 src/", - "phpstan": "vendor/bin/phpstan analyse src/ --level 7" + "test": "vendor/bin/phpunit -c phpunit.xml.dist", + "phpcbf": "vendor/bin/phpcbf src/ tests/", + "phpcs": "vendor/bin/phpcs src/ tests/", + "stan": "vendor/bin/phpstan analyse src/ tests/" }, "extra": { "branch-alias": { diff --git a/examples/testMun.php b/examples/testMun.php old mode 100644 new mode 100755 index 5141662..a1ccdb6 --- a/examples/testMun.php +++ b/examples/testMun.php @@ -7,7 +7,22 @@ try { $ibge = new Ibge(); + + //retorna JSON $resp = $ibge->cidades('16')->get(); + + //retorna ARRAY + //$resp = $ibge->cidades('16')->toArray(); + + //retorna stdClass + //$resp = $ibge->cidades('16')->toStd(); + + //retorna CSV + //$resp = $ibge->cidades('16')->toCSV(); + + //retorna SQL + //$resp = $ibge->cidades('16')->toSQL(); + echo "
";
     print_r($resp);
     echo "
"; diff --git a/examples/testUF.php b/examples/testUF.php old mode 100644 new mode 100755 diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 0000000..364905f --- /dev/null +++ b/phpstan-baseline.neon @@ -0,0 +1,2 @@ +parameters: + ignoreErrors: diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..dfdb305 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,8 @@ +includes: + - phpstan-baseline.neon + +parameters: + level: 5 + checkMissingIterableValueType: false + checkGenericClassInNonGenericObjectType: false + treatPhpDocTypesAsCertain: false diff --git a/phpunit.xml.dist b/phpunit.xml.dist old mode 100644 new mode 100755 diff --git a/src/Ibge.php b/src/Ibge.php old mode 100644 new mode 100755 index a1a05d7..a423902 --- a/src/Ibge.php +++ b/src/Ibge.php @@ -1,36 +1,172 @@ + * @copyright 2021 NFePHP Copyright (c) + * @license https://opensource.org/licenses/MIT MIT + * @link http://github.com/nfephp-org/sped-ibge + */ + namespace NFePHP\Ibge; use NFePHP\Ibge\Rest; -use League\Flysystem\Adapter\Local; -use League\Flysystem\Filesystem; -class Ibge +/** + * Class for get cities from IBGE RestAPI + * + * @category Library + * @package NFePHP\Ibge + * @author Roberto L. Machado + * @copyright 2021 NFePHP Copyright (c) + * @license https://opensource.org/licenses/MIT MIT + * @link http://github.com/nfephp-org/sped-ibge + */ +final class Ibge { const ESTADOS = 1; const MUNICIPIOS = 2; + /** + * Lista com as UF + * + * @var string + */ public $ufs; + /** + * Lista de cidades da UF + * + * @var string + */ public $cities; + /** + * Ultima consulta + * + * @var int + */ protected $lastconsult; + /** + * Ultima UF pelo codigo + * + * @var int + */ protected $lastcuf; + /** + * Ultima uf consultada + * + * @var string + */ protected $lastuf; - protected $filesystem; + /** + * Path para storage local + * + * @var string + */ + protected $path; + + /** + * Lista de ufs + * + * @var array + */ + protected $uflist = [ + 12 => 'AC', + 27 => 'AL', + 13 => 'AM', + 91 => 'AN', + 16 => 'AP', + 29 => 'BA', + 23 => 'CE', + 53 => 'DF', + 32 => 'ES', + 52 => 'GO', + 21 => 'MA', + 31 => 'MG', + 50 => 'MS', + 51 => 'MT', + 15 => 'PA', + 25 => 'PB', + 26 => 'PE', + 22 => 'PI', + 41 => 'PR', + 33 => 'RJ', + 24 => 'RN', + 11 => 'RO', + 14 => 'RR', + 43 => 'RS', + 42 => 'SC', + 28 => 'SE', + 35 => 'SP', + 17 => 'TO', + ]; + + /** + * Constructor Load flysystem and UFs from file + * + * @param string|null $uf sigla ou codigo da UF + */ + public function __construct(string $uf = null) + { + $this->path = __DIR__."/../storage"; + $this->ufs = file_get_contents($this->path . '/estados.json'); + $this->_loaduf($uf); + } + + /** + * Static instantiation off class + * + * @param string|null $uf sigla ou codigo da UF + * + * @return $this + */ + public static function uf(string $uf = null) + { + return new static($uf); + } /** - * Constructor - * Load flysystem and UFs from file + * Carrega propriedades da classe + * + * @param string $uf sigla ou codigo da UF + * + * @return void + * + * @throws \Exception */ - public function __construct() + private function _loaduf(string $uf = null) { - $adapter = new Local(__DIR__ . '/../storage/'); - $this->filesystem = new Filesystem($adapter); - $this->ufs = $this->filesystem->read('estados.json'); + if (empty($uf)) { + return; + } + if ($uf == 'all') { + $this->lastuf = $uf; + $this->lastcuf = 0; + return; + } + if (is_numeric($uf)) { + if (empty($this->uflist[$uf])) { + throw new \Exception("Esse código não existe."); + } + $this->lastuf = $this->uflist[$uf]; + $this->lastcuf = (int) $uf; + } else { + $uf = strtoupper($uf); + $codelist = array_flip($this->uflist); + if (empty($codelist[$uf])) { + throw new \Exception("Esse código não existe."); + } + $this->lastcuf = (int) $codelist[$uf]; + $this->lastuf = $uf; + } } /** * Returns this class setting for states + * * @return $this */ public function estados() @@ -41,52 +177,37 @@ public function estados() /** * Returns this class setting for cities from giving state - * @param string $uf + * + * @param string $uf sigla ou codigo da UF + * * @return $this */ - public function cidades($uf) + public function cidades(string $uf = null) { - if ($uf === 'all') { - $this->lastuf = $uf; - return $this->all(); + if (!empty($uf)) { + $this->_loaduf($uf); } - $cUF = ''; - $ufs = json_decode($this->ufs); - if (!is_numeric($uf)) { - $this->lastuf = $uf; - foreach ($ufs as $u) { - if ($u->sigla == $uf) { - $cUF = $u->id; - break; - } - } - } else { - foreach ($ufs as $u) { - if ($u->id == $uf) { - $this->lastuf = $u->sigla; - $cUF = $uf; - break; - } - } + if ($this->lastuf === 'all') { + return $this->all(); } - if (empty($cUF)) { - throw new \InvalidArgumentException("Esse estado [$uf] não existe!"); + if (empty($this->lastuf)) { + return $this; } $this->lastconsult = self::MUNICIPIOS; - $this->lastcuf = $cUF; - $path = 'municipios_'.$cUF.'.json'; - if (!$this->filesystem->has($path)) { + $path = $this->path . '/municipios_'.$this->lastcuf.'.json'; + if (!is_file($path)) { $this->refresh(); } - $this->cities = $this->filesystem->read($path); + $this->cities = file_get_contents($path); return $this; } /** * Get all brasilian cities + * * @return $this */ - public function all() + protected function all() { $this->lastconsult = self::MUNICIPIOS; $acities = []; @@ -95,11 +216,11 @@ public function all() $cUF = $u->id; $this->lastuf = $u->sigla; $this->lastcuf = $cUF; - $path = 'municipios_'.$cUF.'.json'; - if (!$this->filesystem->has($path)) { + $path = $this->path . '/municipios_'.$cUF.'.json'; + if (!is_file($path)) { $this->refresh(); } - $cities = $this->filesystem->read($path); + $cities = file_get_contents($path); $arr = json_decode($cities, true); foreach ($arr as $r) { array_push($acities, $r); @@ -111,6 +232,7 @@ public function all() /** * Returns data in original json format, like received from IBGE + * * @return string */ public function get() @@ -118,32 +240,44 @@ public function get() if ($this->lastconsult == self::ESTADOS) { return $this->ufs; } else { + if (empty($this->cities)) { + throw new \Exception( + "Para obter a lista de cidades " + . "deve ser indicar uma UF" + ); + } return $this->cities; } } /** * Force reload data from IBGE into filesystem file + * + * @return $this */ public function refresh() { if ($this->lastconsult == self::ESTADOS) { $url = "https://servicodados.ibge.gov.br/api/v1/localidades//estados"; $this->ufs = Rest::send($url); - $this->filesystem->put('estados.json', $this->ufs); + file_put_contents($this->path . '/estados.json', $this->ufs); } else { if ($this->lastuf === 'all') { $ufs = json_decode($this->ufs); foreach ($ufs as $u) { $cUF = $u->id; - $url = "https://servicodados.ibge.gov.br/api/v1/localidades/estados/$cUF/municipios"; + $url = "https://servicodados.ibge.gov.br/api/v1" + . "/localidades/estados/$cUF/municipios"; $this->cities = Rest::send($url); - $this->filesystem->put('municipios_'.$cUF.'.json', $this->cities); + $name = '/municipios_' . $cUF . '.json'; + file_put_contents($this->path . $name, $this->cities); } } else { - $url = "https://servicodados.ibge.gov.br/api/v1/localidades/estados/$this->lastcuf/municipios"; + $url = "https://servicodados.ibge.gov.br/api/v1/localidades" + . "/estados/$this->lastcuf/municipios"; $this->cities = Rest::send($url); - $this->filesystem->put('municipios_'.$this->lastcuf.'.json', $this->cities); + $name = '/municipios_'.$this->lastcuf.'.json'; + file_put_contents($this->path . $name, $this->cities); } } return $this; @@ -151,6 +285,7 @@ public function refresh() /** * Returns data in stdClass format + * * @return \stdClass */ public function toStd() @@ -160,6 +295,7 @@ public function toStd() /** * Retruns data in array format + * * @return array */ public function toArray() @@ -169,7 +305,9 @@ public function toArray() /** * Return data as CSV format - * @param string $separador + * + * @param string $separador separador dos campos + * * @return string */ public function toCSV($separador = ',') @@ -179,16 +317,23 @@ public function toCSV($separador = ',') $csv = "ID,SIGLA,NOME,REGIAO_ID,REGIAO_SIGLA,REGIAO_NOME\n"; $ufs = json_decode($this->ufs); foreach ($ufs as $u) { + $nome = str_replace("'", ' ', $u->nome); $reg = $u->regiao; - $csv .= "$u->id,$u->sigla,$u->nome,$reg->id,$reg->sigla,$reg->nome\n"; + $csv .= "{$u->id}," + . "{$u->sigla}," + . "{$nome}," + . "{$reg->id}," + . "{$reg->sigla}," + . "{$reg->nome}\n"; } } else { if (!empty($this->cities)) { $cities = json_decode($this->cities); $csv = "ID,NOME,UF\n"; foreach ($cities as $c) { + $nome = str_replace("'", ' ', $c->nome); $uf = $c->microrregiao->mesorregiao->UF->sigla; - $csv .= "$c->id,$c->nome,$uf\n"; + $csv .= "{$c->id},{$nome},{$uf}\n"; } } } @@ -198,7 +343,9 @@ public function toCSV($separador = ',') /** * Return data as SQL format - * @param string $tablename + * + * @param string $tablename nome da tabela + * * @return string */ public function toSQL($tablename = '') @@ -209,8 +356,16 @@ public function toSQL($tablename = '') $ufs = json_decode($this->ufs); foreach ($ufs as $u) { $reg = $u->regiao; - $sql .= "INSERT INTO $name (id, sigla, nome, regiao_id, regiao_sigla, regiao_nome) VALUES ("; - $sql .= "$u->id,'$u->sigla','$u->nome','$reg->id', '$reg->sigla', '$reg->nome');\n"; + $nome = str_replace("'", ' ', $u->nome); + $sql .= "INSERT INTO $name (" + . "id, sigla, nome, regiao_id, regiao_sigla, regiao_nome" + . ") VALUES ("; + $sql .= "{$u->id}," + . "'{$u->sigla}'," + . "'{$nome}'," + . "'{$reg->id}'," + . "'{$reg->sigla}'," + . "'{$reg->nome}');\n"; } } else { $name = $name = empty($tablename) ? 'municipios' : $tablename; @@ -218,8 +373,9 @@ public function toSQL($tablename = '') $cities = json_decode($this->cities); foreach ($cities as $c) { $uf = $c->microrregiao->mesorregiao->UF->sigla; + $nome = str_replace("'", ' ', $c->nome); $sql .= "INSERT INTO $name (id, nome, uf) VALUES ("; - $sql .= "$c->id,'$c->nome', '$uf');\n"; + $sql .= "{$c->id},'{$nome}', '{$uf}');\n"; } } } diff --git a/src/Rest.php b/src/Rest.php old mode 100644 new mode 100755 index e375a4b..11e3808 --- a/src/Rest.php +++ b/src/Rest.php @@ -1,13 +1,38 @@ + * @copyright 2021 NFePHP Copyright (c) + * @license https://opensource.org/licenses/MIT MIT + * @link http://github.com/nfephp-org/sped-ibge + */ + namespace NFePHP\Ibge; +/** + * Class RestAPI client + * + * @category Library + * @package NFePHP\Ibge + * @author Roberto L. Machado + * @copyright 2021 NFePHP Copyright (c) + * @license https://opensource.org/licenses/MIT MIT + * @link http://github.com/nfephp-org/sped-ibge + */ class Rest { /** * Send request for IBGE REST service - * @param string $url + * + * @param string $url url do IBGE + * * @return string + * * @throws \Exception */ public static function send($url) diff --git a/storage/municipios_11.json b/storage/municipios_11.json old mode 100644 new mode 100755 diff --git a/storage/municipios_12.json b/storage/municipios_12.json old mode 100644 new mode 100755 diff --git a/storage/municipios_13.json b/storage/municipios_13.json old mode 100644 new mode 100755 diff --git a/storage/municipios_14.json b/storage/municipios_14.json old mode 100644 new mode 100755 diff --git a/storage/municipios_15.json b/storage/municipios_15.json old mode 100644 new mode 100755 diff --git a/storage/municipios_16.json b/storage/municipios_16.json old mode 100644 new mode 100755 index 8703237..261fcfe --- a/storage/municipios_16.json +++ b/storage/municipios_16.json @@ -1 +1 @@ -[{"id":1600055,"nome":"Serra do Navio","microrregiao":{"id":16003,"nome":"Macapá","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600105,"nome":"Amapá","microrregiao":{"id":16002,"nome":"Amapá","mesorregiao":{"id":1601,"nome":"Norte do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600154,"nome":"Pedra Branca do Amapari","microrregiao":{"id":16003,"nome":"Macapá","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600204,"nome":"Calçoene","microrregiao":{"id":16001,"nome":"Oiapoque","mesorregiao":{"id":1601,"nome":"Norte do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600212,"nome":"Cutias","microrregiao":{"id":16003,"nome":"Macapá","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600238,"nome":"Ferreira Gomes","microrregiao":{"id":16003,"nome":"Macapá","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600253,"nome":"Itaubal","microrregiao":{"id":16003,"nome":"Macapá","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600279,"nome":"Laranjal do Jari","microrregiao":{"id":16004,"nome":"Mazagão","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600303,"nome":"Macapá","microrregiao":{"id":16003,"nome":"Macapá","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600402,"nome":"Mazagão","microrregiao":{"id":16004,"nome":"Mazagão","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600501,"nome":"Oiapoque","microrregiao":{"id":16001,"nome":"Oiapoque","mesorregiao":{"id":1601,"nome":"Norte do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600535,"nome":"Porto Grande","microrregiao":{"id":16003,"nome":"Macapá","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600550,"nome":"Pracuúba","microrregiao":{"id":16002,"nome":"Amapá","mesorregiao":{"id":1601,"nome":"Norte do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600600,"nome":"Santana","microrregiao":{"id":16003,"nome":"Macapá","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600709,"nome":"Tartarugalzinho","microrregiao":{"id":16002,"nome":"Amapá","mesorregiao":{"id":1601,"nome":"Norte do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600808,"nome":"Vitória do Jari","microrregiao":{"id":16004,"nome":"Mazagão","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}}] \ No newline at end of file +[{"id":1600055,"nome":"Serra do Navio","microrregiao":{"id":16003,"nome":"Macapá","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}},"regiao-imediata":{"id":160004,"nome":"Porto Grande","regiao-intermediaria":{"id":1602,"nome":"Oiapoque - Porto Grande","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600105,"nome":"Amapá","microrregiao":{"id":16002,"nome":"Amapá","mesorregiao":{"id":1601,"nome":"Norte do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}},"regiao-imediata":{"id":160003,"nome":"Oiapoque","regiao-intermediaria":{"id":1602,"nome":"Oiapoque - Porto Grande","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600154,"nome":"Pedra Branca do Amapari","microrregiao":{"id":16003,"nome":"Macapá","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}},"regiao-imediata":{"id":160004,"nome":"Porto Grande","regiao-intermediaria":{"id":1602,"nome":"Oiapoque - Porto Grande","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600204,"nome":"Calçoene","microrregiao":{"id":16001,"nome":"Oiapoque","mesorregiao":{"id":1601,"nome":"Norte do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}},"regiao-imediata":{"id":160003,"nome":"Oiapoque","regiao-intermediaria":{"id":1602,"nome":"Oiapoque - Porto Grande","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600212,"nome":"Cutias","microrregiao":{"id":16003,"nome":"Macapá","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}},"regiao-imediata":{"id":160003,"nome":"Oiapoque","regiao-intermediaria":{"id":1602,"nome":"Oiapoque - Porto Grande","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600238,"nome":"Ferreira Gomes","microrregiao":{"id":16003,"nome":"Macapá","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}},"regiao-imediata":{"id":160004,"nome":"Porto Grande","regiao-intermediaria":{"id":1602,"nome":"Oiapoque - Porto Grande","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600253,"nome":"Itaubal","microrregiao":{"id":16003,"nome":"Macapá","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}},"regiao-imediata":{"id":160001,"nome":"Macapá","regiao-intermediaria":{"id":1601,"nome":"Macapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600279,"nome":"Laranjal do Jari","microrregiao":{"id":16004,"nome":"Mazagão","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}},"regiao-imediata":{"id":160002,"nome":"Laranjal do Jari","regiao-intermediaria":{"id":1601,"nome":"Macapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600303,"nome":"Macapá","microrregiao":{"id":16003,"nome":"Macapá","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}},"regiao-imediata":{"id":160001,"nome":"Macapá","regiao-intermediaria":{"id":1601,"nome":"Macapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600402,"nome":"Mazagão","microrregiao":{"id":16004,"nome":"Mazagão","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}},"regiao-imediata":{"id":160001,"nome":"Macapá","regiao-intermediaria":{"id":1601,"nome":"Macapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600501,"nome":"Oiapoque","microrregiao":{"id":16001,"nome":"Oiapoque","mesorregiao":{"id":1601,"nome":"Norte do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}},"regiao-imediata":{"id":160003,"nome":"Oiapoque","regiao-intermediaria":{"id":1602,"nome":"Oiapoque - Porto Grande","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600535,"nome":"Porto Grande","microrregiao":{"id":16003,"nome":"Macapá","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}},"regiao-imediata":{"id":160004,"nome":"Porto Grande","regiao-intermediaria":{"id":1602,"nome":"Oiapoque - Porto Grande","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600550,"nome":"Pracuúba","microrregiao":{"id":16002,"nome":"Amapá","mesorregiao":{"id":1601,"nome":"Norte do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}},"regiao-imediata":{"id":160003,"nome":"Oiapoque","regiao-intermediaria":{"id":1602,"nome":"Oiapoque - Porto Grande","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600600,"nome":"Santana","microrregiao":{"id":16003,"nome":"Macapá","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}},"regiao-imediata":{"id":160001,"nome":"Macapá","regiao-intermediaria":{"id":1601,"nome":"Macapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600709,"nome":"Tartarugalzinho","microrregiao":{"id":16002,"nome":"Amapá","mesorregiao":{"id":1601,"nome":"Norte do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}},"regiao-imediata":{"id":160003,"nome":"Oiapoque","regiao-intermediaria":{"id":1602,"nome":"Oiapoque - Porto Grande","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}},{"id":1600808,"nome":"Vitória do Jari","microrregiao":{"id":16004,"nome":"Mazagão","mesorregiao":{"id":1602,"nome":"Sul do Amapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}},"regiao-imediata":{"id":160002,"nome":"Laranjal do Jari","regiao-intermediaria":{"id":1601,"nome":"Macapá","UF":{"id":16,"sigla":"AP","nome":"Amapá","regiao":{"id":1,"sigla":"N","nome":"Norte"}}}}}] \ No newline at end of file diff --git a/storage/municipios_17.json b/storage/municipios_17.json old mode 100644 new mode 100755 diff --git a/storage/municipios_21.json b/storage/municipios_21.json old mode 100644 new mode 100755 diff --git a/storage/municipios_22.json b/storage/municipios_22.json old mode 100644 new mode 100755 diff --git a/storage/municipios_23.json b/storage/municipios_23.json old mode 100644 new mode 100755 diff --git a/storage/municipios_24.json b/storage/municipios_24.json old mode 100644 new mode 100755 diff --git a/storage/municipios_25.json b/storage/municipios_25.json old mode 100644 new mode 100755 diff --git a/storage/municipios_26.json b/storage/municipios_26.json old mode 100644 new mode 100755 diff --git a/storage/municipios_27.json b/storage/municipios_27.json old mode 100644 new mode 100755 diff --git a/storage/municipios_28.json b/storage/municipios_28.json old mode 100644 new mode 100755 diff --git a/storage/municipios_29.json b/storage/municipios_29.json old mode 100644 new mode 100755 diff --git a/storage/municipios_31.json b/storage/municipios_31.json old mode 100644 new mode 100755 diff --git a/storage/municipios_32.json b/storage/municipios_32.json old mode 100644 new mode 100755 diff --git a/storage/municipios_33.json b/storage/municipios_33.json old mode 100644 new mode 100755 diff --git a/storage/municipios_41.json b/storage/municipios_41.json old mode 100644 new mode 100755 diff --git a/storage/municipios_42.json b/storage/municipios_42.json old mode 100644 new mode 100755 diff --git a/storage/municipios_43.json b/storage/municipios_43.json old mode 100644 new mode 100755 diff --git a/storage/municipios_50.json b/storage/municipios_50.json old mode 100644 new mode 100755 diff --git a/storage/municipios_51.json b/storage/municipios_51.json old mode 100644 new mode 100755 diff --git a/storage/municipios_52.json b/storage/municipios_52.json old mode 100644 new mode 100755 diff --git a/storage/municipios_53.json b/storage/municipios_53.json old mode 100644 new mode 100755 diff --git a/storage/municipios_99.json b/storage/municipios_99.json deleted file mode 100644 index 0637a08..0000000 --- a/storage/municipios_99.json +++ /dev/null @@ -1 +0,0 @@ -[] \ No newline at end of file diff --git a/tests/IbgeTest.php b/tests/IbgeTest.php old mode 100644 new mode 100755 index 05aae76..4d5c88c --- a/tests/IbgeTest.php +++ b/tests/IbgeTest.php @@ -1,58 +1,174 @@ + * @copyright 2021 NFePHP Copyright (c) + * @license https://opensource.org/licenses/MIT MIT + * @link http://github.com/nfephp-org/sped-ibge + */ + namespace NFePHP\Ibge\Tests; use NFePHP\Ibge\Ibge; use PHPUnit\Framework\TestCase; +/** + * Unit Test Class + * + * @category Library + * @package NFePHP\Ibge + * @author Roberto L. Machado + * @copyright 2021 NFePHP Copyright (c) + * @license https://opensource.org/licenses/MIT MIT + * @link http://github.com/nfephp-org/sped-ibge + */ class IbgeTest extends TestCase { + /** + * Classe + * + * @var \NFePHP\Ibge\Ibge + */ public $ibge; - public function setUp() + /** + * Can instantiate class test + * + * @covers Ibge + * @covers ::__contruct + * + * @return void + */ + public function testCanInstantiate() { - $this->ibge = new Ibge(); + $ibge = new Ibge(); + $this->assertInstanceOf('NFePHP\Ibge\Ibge', $ibge); } - public function testShouldInstantiate() + /** + * Can instantiate static class test + * + * @covers Ibge + * @covers ::__contruct + * @covers ::uf + * + * @return void + */ + public function testCanInstantiateStatic() { - $this->assertInstanceOf(Ibge::class, $this->ibge); + $ibge = Ibge::uf(); + $this->assertInstanceOf('NFePHP\Ibge\Ibge', $ibge); } + - + /** + * Test Get estados + * + * @covers Ibge + * @covers ::estados + * @covers ::get + * + * @return void + */ public function testEstadosGet() { $expected = file_get_contents(__DIR__.'/fixtures/estados.json'); - $resp = $this->ibge->estados()->get(); + $resp = Ibge::uf()->estados()->get(); $this->assertEquals($expected, $resp); } + /** + * Test Get cidades pelo codigo da UF + * + * @covers Ibge + * @covers ::_loaduf + * @covers ::cidades + * @covers ::get + * + * @return void + */ public function testCidadesGetBycUF() { $expected = file_get_contents(__DIR__.'/fixtures/municipios_16.json'); - $resp = $this->ibge->cidades('16')->get(); + $resp = Ibge::uf()->cidades('16')->get(); $this->assertEquals($expected, $resp); } - + /** + * Test Get cidades pela sigla da UF + * + * @covers Ibge + * @covers ::_loaduf + * @covers ::cidades + * @covers ::get + * + * @return void + */ public function testCidadesGetByUF() { $expected = file_get_contents(__DIR__.'/fixtures/municipios_16.json'); - $resp = $this->ibge->cidades('AP')->get(); + $resp = Ibge::uf('AP')->cidades()->get(); $this->assertEquals($expected, $resp); } + /** + * Test toCSV cidades pelo codigo da UF + * + * @covers Ibge + * @covers ::_loaduf + * @covers ::cidades + * @covers ::get + * @covers ::toCSV + * + * @return void + */ + public function testCidadesToStdByUF() + { + $expected = json_decode( + file_get_contents(__DIR__.'/fixtures/municipios_16.json') + ); + $resp = Ibge::uf()->cidades('AP')->toStd(); + $this->assertEquals($expected, $resp); + } + + /** + * Test toSQL cidades pelo codigo da UF + * + * @covers Ibge + * @covers ::_loaduf + * @covers ::cidades + * @covers ::get + * @covers ::toSQL + * + * @return void + */ public function testCidadesToSQLByUF() { $expected = file_get_contents(__DIR__.'/fixtures/municipios_16.sql'); - $resp = $this->ibge->cidades('AP')->toSQL(); + $resp = Ibge::uf()->cidades('AP')->toSQL(); $this->assertEquals($expected, $resp); } + /** + * Test toCSV cidades pelo codigo da UF + * + * @covers Ibge + * @covers ::_loaduf + * @covers ::cidades + * @covers ::get + * @covers ::toCSV + * + * @return void + */ public function testCidadesToCSVByUF() { $expected = file_get_contents(__DIR__.'/fixtures/municipios_16.csv'); - $resp = $this->ibge->cidades('AP')->toCSV(); + $resp = Ibge::uf()->cidades('AP')->toCSV(); $this->assertEquals($expected, $resp); } diff --git a/tests/fixtures/municipios_16.csv b/tests/fixtures/municipios_16.csv old mode 100644 new mode 100755 diff --git a/tests/fixtures/municipios_16.sql b/tests/fixtures/municipios_16.sql old mode 100644 new mode 100755