Skip to content

Commit 0e72f16

Browse files
authored
Merge pull request #86 from Yarre/support-optional-nullable
Support optional nullable
2 parents f6c6ac4 + 00ca723 commit 0e72f16

File tree

81 files changed

+1288
-446
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1288
-446
lines changed

CHANGELOG.md

+4

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace OpenApi\PetStoreApiConsumer;
4+
5+
use GuzzleHttp\Client;
6+
use OpenApi\PetStoreClient\Request\DeletePetRequest;
7+
use OpenApi\PetStoreClient\Request\FindPetsByStatusRequest;
8+
use OpenApi\PetStoreClient\Request\GetPetByIdRequest;
9+
use OpenApi\PetStoreClient\Request\UpdatePetRequest;
10+
use OpenApi\PetStoreClient\Schema\Pet;
11+
use OpenApi\PetStoreClient\SwaggerPetstoreOpenAPI3ClientFactory;
12+
use UnexpectedValueException;
13+
14+
class PetStoreApiConsumer
15+
{
16+
private $petClient;
17+
18+
public function __construct()
19+
{
20+
$this->petClient = (new SwaggerPetstoreOpenAPI3ClientFactory())
21+
->create(new Client(['base_uri' => 'http://pet.wiremock:8080']));
22+
}
23+
24+
public function findPetsByStatus(): Pet
25+
{
26+
$request = new FindPetsByStatusRequest();
27+
$request->setStatus('sold');
28+
$result = $this->petClient->findPetsByStatus($request);
29+
if ($result === null || $result->count() === 0) {
30+
throw new UnexpectedValueException('findPetsByStatus should be not null or empty');
31+
}
32+
33+
return $result->first();
34+
}
35+
36+
public function getPetById(int $petId): Pet
37+
{
38+
$request = new GetPetByIdRequest($petId);
39+
$result = $this->petClient->getPetById($request);
40+
if ($result === null) {
41+
throw new UnexpectedValueException('getPetById should not be null');
42+
}
43+
44+
return $result;
45+
}
46+
47+
public function updatePet(Pet $pet, string $mimeType): void
48+
{
49+
$request = new UpdatePetRequest($pet, $mimeType);
50+
$result = $this->petClient->updatePet($request);
51+
if ($result === null) {
52+
printf('getPetById failed, result: %s', json_encode($result, JSON_THROW_ON_ERROR)) || exit(1);
53+
}
54+
}
55+
56+
public function deletePet(int $petId): void
57+
{
58+
$request = new DeletePetRequest($petId);
59+
$this->petClient->deletePet($request);
60+
}
61+
}

example/gen/composer.json renamed to example/PetStoreClient/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"require": {
1212
"php": ">=7.4",
13-
"docler-labs/api-client-exception": "^1.0",
13+
"docler-labs/api-client-exception": "^1.0|^2.0",
1414
"ext-dom": "*",
1515
"ext-json": "*",
1616
"guzzlehttp/psr7": "^1.6",

example/gen/doc/petstore3.json renamed to example/PetStoreClient/doc/petstore3.json

+5
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,11 @@
10891089
"pending",
10901090
"sold"
10911091
]
1092+
},
1093+
"chipped": {
1094+
"type": "boolean",
1095+
"description": "Is pet is chipped or not. Null means there is no information",
1096+
"nullable": true
10921097
}
10931098
},
10941099
"xml": {

example/gen/src/Schema/Category.php renamed to example/PetStoreClient/src/Schema/Category.php

+18-4
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,34 @@ class Category implements SerializableInterface, JsonSerializable
1818

1919
private ?string $name = null;
2020

21+
private array $optionalPropertyChanged = ['id' => false, 'name' => false];
22+
2123
public function setId(int $id): self
2224
{
23-
$this->id = $id;
25+
$this->id = $id;
26+
$this->optionalPropertyChanged['id'] = true;
2427

2528
return $this;
2629
}
2730

2831
public function setName(string $name): self
2932
{
30-
$this->name = $name;
33+
$this->name = $name;
34+
$this->optionalPropertyChanged['name'] = true;
3135

3236
return $this;
3337
}
3438

39+
public function hasId(): bool
40+
{
41+
return $this->optionalPropertyChanged['id'];
42+
}
43+
44+
public function hasName(): bool
45+
{
46+
return $this->optionalPropertyChanged['name'];
47+
}
48+
3549
public function getId(): ?int
3650
{
3751
return $this->id;
@@ -45,10 +59,10 @@ public function getName(): ?string
4559
public function toArray(): array
4660
{
4761
$fields = [];
48-
if ($this->id !== null) {
62+
if ($this->hasId()) {
4963
$fields['id'] = $this->id;
5064
}
51-
if ($this->name !== null) {
65+
if ($this->hasName()) {
5266
$fields['name'] = $this->name;
5367
}
5468

example/gen/src/Schema/Mapper/PetMapper.php renamed to example/PetStoreClient/src/Schema/Mapper/PetMapper.php

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public function toSchema(array $payload): Pet
4747
if (isset($payload['status'])) {
4848
$schema->setStatus($payload['status']);
4949
}
50+
if (\array_key_exists('chipped', $payload)) {
51+
$schema->setChipped($payload['chipped']);
52+
}
5053

5154
return $schema;
5255
}

example/gen/src/Schema/Order.php renamed to example/PetStoreClient/src/Schema/Order.php

+50-12
Original file line numberDiff line numberDiff line change
@@ -33,48 +33,86 @@ class Order implements SerializableInterface, JsonSerializable
3333

3434
private ?bool $complete = null;
3535

36+
private array $optionalPropertyChanged = ['id' => false, 'petId' => false, 'quantity' => false, 'shipDate' => false, 'status' => false, 'complete' => false];
37+
3638
public function setId(int $id): self
3739
{
38-
$this->id = $id;
40+
$this->id = $id;
41+
$this->optionalPropertyChanged['id'] = true;
3942

4043
return $this;
4144
}
4245

4346
public function setPetId(int $petId): self
4447
{
45-
$this->petId = $petId;
48+
$this->petId = $petId;
49+
$this->optionalPropertyChanged['petId'] = true;
4650

4751
return $this;
4852
}
4953

5054
public function setQuantity(int $quantity): self
5155
{
52-
$this->quantity = $quantity;
56+
$this->quantity = $quantity;
57+
$this->optionalPropertyChanged['quantity'] = true;
5358

5459
return $this;
5560
}
5661

5762
public function setShipDate(DateTimeInterface $shipDate): self
5863
{
59-
$this->shipDate = $shipDate;
64+
$this->shipDate = $shipDate;
65+
$this->optionalPropertyChanged['shipDate'] = true;
6066

6167
return $this;
6268
}
6369

6470
public function setStatus(string $status): self
6571
{
66-
$this->status = $status;
72+
$this->status = $status;
73+
$this->optionalPropertyChanged['status'] = true;
6774

6875
return $this;
6976
}
7077

7178
public function setComplete(bool $complete): self
7279
{
73-
$this->complete = $complete;
80+
$this->complete = $complete;
81+
$this->optionalPropertyChanged['complete'] = true;
7482

7583
return $this;
7684
}
7785

86+
public function hasId(): bool
87+
{
88+
return $this->optionalPropertyChanged['id'];
89+
}
90+
91+
public function hasPetId(): bool
92+
{
93+
return $this->optionalPropertyChanged['petId'];
94+
}
95+
96+
public function hasQuantity(): bool
97+
{
98+
return $this->optionalPropertyChanged['quantity'];
99+
}
100+
101+
public function hasShipDate(): bool
102+
{
103+
return $this->optionalPropertyChanged['shipDate'];
104+
}
105+
106+
public function hasStatus(): bool
107+
{
108+
return $this->optionalPropertyChanged['status'];
109+
}
110+
111+
public function hasComplete(): bool
112+
{
113+
return $this->optionalPropertyChanged['complete'];
114+
}
115+
78116
public function getId(): ?int
79117
{
80118
return $this->id;
@@ -108,22 +146,22 @@ public function getComplete(): ?bool
108146
public function toArray(): array
109147
{
110148
$fields = [];
111-
if ($this->id !== null) {
149+
if ($this->hasId()) {
112150
$fields['id'] = $this->id;
113151
}
114-
if ($this->petId !== null) {
152+
if ($this->hasPetId()) {
115153
$fields['petId'] = $this->petId;
116154
}
117-
if ($this->quantity !== null) {
155+
if ($this->hasQuantity()) {
118156
$fields['quantity'] = $this->quantity;
119157
}
120-
if ($this->shipDate !== null) {
158+
if ($this->hasShipDate()) {
121159
$fields['shipDate'] = $this->shipDate->format(DATE_RFC3339);
122160
}
123-
if ($this->status !== null) {
161+
if ($this->hasStatus()) {
124162
$fields['status'] = $this->status;
125163
}
126-
if ($this->complete !== null) {
164+
if ($this->hasComplete()) {
127165
$fields['complete'] = $this->complete;
128166
}
129167

0 commit comments

Comments
 (0)