Skip to content

Commit

Permalink
chubbyphp-parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikzogg committed Jan 31, 2024
1 parent 356c691 commit 4148a85
Show file tree
Hide file tree
Showing 20 changed files with 374 additions and 85 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"chubbyphp/chubbyphp-laminas-config-doctrine": "^2.1",
"chubbyphp/chubbyphp-laminas-config-factory": "^1.3",
"chubbyphp/chubbyphp-negotiation": "^2.0",
"chubbyphp/chubbyphp-parsing": "^1.0@dev",
"chubbyphp/chubbyphp-serialization": "^4.0",
"chubbyphp/chubbyphp-validation": "^4.0",
"doctrine/orm": "^2.17.2",
Expand Down
15 changes: 15 additions & 0 deletions config/prod.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use App\Mapping\Orm\VaccinationMapping;
use App\Model\Pet;
use App\Model\Vaccination;
use App\Parsing\PetParsing;
use App\Parsing\VaccinationParsing;
use App\Repository\PetRepository;
use App\RequestHandler\Api\Crud\CreateRequestHandler;
use App\RequestHandler\Api\Crud\DeleteRequestHandler;
Expand All @@ -34,6 +36,9 @@
use App\ServiceFactory\Logger\LoggerFactory;
use App\ServiceFactory\Negotiation\AcceptNegotiatorSupportedMediaTypesFactory;
use App\ServiceFactory\Negotiation\ContentTypeNegotiatorSupportedMediaTypesFactory;
use App\ServiceFactory\Parsing\ParserFactory;
use App\ServiceFactory\Parsing\PetParsingFactory;
use App\ServiceFactory\Parsing\VaccinationParsingFactory;
use App\ServiceFactory\Repository\PetRepositoryFactory;
use App\ServiceFactory\RequestHandler\Api\Crud\PetCreateRequestHandlerFactory;
use App\ServiceFactory\RequestHandler\Api\Crud\PetDeleteRequestHandlerFactory;
Expand All @@ -54,8 +59,12 @@
use Chubbyphp\ApiHttp\ServiceFactory\ResponseManagerFactory;
use Chubbyphp\Cors\CorsMiddleware;
use Chubbyphp\Cors\ServiceFactory\CorsMiddlewareFactory;
use Chubbyphp\DecodeEncode\Decoder\DecoderInterface;
use Chubbyphp\DecodeEncode\Decoder\TypeDecoderInterface;
use Chubbyphp\DecodeEncode\Encoder\EncoderInterface;
use Chubbyphp\DecodeEncode\Encoder\TypeEncoderInterface;
use Chubbyphp\DecodeEncode\ServiceFactory\DecoderFactory;
use Chubbyphp\DecodeEncode\ServiceFactory\EncoderFactory;
use Chubbyphp\Deserialization\DeserializerInterface;
use Chubbyphp\Deserialization\Mapping\DenormalizationObjectMappingInterface;
use Chubbyphp\Deserialization\ServiceFactory\DeserializerFactory;
Expand All @@ -74,6 +83,7 @@
use Chubbyphp\Negotiation\ContentTypeNegotiatorInterface;
use Chubbyphp\Negotiation\ServiceFactory\AcceptNegotiatorFactory;
use Chubbyphp\Negotiation\ServiceFactory\ContentTypeNegotiatorFactory;
use Chubbyphp\Parsing\ParserInterface;
use Chubbyphp\Serialization\Mapping\NormalizationObjectMappingInterface;
use Chubbyphp\Serialization\SerializerInterface;
use Chubbyphp\Serialization\ServiceFactory\SerializerFactory;
Expand Down Expand Up @@ -127,8 +137,10 @@
ContentTypeNegotiatorInterface::class . 'supportedMediaTypes[]' => ContentTypeNegotiatorSupportedMediaTypesFactory::class,
ContentTypeNegotiatorInterface::class => ContentTypeNegotiatorFactory::class,
CorsMiddleware::class => CorsMiddlewareFactory::class,
DecoderInterface::class => DecoderFactory::class,
DenormalizationObjectMappingInterface::class . '[]' => DenormalizationObjectMappingsFactory::class,
DeserializerInterface::class => DeserializerFactory::class,
EncoderInterface::class => EncoderFactory::class,
EntityManagerInterface::class => EntityManagerFactory::class,
EntityManagerProvider::class => ContainerEntityManagerProviderFactory::class,
ExceptionMiddleware::class => ExceptionMiddlewareFactory::class,
Expand All @@ -137,13 +149,15 @@
MiddlewareInterface::class . '[]' => MiddlewaresFactory::class,
NormalizationObjectMappingInterface::class . '[]' => NormalizationObjectMappingsFactory::class,
OpenapiRequestHandler::class => OpenapiRequestHandlerFactory::class,
ParserInterface::class => ParserFactory::class,
Pet::class . CreateRequestHandler::class => PetCreateRequestHandlerFactory::class,
Pet::class . DeleteRequestHandler::class => PetDeleteRequestHandlerFactory::class,
Pet::class . ListRequestHandler::class => PetListRequestHandlerFactory::class,
Pet::class . ReadRequestHandler::class => PetReadRequestHandlerFactory::class,
Pet::class . UpdateRequestHandler::class => PetUpdateRequestHandlerFactory::class,
PetCollectionFactory::class => PetCollectionFactoryFactory::class,
PetFactory::class => PetFactoryFactory::class,
PetParsing::class => PetParsingFactory::class,
PetRepository::class => PetRepositoryFactory::class,
PingRequestHandler::class => PingRequestHandlerFactory::class,
RequestManagerInterface::class => RequestManagerFactory::class,
Expand All @@ -157,6 +171,7 @@
TypeDecoderInterface::class . '[]' => TypeDecodersFactory::class,
TypeEncoderInterface::class . '[]' => TypeEncodersFactory::class,
UrlGeneratorInterface::class => UrlGeneratorFactory::class,
VaccinationParsing::class => VaccinationParsingFactory::class,
ValidationMappingProviderInterface::class . '[]' => ValidationMappingProviderFactory::class,
ValidationMappingProviderRegistryInterface::class => ValidationMappingProviderRegistryFactory::class,
ValidatorInterface::class => ValidatorFactory::class,
Expand Down
14 changes: 14 additions & 0 deletions src/Dto/DtoToModelInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace App\Dto;

use App\Model\ModelInterface;

interface DtoToModelInterface
{
public function createModel(): ModelInterface;

public function updateModel(ModelInterface $model): ModelInterface;
}
59 changes: 59 additions & 0 deletions src/Dto/PetRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace App\Dto;

use App\Model\ModelInterface;
use App\Model\Pet;
use App\Model\Vaccination;

final class PetRequest implements DtoToModelInterface
{
public string $name;

public null|string $tag;

/**
* @var array<VaccinationRequest>
*/
public array $vaccinations;

public function createModel(): ModelInterface
{
$vaccinations = [];
foreach ($this->vaccinations as $vaccinationRequest) {
$vaccination = new Vaccination();
$vaccination->setName($vaccinationRequest->name);

$vaccinations[] = $vaccination;
}

$model = new Pet();
$model->setName($this->name);
$model->setTag($this->tag);
$model->setVaccinations($vaccinations);

return $model;
}

/**
* @param Pet $model
*/
public function updateModel(ModelInterface $model): ModelInterface
{
$vaccinations = [];
foreach ($this->vaccinations as $vaccinationRequest) {
$vaccination = new Vaccination();
$vaccination->setName($vaccinationRequest->name);

$vaccinations[] = $vaccination;
}

$model->setName($this->name);
$model->setTag($this->tag);
$model->setVaccinations($vaccinations);

return $model;
}
}
23 changes: 23 additions & 0 deletions src/Dto/PetResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace App\Dto;

final class PetResponse
{
public string $id;

public string $createdAt;

public null|string $updatedAt;

public string $name;

public null|string $tag;

/**
* @var array<VaccinationResponse>
*/
public array $vaccinations;
}
10 changes: 10 additions & 0 deletions src/Dto/VaccinationRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace App\Dto;

final class VaccinationRequest
{
public string $name;
}
10 changes: 10 additions & 0 deletions src/Dto/VaccinationResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace App\Dto;

final class VaccinationResponse
{
public string $name;
}
2 changes: 1 addition & 1 deletion src/Model/ModelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Model;

interface ModelInterface
interface ModelInterface extends \JsonSerializable
{
public function getId(): string;

Expand Down
17 changes: 17 additions & 0 deletions src/Model/Pet.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,21 @@ public function getVaccinations(): array
{
return $this->vaccinations->getValues();
}

public function jsonSerialize(): array
{
$vaccinations = [];
foreach ($this->vaccinations as $vaccination) {
$vaccinations[] = $vaccination->jsonSerialize();
}

return [
'id' => $this->id,
'createdAt' => $this->createdAt,
'updatedAt' => $this->updatedAt,
'name' => $this->name,
'tag' => $this->tag,
'vaccinations' => $vaccinations,
];
}
}
9 changes: 8 additions & 1 deletion src/Model/Vaccination.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Ramsey\Uuid\Uuid;

final class Vaccination
final class Vaccination implements \JsonSerializable
{
private string $id;

Expand Down Expand Up @@ -38,4 +38,11 @@ public function setPet(?Pet $pet): void
{
$this->pet = $pet;
}

public function jsonSerialize(): array
{
return [
'name' => $this->name,
];
}
}
14 changes: 14 additions & 0 deletions src/Parsing/ParsingInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace App\Parsing;

use Chubbyphp\Parsing\Schema\SchemaInterface;

interface ParsingInterface
{
public function getRequestSchema(): SchemaInterface;

public function getResponeSchema(): SchemaInterface;
}
41 changes: 41 additions & 0 deletions src/Parsing/PetParsing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace App\Parsing;

use App\Dto\PetRequest;
use App\Dto\PetResponse;
use Chubbyphp\Parsing\Parser;
use Chubbyphp\Parsing\Schema\SchemaInterface;

final class PetParsing implements ParsingInterface
{
public function __construct(private Parser $parser, private VaccinationParsing $vaccinationParsing) {}

public function getRequestSchema(): SchemaInterface
{
$p = $this->parser;

return $p->object([
'name' => $p->string(),
'tag' => $p->string()->nullable(),
'vaccinations' => $p->array($this->vaccinationParsing->getRequestSchema()),
], PetRequest::class)->ignoreFieldNames(['id', 'createdAt', 'updatedAt', '_links']);
}

public function getResponeSchema(): SchemaInterface
{
$p = $this->parser;

return $p->object([
'id' => $p->string(),
'createdAt' => $p->dateTime()->toString(),
'updatedAt' => $p->dateTime()->nullable()->toString(),
'name' => $p->string(),
'tag' => $p->string()->nullable(),
'vaccinations' => $p->array($this->vaccinationParsing->getResponeSchema()),

], PetResponse::class);
}
}
33 changes: 33 additions & 0 deletions src/Parsing/VaccinationParsing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace App\Parsing;

use App\Dto\VaccinationRequest;
use App\Dto\VaccinationResponse;
use Chubbyphp\Parsing\Parser;
use Chubbyphp\Parsing\Schema\SchemaInterface;

final class VaccinationParsing implements ParsingInterface
{
public function __construct(private Parser $parser) {}

public function getRequestSchema(): SchemaInterface
{
$p = $this->parser;

return $p->object([
'name' => $p->string(),
], VaccinationRequest::class);
}

public function getResponeSchema(): SchemaInterface
{
$p = $this->parser;

return $p->object([
'name' => $p->string(),
], VaccinationResponse::class);
}
}
Loading

0 comments on commit 4148a85

Please sign in to comment.