Skip to content

Commit

Permalink
More compatibility adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Oct 29, 2024
1 parent 7ba0095 commit 552a876
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"prefer-stable": true,
"license": "MIT",
"require": {
"php": ">=8.1",
"php": ">=8.1 <8.4",
"ext-json": "*",
"ext-openssl": "*",
"ext-curl": "*",
Expand Down
61 changes: 32 additions & 29 deletions src/Repository/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use ByJG\Config\Exception\KeyNotFoundException;
use ByJG\MicroOrm\Exception\OrmBeforeInvalidException;
use ByJG\MicroOrm\Exception\OrmInvalidFieldsException;
use ByJG\MicroOrm\Exception\RepositoryReadOnlyException;
use ByJG\MicroOrm\Exception\UpdateConstraintException;
use ByJG\MicroOrm\FieldMapping;
use ByJG\MicroOrm\Literal\HexUuidLiteral;
use ByJG\MicroOrm\Literal\Literal;
Expand Down Expand Up @@ -41,6 +39,11 @@ public function get($itemId)
return $this->repository->get(HexUuidLiteral::create($itemId));
}

public function getRepository(): Repository
{
return $this->repository;
}

public function getMapper()
{
return $this->repository->getMapper();
Expand Down Expand Up @@ -84,7 +87,7 @@ public function listGeneric($tableName, $fields = [], $page = 0, $size = 20, $or

$object = $query->build($this->repository->getDbDriver());

$iterator = $this->repository->getDbDriver()->getIterator($object["sql"], $object["params"]);
$iterator = $this->repository->getDbDriver()->getIterator($object->getSql(), $object->getParameters());
return $iterator->toArray();
}

Expand Down Expand Up @@ -158,26 +161,31 @@ public static function getUuid()
protected function setClosureFixBinaryUUID(?Mapper $mapper, $binPropertyName = 'id', $uuidStrPropertyName = 'uuid')
{
$fieldMapping = FieldMapping::create($binPropertyName)
->withUpdateFunction(function ($value, $instance) {
if (empty($value)) {
return null;
}
if (!($value instanceof Literal)) {
$value = new HexUuidLiteral($value);
}
return $value;
})
->withSelectFunction(function ($value, $instance) use ($binPropertyName, $uuidStrPropertyName) {
if (!empty($uuidStrPropertyName)) {
$fieldValue = $instance->{'get' . $uuidStrPropertyName}();
} else {
$fieldValue = HexUuidLiteral::getFormattedUuid($instance->{'get' . $binPropertyName}(), false);
->withUpdateFunction(
function ($value, $instance) {
if (empty($value)) {
return null;
}
if (!($value instanceof Literal)) {
$value = new HexUuidLiteral($value);
}
return $value;
}
if (is_null($fieldValue)) {
return null;
)
->withSelectFunction(
function ($value, $instance) use ($binPropertyName, $uuidStrPropertyName) {
if (!empty($uuidStrPropertyName)) {
$fieldValue = $instance->{'get' . $uuidStrPropertyName}();
} else {
$itemValue = $instance->{'get' . $binPropertyName}();
$fieldValue = HexUuidLiteral::getFormattedUuid($itemValue, false, $itemValue);
}
if (is_null($fieldValue)) {
return null;
}
return $fieldValue;
}
return $fieldValue;
});
);

if (!empty($mapper)) {
$mapper->addFieldMapping($fieldMapping);
Expand All @@ -187,26 +195,21 @@ protected function setClosureFixBinaryUUID(?Mapper $mapper, $binPropertyName = '
}

/**
* @param $model
* @param UpdateConstraint|null $updateConstraint
* @param $model
* @return mixed
* @throws InvalidArgumentException
* @throws OrmBeforeInvalidException
* @throws OrmInvalidFieldsException
* @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
* @throws RepositoryReadOnlyException
* @throws UpdateConstraintException
*/
public function save($model, ?UpdateConstraint $updateConstraint = null)
{
$model = $this->repository->save($model, $updateConstraint);

$primaryKey = $this->repository->getMapper()->getPrimaryKey()[0];

if ($model->{"get$primaryKey"}() instanceof HexUuidLiteral) {
/** @var HexUuidLiteral $literal */
$literal = $model->{"get$primaryKey"}();
$model->{"set$primaryKey"}($literal->formatUuid());
if ($model->{"get$primaryKey"}() instanceof Literal) {
$model->{"set$primaryKey"}(HexUuidLiteral::create($model->{"get$primaryKey"}()));
}

return $model;
Expand Down
14 changes: 10 additions & 4 deletions src/Util/OpenApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ public static function validateRequest(HttpRequest $request)
$path = $request->getRequestPath();
$method = $request->server('REQUEST_METHOD');

// Returns a SwaggerRequestBody instance
$bodyRequestDef = $schema->getRequestParameters($path, $method);

// Validate the request body (payload)
if (str_contains($request->getHeader('Content-Type') ?? "", 'multipart/')) {
$requestBody = $request->post();
Expand All @@ -31,7 +28,16 @@ public static function validateRequest(HttpRequest $request)
}

try {
$bodyRequestDef->match($requestBody);
// Validate the request path and query against the OpenAPI schema
$schema->getPathDefinition($path, $method);

if (!empty($requestBody)) {
// Returns a SwaggerRequestBody instance
$bodyRequestDef = $schema->getRequestParameters($path, $method);
$bodyRequestDef->match($requestBody);
} else {
return [];
}
} catch (Exception $ex) {
throw new Error400Exception(explode("\n", $ex->getMessage())[0]);
}
Expand Down

0 comments on commit 552a876

Please sign in to comment.