├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── composer.lock └── src ├── ApiConsumer.php ├── BankApi.php ├── CepApi.php ├── CnpjApi.php ├── CptecApi.php ├── Data ├── BankApi │ └── BankData.php ├── CepApi │ ├── CepData.php │ ├── CoordinatesData.php │ └── LocationData.php ├── CnpjApi │ ├── CnaeData.php │ ├── CnpjData.php │ └── QsaData.php ├── CptecApi │ ├── CityData.php │ ├── OceanicForecastData.php │ ├── OceanicForecastDayData.php │ ├── OceanicForecastWaveData.php │ ├── WeatherData.php │ ├── WeatherForecastData.php │ └── WeatherForecastDayData.php ├── DataTransfer.php ├── DddApi │ └── DddData.php ├── FipeApi │ ├── BrandData.php │ ├── PriceData.php │ └── RefTableData.php ├── HolidayApi │ └── HolidayData.php ├── IbgeApi │ ├── CityData.php │ ├── RegionData.php │ └── UfData.php ├── IsbnApi │ ├── DimensionData.php │ ├── IsbnData.php │ └── RetailPriceData.php ├── NcmApi │ └── NcmData.php ├── PixApi │ └── ParticipantData.php ├── RegistroBrApi │ └── DomainData.php └── TaxApi │ └── TaxData.php ├── DddApi.php ├── FipeApi.php ├── HolidayApi.php ├── IbgeApi.php ├── IsbnApi.php ├── NcmApi.php ├── PixApi.php ├── RegistroBrApi.php ├── TaxApi.php ├── Traits └── ConvertsSnakeKeysToCamel.php └── Utils └── Str.php /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea/ 2 | /vendor/ 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Corviz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BrasilApi PHP 2 | 3 | A PHP SDK for BrasilAPI project (PHP 8.1+) 4 | 5 | ## Install with composer 6 | 7 | ``` 8 | composer require corviz/brasilapi-php 9 | ``` 10 | 11 | ## How to use 12 | 13 | * Every api segment (or sub-path) is represented by a class in this SDK. Eg: 14 | * `/banks/` -> `BanksApi` 15 | * `/cep/` -> `CepApi` 16 | * `/cnpj/` -> `CnpjApi` 17 | * And so on... 18 | * Each api response is stored into a data transfer object (`BankData`, `CnpjData`, etc...) 19 | * Every unsuccessful request will throw an `GuzzleException` 20 | * All indexes have the same name as documented in the [official api docs](https://brasilapi.com.br/docs). 21 | * Indexes that are composed by two or more words, are formatted as **lowerSnakeCase** 22 | 23 | ## Environment variables 24 | 25 | `BRASILAPI_TIMEOUT` - maximum request time in seconds 26 | 27 | `BRASILAPI_PROXY` - Proxy configuration for guzzle (ip) 28 | 29 | ## Usage examples 30 | 31 | ### Example 1. Listing all banks: 32 | 33 | ```php 34 | use Corviz\BrasilAPI\BankApi; 35 | 36 | $banks = BankApi::all(); 37 | 38 | foreach ($banks as $bank) { 39 | echo $bank->code, ' - ', $bank->name; 40 | } 41 | ``` 42 | 43 | ### Example 2. Show address data by CEP (ZIP code) 44 | 45 | ```php 46 | use Corviz\BrasilAPI\CepApi; 47 | 48 | $address = CepApi::get('13087901'); 49 | 50 | echo $address->street; //Avenida Guilherme Campos 51 | echo $address->neighborhood; //Jardim Santa Genebra 52 | echo $address->city; //Campinas 53 | 54 | //and so on... 55 | ``` 56 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "corviz/brasilapi-php", 3 | "description": "A PHP SDK for BrasilAPI", 4 | "type": "library", 5 | "license": "MIT", 6 | "autoload": { 7 | "psr-4": { 8 | "Corviz\\BrasilAPI\\": "src/" 9 | } 10 | }, 11 | "require": { 12 | "php": ">=8.1", 13 | "guzzlehttp/guzzle": "^7.0" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "4f5dbaa746bc3872ac30c8696e55005f", 8 | "packages": [ 9 | { 10 | "name": "guzzlehttp/guzzle", 11 | "version": "7.5.0", 12 | "source": { 13 | "type": "git", 14 | "url": "https://github.com/guzzle/guzzle.git", 15 | "reference": "b50a2a1251152e43f6a37f0fa053e730a67d25ba" 16 | }, 17 | "dist": { 18 | "type": "zip", 19 | "url": "https://api.github.com/repos/guzzle/guzzle/zipball/b50a2a1251152e43f6a37f0fa053e730a67d25ba", 20 | "reference": "b50a2a1251152e43f6a37f0fa053e730a67d25ba", 21 | "shasum": "" 22 | }, 23 | "require": { 24 | "ext-json": "*", 25 | "guzzlehttp/promises": "^1.5", 26 | "guzzlehttp/psr7": "^1.9 || ^2.4", 27 | "php": "^7.2.5 || ^8.0", 28 | "psr/http-client": "^1.0", 29 | "symfony/deprecation-contracts": "^2.2 || ^3.0" 30 | }, 31 | "provide": { 32 | "psr/http-client-implementation": "1.0" 33 | }, 34 | "require-dev": { 35 | "bamarni/composer-bin-plugin": "^1.8.1", 36 | "ext-curl": "*", 37 | "php-http/client-integration-tests": "^3.0", 38 | "phpunit/phpunit": "^8.5.29 || ^9.5.23", 39 | "psr/log": "^1.1 || ^2.0 || ^3.0" 40 | }, 41 | "suggest": { 42 | "ext-curl": "Required for CURL handler support", 43 | "ext-intl": "Required for Internationalized Domain Name (IDN) support", 44 | "psr/log": "Required for using the Log middleware" 45 | }, 46 | "type": "library", 47 | "extra": { 48 | "bamarni-bin": { 49 | "bin-links": true, 50 | "forward-command": false 51 | }, 52 | "branch-alias": { 53 | "dev-master": "7.5-dev" 54 | } 55 | }, 56 | "autoload": { 57 | "files": [ 58 | "src/functions_include.php" 59 | ], 60 | "psr-4": { 61 | "GuzzleHttp\\": "src/" 62 | } 63 | }, 64 | "notification-url": "https://packagist.org/downloads/", 65 | "license": [ 66 | "MIT" 67 | ], 68 | "authors": [ 69 | { 70 | "name": "Graham Campbell", 71 | "email": "hello@gjcampbell.co.uk", 72 | "homepage": "https://github.com/GrahamCampbell" 73 | }, 74 | { 75 | "name": "Michael Dowling", 76 | "email": "mtdowling@gmail.com", 77 | "homepage": "https://github.com/mtdowling" 78 | }, 79 | { 80 | "name": "Jeremy Lindblom", 81 | "email": "jeremeamia@gmail.com", 82 | "homepage": "https://github.com/jeremeamia" 83 | }, 84 | { 85 | "name": "George Mponos", 86 | "email": "gmponos@gmail.com", 87 | "homepage": "https://github.com/gmponos" 88 | }, 89 | { 90 | "name": "Tobias Nyholm", 91 | "email": "tobias.nyholm@gmail.com", 92 | "homepage": "https://github.com/Nyholm" 93 | }, 94 | { 95 | "name": "Márk Sági-Kazár", 96 | "email": "mark.sagikazar@gmail.com", 97 | "homepage": "https://github.com/sagikazarmark" 98 | }, 99 | { 100 | "name": "Tobias Schultze", 101 | "email": "webmaster@tubo-world.de", 102 | "homepage": "https://github.com/Tobion" 103 | } 104 | ], 105 | "description": "Guzzle is a PHP HTTP client library", 106 | "keywords": [ 107 | "client", 108 | "curl", 109 | "framework", 110 | "http", 111 | "http client", 112 | "psr-18", 113 | "psr-7", 114 | "rest", 115 | "web service" 116 | ], 117 | "support": { 118 | "issues": "https://github.com/guzzle/guzzle/issues", 119 | "source": "https://github.com/guzzle/guzzle/tree/7.5.0" 120 | }, 121 | "funding": [ 122 | { 123 | "url": "https://github.com/GrahamCampbell", 124 | "type": "github" 125 | }, 126 | { 127 | "url": "https://github.com/Nyholm", 128 | "type": "github" 129 | }, 130 | { 131 | "url": "https://tidelift.com/funding/github/packagist/guzzlehttp/guzzle", 132 | "type": "tidelift" 133 | } 134 | ], 135 | "time": "2022-08-28T15:39:27+00:00" 136 | }, 137 | { 138 | "name": "guzzlehttp/promises", 139 | "version": "1.5.2", 140 | "source": { 141 | "type": "git", 142 | "url": "https://github.com/guzzle/promises.git", 143 | "reference": "b94b2807d85443f9719887892882d0329d1e2598" 144 | }, 145 | "dist": { 146 | "type": "zip", 147 | "url": "https://api.github.com/repos/guzzle/promises/zipball/b94b2807d85443f9719887892882d0329d1e2598", 148 | "reference": "b94b2807d85443f9719887892882d0329d1e2598", 149 | "shasum": "" 150 | }, 151 | "require": { 152 | "php": ">=5.5" 153 | }, 154 | "require-dev": { 155 | "symfony/phpunit-bridge": "^4.4 || ^5.1" 156 | }, 157 | "type": "library", 158 | "extra": { 159 | "branch-alias": { 160 | "dev-master": "1.5-dev" 161 | } 162 | }, 163 | "autoload": { 164 | "files": [ 165 | "src/functions_include.php" 166 | ], 167 | "psr-4": { 168 | "GuzzleHttp\\Promise\\": "src/" 169 | } 170 | }, 171 | "notification-url": "https://packagist.org/downloads/", 172 | "license": [ 173 | "MIT" 174 | ], 175 | "authors": [ 176 | { 177 | "name": "Graham Campbell", 178 | "email": "hello@gjcampbell.co.uk", 179 | "homepage": "https://github.com/GrahamCampbell" 180 | }, 181 | { 182 | "name": "Michael Dowling", 183 | "email": "mtdowling@gmail.com", 184 | "homepage": "https://github.com/mtdowling" 185 | }, 186 | { 187 | "name": "Tobias Nyholm", 188 | "email": "tobias.nyholm@gmail.com", 189 | "homepage": "https://github.com/Nyholm" 190 | }, 191 | { 192 | "name": "Tobias Schultze", 193 | "email": "webmaster@tubo-world.de", 194 | "homepage": "https://github.com/Tobion" 195 | } 196 | ], 197 | "description": "Guzzle promises library", 198 | "keywords": [ 199 | "promise" 200 | ], 201 | "support": { 202 | "issues": "https://github.com/guzzle/promises/issues", 203 | "source": "https://github.com/guzzle/promises/tree/1.5.2" 204 | }, 205 | "funding": [ 206 | { 207 | "url": "https://github.com/GrahamCampbell", 208 | "type": "github" 209 | }, 210 | { 211 | "url": "https://github.com/Nyholm", 212 | "type": "github" 213 | }, 214 | { 215 | "url": "https://tidelift.com/funding/github/packagist/guzzlehttp/promises", 216 | "type": "tidelift" 217 | } 218 | ], 219 | "time": "2022-08-28T14:55:35+00:00" 220 | }, 221 | { 222 | "name": "guzzlehttp/psr7", 223 | "version": "2.4.3", 224 | "source": { 225 | "type": "git", 226 | "url": "https://github.com/guzzle/psr7.git", 227 | "reference": "67c26b443f348a51926030c83481b85718457d3d" 228 | }, 229 | "dist": { 230 | "type": "zip", 231 | "url": "https://api.github.com/repos/guzzle/psr7/zipball/67c26b443f348a51926030c83481b85718457d3d", 232 | "reference": "67c26b443f348a51926030c83481b85718457d3d", 233 | "shasum": "" 234 | }, 235 | "require": { 236 | "php": "^7.2.5 || ^8.0", 237 | "psr/http-factory": "^1.0", 238 | "psr/http-message": "^1.0", 239 | "ralouphie/getallheaders": "^3.0" 240 | }, 241 | "provide": { 242 | "psr/http-factory-implementation": "1.0", 243 | "psr/http-message-implementation": "1.0" 244 | }, 245 | "require-dev": { 246 | "bamarni/composer-bin-plugin": "^1.8.1", 247 | "http-interop/http-factory-tests": "^0.9", 248 | "phpunit/phpunit": "^8.5.29 || ^9.5.23" 249 | }, 250 | "suggest": { 251 | "laminas/laminas-httphandlerrunner": "Emit PSR-7 responses" 252 | }, 253 | "type": "library", 254 | "extra": { 255 | "bamarni-bin": { 256 | "bin-links": true, 257 | "forward-command": false 258 | }, 259 | "branch-alias": { 260 | "dev-master": "2.4-dev" 261 | } 262 | }, 263 | "autoload": { 264 | "psr-4": { 265 | "GuzzleHttp\\Psr7\\": "src/" 266 | } 267 | }, 268 | "notification-url": "https://packagist.org/downloads/", 269 | "license": [ 270 | "MIT" 271 | ], 272 | "authors": [ 273 | { 274 | "name": "Graham Campbell", 275 | "email": "hello@gjcampbell.co.uk", 276 | "homepage": "https://github.com/GrahamCampbell" 277 | }, 278 | { 279 | "name": "Michael Dowling", 280 | "email": "mtdowling@gmail.com", 281 | "homepage": "https://github.com/mtdowling" 282 | }, 283 | { 284 | "name": "George Mponos", 285 | "email": "gmponos@gmail.com", 286 | "homepage": "https://github.com/gmponos" 287 | }, 288 | { 289 | "name": "Tobias Nyholm", 290 | "email": "tobias.nyholm@gmail.com", 291 | "homepage": "https://github.com/Nyholm" 292 | }, 293 | { 294 | "name": "Márk Sági-Kazár", 295 | "email": "mark.sagikazar@gmail.com", 296 | "homepage": "https://github.com/sagikazarmark" 297 | }, 298 | { 299 | "name": "Tobias Schultze", 300 | "email": "webmaster@tubo-world.de", 301 | "homepage": "https://github.com/Tobion" 302 | }, 303 | { 304 | "name": "Márk Sági-Kazár", 305 | "email": "mark.sagikazar@gmail.com", 306 | "homepage": "https://sagikazarmark.hu" 307 | } 308 | ], 309 | "description": "PSR-7 message implementation that also provides common utility methods", 310 | "keywords": [ 311 | "http", 312 | "message", 313 | "psr-7", 314 | "request", 315 | "response", 316 | "stream", 317 | "uri", 318 | "url" 319 | ], 320 | "support": { 321 | "issues": "https://github.com/guzzle/psr7/issues", 322 | "source": "https://github.com/guzzle/psr7/tree/2.4.3" 323 | }, 324 | "funding": [ 325 | { 326 | "url": "https://github.com/GrahamCampbell", 327 | "type": "github" 328 | }, 329 | { 330 | "url": "https://github.com/Nyholm", 331 | "type": "github" 332 | }, 333 | { 334 | "url": "https://tidelift.com/funding/github/packagist/guzzlehttp/psr7", 335 | "type": "tidelift" 336 | } 337 | ], 338 | "time": "2022-10-26T14:07:24+00:00" 339 | }, 340 | { 341 | "name": "psr/http-client", 342 | "version": "1.0.1", 343 | "source": { 344 | "type": "git", 345 | "url": "https://github.com/php-fig/http-client.git", 346 | "reference": "2dfb5f6c5eff0e91e20e913f8c5452ed95b86621" 347 | }, 348 | "dist": { 349 | "type": "zip", 350 | "url": "https://api.github.com/repos/php-fig/http-client/zipball/2dfb5f6c5eff0e91e20e913f8c5452ed95b86621", 351 | "reference": "2dfb5f6c5eff0e91e20e913f8c5452ed95b86621", 352 | "shasum": "" 353 | }, 354 | "require": { 355 | "php": "^7.0 || ^8.0", 356 | "psr/http-message": "^1.0" 357 | }, 358 | "type": "library", 359 | "extra": { 360 | "branch-alias": { 361 | "dev-master": "1.0.x-dev" 362 | } 363 | }, 364 | "autoload": { 365 | "psr-4": { 366 | "Psr\\Http\\Client\\": "src/" 367 | } 368 | }, 369 | "notification-url": "https://packagist.org/downloads/", 370 | "license": [ 371 | "MIT" 372 | ], 373 | "authors": [ 374 | { 375 | "name": "PHP-FIG", 376 | "homepage": "http://www.php-fig.org/" 377 | } 378 | ], 379 | "description": "Common interface for HTTP clients", 380 | "homepage": "https://github.com/php-fig/http-client", 381 | "keywords": [ 382 | "http", 383 | "http-client", 384 | "psr", 385 | "psr-18" 386 | ], 387 | "support": { 388 | "source": "https://github.com/php-fig/http-client/tree/master" 389 | }, 390 | "time": "2020-06-29T06:28:15+00:00" 391 | }, 392 | { 393 | "name": "psr/http-factory", 394 | "version": "1.0.1", 395 | "source": { 396 | "type": "git", 397 | "url": "https://github.com/php-fig/http-factory.git", 398 | "reference": "12ac7fcd07e5b077433f5f2bee95b3a771bf61be" 399 | }, 400 | "dist": { 401 | "type": "zip", 402 | "url": "https://api.github.com/repos/php-fig/http-factory/zipball/12ac7fcd07e5b077433f5f2bee95b3a771bf61be", 403 | "reference": "12ac7fcd07e5b077433f5f2bee95b3a771bf61be", 404 | "shasum": "" 405 | }, 406 | "require": { 407 | "php": ">=7.0.0", 408 | "psr/http-message": "^1.0" 409 | }, 410 | "type": "library", 411 | "extra": { 412 | "branch-alias": { 413 | "dev-master": "1.0.x-dev" 414 | } 415 | }, 416 | "autoload": { 417 | "psr-4": { 418 | "Psr\\Http\\Message\\": "src/" 419 | } 420 | }, 421 | "notification-url": "https://packagist.org/downloads/", 422 | "license": [ 423 | "MIT" 424 | ], 425 | "authors": [ 426 | { 427 | "name": "PHP-FIG", 428 | "homepage": "http://www.php-fig.org/" 429 | } 430 | ], 431 | "description": "Common interfaces for PSR-7 HTTP message factories", 432 | "keywords": [ 433 | "factory", 434 | "http", 435 | "message", 436 | "psr", 437 | "psr-17", 438 | "psr-7", 439 | "request", 440 | "response" 441 | ], 442 | "support": { 443 | "source": "https://github.com/php-fig/http-factory/tree/master" 444 | }, 445 | "time": "2019-04-30T12:38:16+00:00" 446 | }, 447 | { 448 | "name": "psr/http-message", 449 | "version": "1.0.1", 450 | "source": { 451 | "type": "git", 452 | "url": "https://github.com/php-fig/http-message.git", 453 | "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" 454 | }, 455 | "dist": { 456 | "type": "zip", 457 | "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", 458 | "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", 459 | "shasum": "" 460 | }, 461 | "require": { 462 | "php": ">=5.3.0" 463 | }, 464 | "type": "library", 465 | "extra": { 466 | "branch-alias": { 467 | "dev-master": "1.0.x-dev" 468 | } 469 | }, 470 | "autoload": { 471 | "psr-4": { 472 | "Psr\\Http\\Message\\": "src/" 473 | } 474 | }, 475 | "notification-url": "https://packagist.org/downloads/", 476 | "license": [ 477 | "MIT" 478 | ], 479 | "authors": [ 480 | { 481 | "name": "PHP-FIG", 482 | "homepage": "http://www.php-fig.org/" 483 | } 484 | ], 485 | "description": "Common interface for HTTP messages", 486 | "homepage": "https://github.com/php-fig/http-message", 487 | "keywords": [ 488 | "http", 489 | "http-message", 490 | "psr", 491 | "psr-7", 492 | "request", 493 | "response" 494 | ], 495 | "support": { 496 | "source": "https://github.com/php-fig/http-message/tree/master" 497 | }, 498 | "time": "2016-08-06T14:39:51+00:00" 499 | }, 500 | { 501 | "name": "ralouphie/getallheaders", 502 | "version": "3.0.3", 503 | "source": { 504 | "type": "git", 505 | "url": "https://github.com/ralouphie/getallheaders.git", 506 | "reference": "120b605dfeb996808c31b6477290a714d356e822" 507 | }, 508 | "dist": { 509 | "type": "zip", 510 | "url": "https://api.github.com/repos/ralouphie/getallheaders/zipball/120b605dfeb996808c31b6477290a714d356e822", 511 | "reference": "120b605dfeb996808c31b6477290a714d356e822", 512 | "shasum": "" 513 | }, 514 | "require": { 515 | "php": ">=5.6" 516 | }, 517 | "require-dev": { 518 | "php-coveralls/php-coveralls": "^2.1", 519 | "phpunit/phpunit": "^5 || ^6.5" 520 | }, 521 | "type": "library", 522 | "autoload": { 523 | "files": [ 524 | "src/getallheaders.php" 525 | ] 526 | }, 527 | "notification-url": "https://packagist.org/downloads/", 528 | "license": [ 529 | "MIT" 530 | ], 531 | "authors": [ 532 | { 533 | "name": "Ralph Khattar", 534 | "email": "ralph.khattar@gmail.com" 535 | } 536 | ], 537 | "description": "A polyfill for getallheaders.", 538 | "support": { 539 | "issues": "https://github.com/ralouphie/getallheaders/issues", 540 | "source": "https://github.com/ralouphie/getallheaders/tree/develop" 541 | }, 542 | "time": "2019-03-08T08:55:37+00:00" 543 | }, 544 | { 545 | "name": "symfony/deprecation-contracts", 546 | "version": "v3.2.0", 547 | "source": { 548 | "type": "git", 549 | "url": "https://github.com/symfony/deprecation-contracts.git", 550 | "reference": "1ee04c65529dea5d8744774d474e7cbd2f1206d3" 551 | }, 552 | "dist": { 553 | "type": "zip", 554 | "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/1ee04c65529dea5d8744774d474e7cbd2f1206d3", 555 | "reference": "1ee04c65529dea5d8744774d474e7cbd2f1206d3", 556 | "shasum": "" 557 | }, 558 | "require": { 559 | "php": ">=8.1" 560 | }, 561 | "type": "library", 562 | "extra": { 563 | "branch-alias": { 564 | "dev-main": "3.3-dev" 565 | }, 566 | "thanks": { 567 | "name": "symfony/contracts", 568 | "url": "https://github.com/symfony/contracts" 569 | } 570 | }, 571 | "autoload": { 572 | "files": [ 573 | "function.php" 574 | ] 575 | }, 576 | "notification-url": "https://packagist.org/downloads/", 577 | "license": [ 578 | "MIT" 579 | ], 580 | "authors": [ 581 | { 582 | "name": "Nicolas Grekas", 583 | "email": "p@tchwork.com" 584 | }, 585 | { 586 | "name": "Symfony Community", 587 | "homepage": "https://symfony.com/contributors" 588 | } 589 | ], 590 | "description": "A generic function and convention to trigger deprecation notices", 591 | "homepage": "https://symfony.com", 592 | "support": { 593 | "source": "https://github.com/symfony/deprecation-contracts/tree/v3.2.0" 594 | }, 595 | "funding": [ 596 | { 597 | "url": "https://symfony.com/sponsor", 598 | "type": "custom" 599 | }, 600 | { 601 | "url": "https://github.com/fabpot", 602 | "type": "github" 603 | }, 604 | { 605 | "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", 606 | "type": "tidelift" 607 | } 608 | ], 609 | "time": "2022-11-25T10:21:52+00:00" 610 | } 611 | ], 612 | "packages-dev": [], 613 | "aliases": [], 614 | "minimum-stability": "stable", 615 | "stability-flags": [], 616 | "prefer-stable": false, 617 | "prefer-lowest": false, 618 | "platform": { 619 | "php": ">=8.1" 620 | }, 621 | "platform-dev": [], 622 | "plugin-api-version": "2.2.0" 623 | } 624 | -------------------------------------------------------------------------------- /src/ApiConsumer.php: -------------------------------------------------------------------------------- 1 | 'https://brasilapi.com.br/api/', 23 | 'timeout' => $_ENV['BRASILAPI_TIMEOUT'] ?? 0, 24 | 'allow_redirects' => true, 25 | ]; 26 | 27 | if ($proxy = ($_ENV['BRASILAPI_PROXY'] ?? null)) { 28 | $options['proxy'] = $proxy; 29 | } 30 | self::$client = new Client($options); 31 | } 32 | 33 | return self::$client; 34 | } 35 | 36 | /** 37 | * @param ResponseInterface $response 38 | * @return array|null 39 | */ 40 | protected static function parseJson(ResponseInterface $response): ?array 41 | { 42 | $data = null; 43 | $status = $response->getStatusCode(); 44 | 45 | if ($status >= 200 && $status < 300) { 46 | $data = json_decode($response->getBody()->getContents(), true); 47 | } 48 | 49 | return $data; 50 | } 51 | } -------------------------------------------------------------------------------- /src/BankApi.php: -------------------------------------------------------------------------------- 1 | get("banks/v1"); 19 | $data = []; 20 | 21 | if ($responseData = self::parseJson($response)) { 22 | foreach ($responseData as $bank) { 23 | $data[] = BankData::from($bank); 24 | } 25 | } 26 | 27 | return $data; 28 | } 29 | 30 | /** 31 | * @param int $code 32 | * @return BankData|null 33 | * @throws GuzzleException 34 | * @throws ReflectionException 35 | */ 36 | public static function get(int $code): ?BankData 37 | { 38 | $response = static::getClient()->get("banks/v1/$code"); 39 | $data = null; 40 | 41 | if ($responseData = self::parseJson($response)) { 42 | $data = BankData::from($responseData); 43 | } 44 | 45 | return $data; 46 | } 47 | } -------------------------------------------------------------------------------- /src/CepApi.php: -------------------------------------------------------------------------------- 1 | get("cep/$version/$number"); 27 | $data = null; 28 | 29 | if ($responseData = self::parseJson($response)) { 30 | if (!empty($responseData['location'])) { 31 | $responseData['location']['coordinates'] = CoordinatesData::from($responseData['location']['coordinates']); 32 | $responseData['location'] = LocationData::from($responseData['location']); 33 | } 34 | 35 | $data = CepData::from($responseData); 36 | } 37 | 38 | return $data; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/CnpjApi.php: -------------------------------------------------------------------------------- 1 | get("cnpj/v1/$cnpj"); 36 | $data = null; 37 | 38 | if ($responseData = self::parseJson($response)) { 39 | $responseData = self::snakeToCamelKeys($responseData); 40 | 41 | if (!empty($responseData['cnaesSecundarios'])) { 42 | foreach ($responseData['cnaesSecundarios'] as &$item) { 43 | $item = CnaeData::from($item); 44 | } 45 | } 46 | 47 | if (!empty($responseData['qsa'])) { 48 | foreach ($responseData['qsa'] as &$item) { 49 | !empty($item['dataEntradaSociedade']) 50 | && $item['dataEntradaSociedade'] = DateTimeImmutable::createFromFormat( 51 | "Y-m-d P", $item['dataEntradaSociedade'].' -03:00' 52 | ); 53 | 54 | $item = QsaData::from($item); 55 | } 56 | } 57 | 58 | foreach (self::DATE_FIELDS as $field) { 59 | !empty($responseData[$field]) 60 | && $responseData[$field] = DateTimeImmutable::createFromFormat( 61 | "Y-m-d P", $responseData[$field].' -03:00' 62 | ); 63 | } 64 | 65 | $responseData['opcaoPeloSimples'] = (bool) $responseData['opcaoPeloSimples']; 66 | $responseData['opcaoPeloMei'] = (bool) $responseData['opcaoPeloMei']; 67 | 68 | $data = CnpjData::from($responseData); 69 | } 70 | 71 | return $data; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/CptecApi.php: -------------------------------------------------------------------------------- 1 | get($uri); 35 | $data = []; 36 | 37 | if ($responseData = self::parseJson($response)) { 38 | foreach ($responseData as $city) { 39 | $data[] = CityData::from($city); 40 | } 41 | } 42 | 43 | return $data; 44 | } 45 | 46 | /** 47 | * @return WeatherData[] 48 | * @throws GuzzleException 49 | * @throws ReflectionException 50 | */ 51 | public static function getWeatherAtCapitalCities(): array 52 | { 53 | $response = static::getClient()->get('cptec/v1/clima/capital'); 54 | $data = []; 55 | 56 | if ($responseData = self::parseJson($response)) { 57 | foreach ($responseData as $weather) { 58 | $data[] = self::prepareWeatherData($weather); 59 | } 60 | } 61 | 62 | return $data; 63 | } 64 | 65 | /** 66 | * @param string $icaoCode 67 | * @return WeatherData|null 68 | * @throws GuzzleException 69 | * @throws ReflectionException 70 | */ 71 | public static function getWeatherAtAirport(string $icaoCode): ?WeatherData 72 | { 73 | $response = static::getClient()->get("cptec/v1/clima/aeroporto/$icaoCode"); 74 | $data = null; 75 | 76 | if ($responseData = self::parseJson($response)) { 77 | $data = self::prepareWeatherData($responseData); 78 | } 79 | 80 | return $data; 81 | } 82 | 83 | /** 84 | * @param int $cityCode 85 | * @param int $days 1-6 days 86 | * @return WeatherForecastData|null 87 | * @throws GuzzleException 88 | * @throws ReflectionException 89 | */ 90 | public static function getWeatherForecastForCity(int $cityCode, int $days = 1): ?WeatherForecastData 91 | { 92 | $response = static::getClient()->get("cptec/v1/clima/previsao/$cityCode/$days"); 93 | $data = null; 94 | 95 | if ($responseData = self::parseJson($response)) { 96 | $data = self::prepareWeatherForecastData($responseData); 97 | } 98 | 99 | return $data; 100 | } 101 | 102 | /** 103 | * @param int $cityCode 104 | * @param int $days 105 | * @return OceanicForecastData|null 106 | * @throws GuzzleException 107 | */ 108 | public static function getOceanicForecastForCity(int $cityCode, int $days = 1): ?OceanicForecastData 109 | { 110 | $response = static::getClient()->get("cptec/v1/ondas/$cityCode/$days"); 111 | $data = null; 112 | 113 | if ($responseData = self::parseJson($response)) { 114 | $data = self::prepareOceanicForecastData($responseData); 115 | } 116 | 117 | return $data; 118 | } 119 | 120 | /** 121 | * @param array $weather 122 | * @return WeatherData 123 | * @throws ReflectionException 124 | */ 125 | private static function prepareWeatherData(array $weather): WeatherData 126 | { 127 | $weather = self::snakeToCamelKeys($weather); 128 | $weather['atualizadoEm'] = DateTimeImmutable::createFromFormat( 129 | DateTimeImmutable::RFC3339_EXTENDED, $weather['atualizadoEm'] 130 | ); 131 | 132 | return WeatherData::from($weather); 133 | } 134 | 135 | /** 136 | * @param array $weather 137 | * @return WeatherForecastData 138 | * @throws ReflectionException 139 | */ 140 | private static function prepareWeatherForecastData(array $weather): WeatherForecastData 141 | { 142 | $weather = self::snakeToCamelKeys($weather); 143 | $weather['atualizadoEm'] = DateTimeImmutable::createFromFormat( 144 | 'Y-m-d H:i:s P', "{$weather['atualizadoEm']} 00:00:00 -03:00" 145 | ); 146 | 147 | $clima = []; 148 | foreach ($weather['clima'] as $c) { 149 | $c['data'] = DateTimeImmutable::createFromFormat( 150 | 'Y-m-d H:i:s P', "{$c['data']} 00:00:00 -03:00" 151 | ); 152 | $clima[] = WeatherForecastDayData::from($c); 153 | } 154 | $weather['clima'] = $clima; 155 | 156 | return WeatherForecastData::from($weather); 157 | } 158 | 159 | private static function prepareOceanicForecastData(array $data): OceanicForecastData 160 | { 161 | $data = self::snakeToCamelKeys($data); 162 | $data['atualizadoEm'] = DateTimeImmutable::createFromFormat( 163 | 'Y-m-d H:i:s P', "{$data['atualizadoEm']} 00:00:00 -03:00" 164 | ); 165 | 166 | foreach ($data['ondas'] as &$onda) { 167 | $onda['data'] = DateTimeImmutable::createFromFormat( 168 | 'Y-m-d H:i:s P', "{$onda['data']} 00:00:00 -03:00" 169 | ); 170 | 171 | foreach ($onda['dadosOndas'] as &$dadosOnda) { 172 | try { 173 | $dadosOnda = OceanicForecastWaveData::from($dadosOnda); 174 | } catch (\Exception $e) { 175 | print_r($dadosOnda); 176 | } 177 | } 178 | 179 | $onda = OceanicForecastDayData::from($onda); 180 | } 181 | 182 | return OceanicForecastData::from($data); 183 | } 184 | } 185 | -------------------------------------------------------------------------------- /src/Data/BankApi/BankData.php: -------------------------------------------------------------------------------- 1 | getProperties() as $attribute) { 24 | $attrNames[] = $attribute->getName(); 25 | } 26 | 27 | $constructorArgs = array_intersect_key($data, array_flip($attrNames)); 28 | return $r->newInstanceArgs($constructorArgs); 29 | } 30 | 31 | /** 32 | * @return array 33 | */ 34 | public function toArray(): array 35 | { 36 | $arr = (array) $this; 37 | 38 | foreach ($arr as &$item) { 39 | if ($item instanceof DataTransfer) { 40 | $item = $item->toArray(); 41 | } 42 | } 43 | 44 | return $arr; 45 | } 46 | 47 | /** 48 | * @return string|false 49 | */ 50 | public function toJson(): string|false 51 | { 52 | return json_encode($this); 53 | } 54 | 55 | /** 56 | * Whether a offset exists 57 | * @link https://php.net/manual/en/arrayaccess.offsetexists.php 58 | * @param mixed $offset
59 | * An offset to check for. 60 | *
61 | * @return bool true on success or false on failure. 62 | * 63 | *64 | * The return value will be casted to boolean if non-boolean was returned. 65 | */ 66 | public function offsetExists(mixed $offset): bool 67 | { 68 | return isset($this->$offset); 69 | } 70 | 71 | /** 72 | * Offset to retrieve 73 | * @link https://php.net/manual/en/arrayaccess.offsetget.php 74 | * @param mixed $offset
75 | * The offset to retrieve. 76 | *
77 | * @return mixed Can return all value types. 78 | */ 79 | public function offsetGet(mixed $offset): mixed 80 | { 81 | return $this->$offset; 82 | } 83 | 84 | /** 85 | * Offset to set 86 | * @link https://php.net/manual/en/arrayaccess.offsetset.php 87 | * @param mixed $offset88 | * The offset to assign the value to. 89 | *
90 | * @param mixed $value91 | * The value to set. 92 | *
93 | * @return void 94 | */ 95 | public function offsetSet(mixed $offset, mixed $value): void 96 | { 97 | $this->$offset = $value; 98 | } 99 | 100 | /** 101 | * Offset to unset 102 | * @link https://php.net/manual/en/arrayaccess.offsetunset.php 103 | * @param mixed $offset104 | * The offset to unset. 105 | *
106 | * @return void 107 | */ 108 | public function offsetUnset(mixed $offset): void 109 | { 110 | unset($this->$offset); 111 | } 112 | 113 | /** 114 | * Specify data which should be serialized to JSON 115 | * @link https://php.net/manual/en/jsonserializable.jsonserialize.php 116 | * @return mixed data which can be serialized by json_encode, 117 | * which is a value of any type other than a resource. 118 | * @since 5.4 119 | */ 120 | public function jsonSerialize(): mixed 121 | { 122 | return $this->toArray(); 123 | } 124 | } -------------------------------------------------------------------------------- /src/Data/DddApi/DddData.php: -------------------------------------------------------------------------------- 1 | get("ddd/v1/$ddd"); 20 | $data = null; 21 | 22 | if ($responseData = self::parseJson($response)) { 23 | $data = DddData::from($responseData); 24 | } 25 | 26 | return $data; 27 | } 28 | } -------------------------------------------------------------------------------- /src/FipeApi.php: -------------------------------------------------------------------------------- 1 | get("fipe/tabelas/v1"); 25 | $data = []; 26 | 27 | if ($responseData = self::parseJson($response)) { 28 | foreach ($responseData as $refTable) { 29 | $data[] = RefTableData::from($refTable); 30 | } 31 | } 32 | 33 | return $data; 34 | } 35 | 36 | /** 37 | * @param string $codigoFipe 38 | * @param int|null $tabelaReferencia 39 | * @return PriceData[] 40 | * @throws GuzzleException 41 | * @throws ReflectionException 42 | */ 43 | public static function pricesByCode(string $codigoFipe, ?int $tabelaReferencia = null): array 44 | { 45 | $uri = "fipe/preco/v1/$codigoFipe"; 46 | if (!is_null($tabelaReferencia)) { 47 | $uri .= '?'.http_build_query([ 48 | 'tabela_referencia' => $tabelaReferencia, 49 | ]); 50 | } 51 | 52 | $response = static::getClient()->get($uri); 53 | $data = []; 54 | 55 | if ($responseData = self::parseJson($response)) { 56 | foreach ($responseData as $priceData) { 57 | $data[] = PriceData::from($priceData); 58 | } 59 | } 60 | 61 | return $data; 62 | } 63 | 64 | /** 65 | * @param string $type One of the values represented by TYPE_* constants 66 | * @param int|null $tabelaReferencia 67 | * @return BrandData[] 68 | * @throws GuzzleException 69 | * @throws ReflectionException 70 | */ 71 | public static function brandsByType(string $type, ?int $tabelaReferencia = null): array 72 | { 73 | $uri = "fipe/marcas/v1/$type"; 74 | if (!is_null($tabelaReferencia)) { 75 | $uri .= '?'.http_build_query([ 76 | 'tabela_referencia' => $tabelaReferencia, 77 | ]); 78 | } 79 | 80 | $response = static::getClient()->get($uri); 81 | $data = []; 82 | 83 | if ($responseData = self::parseJson($response)) { 84 | foreach ($responseData as $brand) { 85 | $data[] = BrandData::from($brand); 86 | } 87 | } 88 | 89 | return $data; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/HolidayApi.php: -------------------------------------------------------------------------------- 1 | get("feriados/v1/$year"); 26 | $data = []; 27 | 28 | if ($responseData = self::parseJson($response)) { 29 | foreach ($responseData as $holiday) { 30 | $holiday['date'] = DateTimeImmutable::createFromFormat('Y-m-d P', $holiday['date'].' -03:00'); 31 | $data[] = HolidayData::from($holiday); 32 | } 33 | } 34 | 35 | return $data; 36 | } 37 | } -------------------------------------------------------------------------------- /src/IbgeApi.php: -------------------------------------------------------------------------------- 1 | get("ibge/uf/v1"); 24 | $data = []; 25 | 26 | if ($responseData = self::parseJson($response)) { 27 | foreach ($responseData as $uf) { 28 | $data[] = self::prepareUfData($uf); 29 | } 30 | } 31 | 32 | return $data; 33 | } 34 | 35 | /** 36 | * @param string $code 37 | * @return UfData|null 38 | * @throws GuzzleException 39 | * @throws ReflectionException 40 | */ 41 | public static function getUf(string $code): ?UfData 42 | { 43 | $response = static::getClient()->get("ibge/uf/v1/$code"); 44 | $data = null; 45 | 46 | if ($responseData = self::parseJson($response)) { 47 | $data = self::prepareUfData($responseData); 48 | } 49 | 50 | return $data; 51 | } 52 | 53 | /** 54 | * @param string $siglaUf 55 | * @return CityData[] 56 | * @throws GuzzleException 57 | * @throws ReflectionException 58 | */ 59 | public static function getCitiesByUf(string $siglaUf): array 60 | { 61 | $response = static::getClient()->get("ibge/municipios/v1/$siglaUf"); 62 | $data = []; 63 | 64 | if ($responseData = self::parseJson($response)) { 65 | foreach ($responseData as $city) { 66 | $data[] = CityData::from(self::snakeToCamelKeys($city)); 67 | } 68 | } 69 | 70 | return $data; 71 | } 72 | 73 | /** 74 | * @param array $data 75 | * @return UfData 76 | * @throws ReflectionException 77 | */ 78 | private static function prepareUfData(array $data): UfData 79 | { 80 | $data['regiao'] = RegionData::from($data['regiao']); 81 | return UfData::from($data); 82 | } 83 | } -------------------------------------------------------------------------------- /src/IsbnApi.php: -------------------------------------------------------------------------------- 1 | get("isbn/v1/$isbn"); 25 | $data = null; 26 | 27 | if ($responseData = self::parseJson($response)) { 28 | $responseData = self::snakeToCamelKeys($responseData); 29 | 30 | if (!empty($responseData['dimensions'])) { 31 | $responseData['dimensions'] = DimensionData::from($responseData['dimensions']); 32 | } 33 | 34 | if (!empty($responseData['retailPrice'])) { 35 | $responseData['retailPrice'] = RetailPriceData::from($responseData['retailPrice']); 36 | } 37 | 38 | $data = IsbnData::from($responseData); 39 | } 40 | 41 | return $data; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/NcmApi.php: -------------------------------------------------------------------------------- 1 | get($uri); 30 | $data = []; 31 | 32 | if ($responseData = self::parseJson($response)) { 33 | foreach ($responseData as $ncm) { 34 | $ncm = self::snakeToCamelKeys($ncm); 35 | self::convertDates($ncm); 36 | 37 | $data[] = NcmData::from($ncm); 38 | } 39 | } 40 | 41 | return $data; 42 | } 43 | 44 | /** 45 | * @param string $code 46 | * @return NcmData|null 47 | * @throws GuzzleException 48 | * @throws ReflectionException 49 | */ 50 | public static function get(string $code): ?NcmData 51 | { 52 | $response = static::getClient()->get("ncm/v1/$code"); 53 | $data = null; 54 | 55 | if ($responseData = self::parseJson($response)) { 56 | $ncm = self::snakeToCamelKeys($responseData); 57 | self::convertDates($ncm); 58 | 59 | $data = NcmData::from($ncm); 60 | } 61 | 62 | return $data; 63 | } 64 | 65 | /** 66 | * @param array $ncm 67 | * @return void 68 | */ 69 | private static function convertDates(array &$ncm): void 70 | { 71 | foreach (['dataInicio', 'dataFim'] as $field) { 72 | if (!empty($ncm[$field])) { 73 | continue; 74 | } 75 | 76 | $ncm[$field] = DateTimeImmutable::createFromFormat('Y-m-d H:i P', "$ncm[$field] 00:00 -03:00"); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/PixApi.php: -------------------------------------------------------------------------------- 1 | get("pix/v1/participants"); 23 | $data = []; 24 | 25 | if ($responseData = self::parseJson($response)) { 26 | foreach ($responseData as $participant) { 27 | $participant = self::snakeToCamelKeys($participant); 28 | $participant['inicioOperacao'] = DateTimeImmutable::createFromFormat( 29 | DateTimeImmutable::RFC3339_EXTENDED, $participant['inicioOperacao'] 30 | ); 31 | 32 | $data[] = ParticipantData::from($participant); 33 | } 34 | } 35 | 36 | return $data; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/RegistroBrApi.php: -------------------------------------------------------------------------------- 1 | get("registrobr/v1/$domain"); 22 | $data = null; 23 | 24 | if ($responseData = self::parseJson($response)) { 25 | $responseData['publicationStatus'] = $responseData['publication-status']; 26 | $responseData['expiresAt'] = $responseData['expires-at'] ? 27 | DateTimeImmutable::createFromFormat('Y-m-d\TH:i:sP', $responseData['expires-at']) : null; 28 | $responseData['statusCode'] = $responseData['status_code']; 29 | 30 | $data = DomainData::from($responseData); 31 | } 32 | 33 | return $data; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/TaxApi.php: -------------------------------------------------------------------------------- 1 | get("taxas/v1"); 19 | $data = []; 20 | 21 | if ($responseData = self::parseJson($response)) { 22 | foreach ($responseData as $tax) { 23 | $data[] = TaxData::from($tax); 24 | } 25 | } 26 | 27 | return $data; 28 | } 29 | 30 | /** 31 | * @param string $nome 32 | * @return TaxData|null 33 | * @throws GuzzleException 34 | * @throws ReflectionException 35 | */ 36 | public static function getByNome(string $nome): ?TaxData 37 | { 38 | $response = static::getClient()->get("taxas/v1/$nome"); 39 | $data = null; 40 | 41 | if ($responseData = self::parseJson($response)) { 42 | $data = TaxData::from($responseData); 43 | } 44 | 45 | return $data; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Traits/ConvertsSnakeKeysToCamel.php: -------------------------------------------------------------------------------- 1 | $value) { 18 | if (is_array($value)) { 19 | $value = self::snakeToCamelKeys($value); 20 | } 21 | 22 | $aux[Str::toCamelCase($key)] = $value; 23 | } 24 | 25 | return $aux; 26 | } 27 | } -------------------------------------------------------------------------------- /src/Utils/Str.php: -------------------------------------------------------------------------------- 1 |