-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: padronizar e adicionar campos customizados
- Loading branch information
1 parent
d6f0878
commit ced24f2
Showing
55 changed files
with
1,430 additions
and
153 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,2 +1,4 @@ | ||
/src/**/__tests__ export-ignore | ||
*.spec.php export-ignore | ||
.vscode export-ignore | ||
demo export-ignore |
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
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,86 @@ | ||
<?php | ||
|
||
namespace AleBatistella\BlingErpApi\Entities\Shared; | ||
|
||
use AleBatistella\BlingErpApi\Contracts\IResponseObject; | ||
|
||
/** | ||
* Classe base para objetos de retorno. | ||
*/ | ||
readonly abstract class BaseResponseObject implements IResponseObject | ||
{ | ||
/** | ||
* Construtor base. | ||
*/ | ||
public function __construct(...$args) | ||
{ | ||
} | ||
|
||
/** | ||
* Conjunto de regras de conversão para o método `from`. | ||
* | ||
* Função usada para ser sobrescrita quando há propriedades na classe que | ||
* são "_array_ de objetos". | ||
* | ||
* @return array<string, IResponseObject> | ||
*/ | ||
protected static function fromRules(): array | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public static function from(array $attributes): static | ||
{ | ||
$reflectionProperties = (new \ReflectionClass(static::class))->getProperties(); | ||
$properties = []; | ||
$fromRules = static::fromRules(); | ||
|
||
foreach ($reflectionProperties as $reflectionProperty) { | ||
$propName = $reflectionProperty->getName(); | ||
$propType = $reflectionProperty->getType(); | ||
$propTypeName = $propType->getName(); | ||
$allowsNull = $reflectionProperty->getType()->allowsNull(); | ||
|
||
// Null check | ||
if ($allowsNull && !array_key_exists($propName, $attributes)) { | ||
$properties[$propName] = null; | ||
continue; | ||
} | ||
|
||
if (in_array($propTypeName, ['int', 'string', 'bool', 'float'])) { | ||
$properties[$propName] = $attributes[$propName]; | ||
} else if ($propTypeName === 'array') { | ||
if (array_key_exists($propName, $fromRules)) { | ||
// Array de objetos | ||
$properties[$propName] = array_map( | ||
fn(array $item) => $fromRules[$propName]::from($item), | ||
$attributes[$propName] | ||
); | ||
|
||
continue; | ||
} | ||
|
||
// Array de primitivos | ||
$properties[$propName] = $attributes[$propName]; | ||
} else { | ||
// Objeto | ||
/** @var IResponseObject */ | ||
$objSignature = $propTypeName; | ||
$properties[$propName] = $objSignature::from($attributes[$propName]); | ||
} | ||
} | ||
|
||
return new static(...$properties); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return objectToArray($this); | ||
} | ||
} |
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 | ||
|
||
namespace AleBatistella\BlingErpApi\Entities\Shared; | ||
|
||
use AleBatistella\BlingErpApi\Contracts\IResponseRootObject; | ||
use AleBatistella\BlingErpApi\Entities\Shared\DTO\Request\ResponseOptions; | ||
use AleBatistella\BlingErpApi\Exceptions\BlingInternalException; | ||
|
||
/** | ||
* Classe base para objetos raiz de retorno. | ||
*/ | ||
readonly abstract class BaseResponseRootObject extends BaseResponseObject implements IResponseRootObject | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public abstract static function fromResponse(ResponseOptions $response): static|null; | ||
|
||
/** | ||
* Lança `BlingInternalException` caso a resposta da API esteja | ||
* inconsistente. | ||
* | ||
* @param ResponseOptions $response | ||
* | ||
* @return never | ||
*/ | ||
protected static function throwForInconsistentResponseOptions(ResponseOptions $response): never | ||
{ | ||
throw new BlingInternalException( | ||
message: "Resposta inconsistente da API: $response->method $response->endpoint", | ||
responseHeaders: $response->headers, | ||
responseBody: $response->body, | ||
code: $response->status, | ||
); | ||
} | ||
} |
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
Oops, something went wrong.