Skip to content

Commit

Permalink
chubbyphp-parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikzogg committed Feb 4, 2024
1 parent 356c691 commit 85414b7
Show file tree
Hide file tree
Showing 25 changed files with 457 additions and 99 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"chubbyphp/chubbyphp-laminas-config": "^1.4",
"chubbyphp/chubbyphp-laminas-config-doctrine": "^2.1",
"chubbyphp/chubbyphp-laminas-config-factory": "^1.3",
"chubbyphp/chubbyphp-negotiation": "^2.0",
"chubbyphp/chubbyphp-negotiation": "^2.1",
"chubbyphp/chubbyphp-parsing": "^1.0@dev",
"chubbyphp/chubbyphp-serialization": "^4.0",
"chubbyphp/chubbyphp-validation": "^4.0",
"doctrine/orm": "^2.17.2",
Expand Down
24 changes: 21 additions & 3 deletions config/prod.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

declare(strict_types=1);

use App\Enrich\EnrichInterface;
use App\Factory\Collection\PetCollectionFactory;
use App\Factory\Model\PetFactory;
use App\Mapping\Orm\PetMapping;
use App\Mapping\Orm\VaccinationMapping;
use App\Model\Pet;
use App\Model\Vaccination;
use App\Parsing\PetParsing;
use App\Repository\PetRepository;
use App\RequestHandler\Api\Crud\CreateRequestHandler;
use App\RequestHandler\Api\Crud\DeleteRequestHandler;
Expand All @@ -21,6 +23,7 @@
use App\ServiceFactory\DecodeEncode\TypeDecodersFactory;
use App\ServiceFactory\DecodeEncode\TypeEncodersFactory;
use App\ServiceFactory\Deserialization\DenormalizationObjectMappingsFactory;
use App\ServiceFactory\Enrich\PetEnrichFactory;
use App\ServiceFactory\Factory\Collection\PetCollectionFactoryFactory;
use App\ServiceFactory\Factory\Model\PetFactoryFactory;
use App\ServiceFactory\Framework\ExceptionMiddlewareFactory;
Expand All @@ -34,6 +37,8 @@
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\Repository\PetRepositoryFactory;
use App\ServiceFactory\RequestHandler\Api\Crud\PetCreateRequestHandlerFactory;
use App\ServiceFactory\RequestHandler\Api\Crud\PetDeleteRequestHandlerFactory;
Expand All @@ -46,16 +51,18 @@
use App\ServiceFactory\Validation\ValidationMappingProviderFactory;
use Chubbyphp\ApiHttp\Manager\RequestManagerInterface;
use Chubbyphp\ApiHttp\Manager\ResponseManagerInterface;
use Chubbyphp\ApiHttp\Middleware\AcceptAndContentTypeMiddleware;
use Chubbyphp\ApiHttp\Middleware\ApiExceptionMiddleware;
use Chubbyphp\ApiHttp\ServiceFactory\AcceptAndContentTypeMiddlewareFactory;
use Chubbyphp\ApiHttp\ServiceFactory\ApiExceptionMiddlewareFactory;
use Chubbyphp\ApiHttp\ServiceFactory\RequestManagerFactory;
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 @@ -72,8 +79,13 @@
use Chubbyphp\Laminas\Config\Doctrine\ServiceFactory\Persistence\Mapping\Driver\ClassMapDriverFactory;
use Chubbyphp\Negotiation\AcceptNegotiatorInterface;
use Chubbyphp\Negotiation\ContentTypeNegotiatorInterface;
use Chubbyphp\Negotiation\Middleware\AcceptMiddleware;
use Chubbyphp\Negotiation\Middleware\ContentTypeMiddleware;
use Chubbyphp\Negotiation\ServiceFactory\AcceptMiddlewareFactory;
use Chubbyphp\Negotiation\ServiceFactory\AcceptNegotiatorFactory;
use Chubbyphp\Negotiation\ServiceFactory\ContentTypeMiddlewareFactory;
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 @@ -116,19 +128,22 @@
EntityManager::class => EntityManagerInterface::class,
],
'factories' => [
AcceptAndContentTypeMiddleware::class => AcceptAndContentTypeMiddlewareFactory::class,
AcceptMiddleware::class => AcceptMiddlewareFactory::class,
AcceptNegotiatorInterface::class . 'supportedMediaTypes[]' => AcceptNegotiatorSupportedMediaTypesFactory::class,
AcceptNegotiatorInterface::class => AcceptNegotiatorFactory::class,
ApiExceptionMiddleware::class => ApiExceptionMiddlewareFactory::class,
CacheItemPoolInterface::class => ApcuAdapterFactory::class,
Command::class . '[]' => CommandsFactory::class,
Connection::class => ConnectionFactory::class,
ConnectionProvider::class => ContainerConnectionProviderFactory::class,
ContentTypeMiddleware::class => ContentTypeMiddlewareFactory::class,
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 +152,16 @@
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 . EnrichInterface::class => PetEnrichFactory::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 Down
15 changes: 15 additions & 0 deletions src/Dto/LinkResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace App\Dto;

final class LinkResponse
{
public string $href;

/**
* @var array<string, string>
*/
public array $attributes;
}
14 changes: 14 additions & 0 deletions src/Dto/ModelRequestInterface.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 ModelRequestInterface
{
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 ModelRequestInterface
{
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;
}
}
28 changes: 28 additions & 0 deletions src/Dto/PetResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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;

/**
* @var array<string, LinkResponse>
*/
public array $_links;
}
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;
}
27 changes: 27 additions & 0 deletions src/Enrich/Enrich.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace App\Enrich;

use App\Model\ModelInterface;

final class Enrich implements EnrichInterface
{
public function __construct(private string $basePath) {}

/**
* @return array<mixed>
*/
public function enrich(ModelInterface $model): array
{
return [
...$model->jsonSerialize(),
'_links' => [
'read' => ['href' => $this->basePath.$model->getId(), 'attributes' => ['method' => 'GET']],
'update' => ['href' => $this->basePath.$model->getId(), 'attributes' => ['method' => 'PUT']],
'delete' => ['href' => $this->basePath.$model->getId(), 'attributes' => ['method' => 'DELETE']],
],
];
}
}
15 changes: 15 additions & 0 deletions src/Enrich/EnrichInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace App\Enrich;

use App\Model\ModelInterface;

interface EnrichInterface
{
/**
* @return array<mixed>
*/
public function enrich(ModelInterface $model): array;
}
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;
}
Loading

0 comments on commit 85414b7

Please sign in to comment.