-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
460 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
{ | ||
"cSpell.words": [ | ||
"brasilapi", | ||
"caminhoes", | ||
"cnpj", | ||
"Cnpj", | ||
"fipe", | ||
"Fipe", | ||
"ispb", | ||
"Spatie" | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gutocf\BrasilAPI\Entity\V1\Fipe; | ||
|
||
use Spatie\DataTransferObject\FlexibleDataTransferObject; | ||
|
||
class Brand extends FlexibleDataTransferObject | ||
{ | ||
public ?string $name; | ||
public ?string $value; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param array<string, mixed> $parameters | ||
*/ | ||
public function __construct(array $parameters = []) | ||
{ | ||
if (isset($parameters['nome'])) { | ||
$parameters['name'] = $parameters['nome']; | ||
unset($parameters['nome']); | ||
} | ||
|
||
if (isset($parameters['valor'])) { | ||
$parameters['value'] = $parameters['valor']; | ||
unset($parameters['valor']); | ||
} | ||
|
||
parent::__construct($parameters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gutocf\BrasilAPI\Entity\V1\Fipe\Enum; | ||
|
||
use MyCLabs\Enum\Enum; | ||
|
||
/** | ||
* @method static VehicleType TRUCKS() | ||
* @method static VehicleType CARS() | ||
* @method static VehicleType MOTORCYCLES() | ||
* @extends Enum<string> | ||
*/ | ||
final class VehicleType extends Enum | ||
{ | ||
private const TRUCKS = 'caminhoes'; | ||
private const CARS = 'carros'; | ||
private const MOTORCYCLES = 'motos'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gutocf\BrasilAPI\Entity\V1\Fipe; | ||
|
||
use Spatie\DataTransferObject\FlexibleDataTransferObject; | ||
|
||
class ReferenceTable extends FlexibleDataTransferObject | ||
{ | ||
public ?int $codigo; | ||
public ?string $mes; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gutocf\BrasilAPI\Entity\V1\Fipe; | ||
|
||
use Spatie\DataTransferObject\FlexibleDataTransferObject; | ||
|
||
class Vehicle extends FlexibleDataTransferObject | ||
{ | ||
public ?float $valor; | ||
public ?string $marca; | ||
public ?string $modelo; | ||
public ?int $anoModelo; | ||
public ?string $combustivel; | ||
public ?string $codigoFipe; | ||
public ?string $mesReferencia; | ||
public ?int $tipoVeiculo; | ||
public ?string $siglaCombustivel; | ||
public ?string $dataConsulta; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param array<string, mixed> $parameters | ||
*/ | ||
public function __construct(array $parameters = []) | ||
{ | ||
if (isset($parameters['valor'])) { | ||
$parameters['valor'] = floatval(intval(preg_replace('/\D/', '', $parameters['valor'])) / 100); | ||
} | ||
|
||
parent::__construct($parameters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gutocf\BrasilAPI\Exception; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class BadRequestException extends AbstractHttpException | ||
{ | ||
public function __construct(ResponseInterface $response) | ||
{ | ||
parent::__construct($response, 400); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gutocf\BrasilAPI\Service\V1; | ||
|
||
use Gutocf\BrasilAPI\Entity\V1\Fipe\Brand; | ||
use Gutocf\BrasilAPI\Entity\V1\Fipe\Enum\VehicleType; | ||
use Gutocf\BrasilAPI\Entity\V1\Fipe\ReferenceTable; | ||
use Gutocf\BrasilAPI\Entity\V1\Fipe\Vehicle; | ||
use Gutocf\BrasilAPI\Service\AbstractService; | ||
|
||
class FipeService extends AbstractService | ||
{ | ||
/** | ||
* Retrieve price for a vehicle. | ||
* | ||
* @param string $code Fipe code of the vehicle | ||
* @param int|null $referenceTableId The reference table id | ||
* @throws \Gutocf\BrasilAPI\Exception\InternalServerErrorException | ||
* @throws \Gutocf\BrasilAPI\Exception\BadRequestException | ||
* @throws \Gutocf\BrasilAPI\Exception\NotFoundException | ||
* @return \Gutocf\BrasilAPI\Entity\V1\Fipe\Vehicle[] | ||
*/ | ||
public function getAllVehicleByCode(string $code, int $referenceTableId = null): array | ||
{ | ||
$path = sprintf('/api/feriados/v1/%s', $code); | ||
$queryParams = ['tabela_referencia' => $referenceTableId]; | ||
$data = $this->adapter->get($path, $queryParams); | ||
|
||
return array_map(function ($data) { | ||
return new Vehicle($data); | ||
}, $data); | ||
} | ||
|
||
/** | ||
* Retrieve price for a vehicle. | ||
* | ||
* @throws \Gutocf\BrasilAPI\Exception\InternalServerErrorException | ||
* @return \Gutocf\BrasilAPI\Entity\V1\Fipe\ReferenceTable[] | ||
*/ | ||
public function getReferenceTables(): array | ||
{ | ||
$data = $this->adapter->get('/api/fipe/tabelas/v1'); | ||
|
||
return array_map(function ($data) { | ||
return new ReferenceTable($data); | ||
}, $data); | ||
} | ||
|
||
/** | ||
* Retrieve a list of holidays for a given year. | ||
* | ||
* @param \Gutocf\BrasilAPI\Entity\V1\Fipe\Enum\VehicleType|null $vehicleType The vehicle type | ||
* @param int|null $referenceTableId The reference table id | ||
* @throws \Gutocf\BrasilAPI\Exception\BadRequestException | ||
* @throws \Gutocf\BrasilAPI\Exception\InternalServerErrorException | ||
* @throws \Gutocf\BrasilAPI\Exception\NotFoundException | ||
* @return \Gutocf\BrasilAPI\Entity\V1\Fipe\Brand[] Vehicle brands. | ||
*/ | ||
public function getAllBrandsByType(?VehicleType $vehicleType = null, ?int $referenceTableId = null): array | ||
{ | ||
$path = sprintf('/api/feriados/v1/%s', $vehicleType?->getValue()); | ||
$queryParams = ['tabela_referencia' => $referenceTableId]; | ||
$data = $this->adapter->get($path, $queryParams); | ||
|
||
return array_map(function ($data) { | ||
return new Brand($data); | ||
}, $data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gutocf\BrasilAPI\Tests\Adapter; | ||
|
||
use Gutocf\BrasilAPI\Adapter\AbstractAdapter; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Psr7\Uri; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class AbstractAdapterTest extends TestCase | ||
{ | ||
private AbstractAdapter $adapter; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->adapter = $this->getMockForAbstractClass(AbstractAdapter::class, [new Client()]); | ||
} | ||
|
||
public function testGetUri(): void | ||
{ | ||
$reflection = new \ReflectionClass(get_class($this->adapter)); | ||
$method = $reflection->getMethod('getUri'); | ||
$method->setAccessible(true); | ||
/** @var \GuzzleHttp\Psr7\Uri $uri */ | ||
$uri = $method->invoke($this->adapter, '/path/to/resource', [ | ||
'foo' => 'bar', | ||
'baz' => 'qux', | ||
'foobar' => '', | ||
'quux' => null | ||
]); | ||
$this->assertInstanceOf(Uri::class, $uri); | ||
$this->assertEquals('https://brasilapi.com.br/path/to/resource?foo=bar&baz=qux&foobar=', $uri->__toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Gutocf\BrasilAPI\Tests\Entity\V1\Fipe; | ||
|
||
use Gutocf\BrasilAPI\Entity\V1\Fipe\Brand; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class BrandTest extends TestCase | ||
{ | ||
public function testProperties(): void | ||
{ | ||
$data = loadFixture('Entity/V1/Fipe/brand'); | ||
$brand = new Brand($data); | ||
$this->assertEquals($brand->name, $data['nome']); | ||
$this->assertEquals($brand->value, $data['valor']); | ||
$this->assertObjectNotHasAttribute('invalid', $brand); | ||
} | ||
} |
Oops, something went wrong.