-
Notifications
You must be signed in to change notification settings - Fork 6
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
1 parent
356c691
commit 4148a85
Showing
20 changed files
with
374 additions
and
85 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
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,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; | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} |
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Dto; | ||
|
||
final class VaccinationRequest | ||
{ | ||
public string $name; | ||
} |
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Dto; | ||
|
||
final class VaccinationResponse | ||
{ | ||
public string $name; | ||
} |
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Parsing; | ||
|
||
use Chubbyphp\Parsing\Schema\SchemaInterface; | ||
|
||
interface ParsingInterface | ||
{ | ||
public function getRequestSchema(): SchemaInterface; | ||
|
||
public function getResponeSchema(): SchemaInterface; | ||
} |
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,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); | ||
} | ||
} |
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 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); | ||
} | ||
} |
Oops, something went wrong.