Skip to content

Commit

Permalink
OpenAPI: add Exaple implementation add type hints to OpenAPI schema o…
Browse files Browse the repository at this point in the history
…bject properties

Signed-off-by: Roman Ondráček <[email protected]>
  • Loading branch information
Roman3349 authored and f3l1x committed Oct 24, 2022
1 parent 3b51deb commit 08e8857
Show file tree
Hide file tree
Showing 27 changed files with 289 additions and 96 deletions.
2 changes: 1 addition & 1 deletion src/OpenApi/Schema/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Callback
{

/** @var mixed[] */
private $data = [];
private array $data = [];

/**
* @param mixed[] $data
Expand Down
36 changes: 27 additions & 9 deletions src/OpenApi/Schema/Components.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@ class Components
{

/** @var Schema[]|Reference[] */
private $schemas = [];
private array $schemas = [];

/** @var Response[]|Reference[] */
private $responses = [];
private array $responses = [];

/** @var Parameter[]|Reference[] */
private $parameters = [];
private array $parameters = [];

/** @var Example[]|Reference[] */
private $examples = [];
private array $examples = [];

/** @var RequestBody[]|Reference[] */
private $requestBodies = [];
private array $requestBodies = [];

/** @var Header[]|Reference[] */
private $headers = [];
private array $headers = [];

/** @var SecurityScheme[]|Reference[] */
private $securitySchemes = [];
private array $securitySchemes = [];

/** @var Link[]|Reference[] */
private $links = [];
private array $links = [];

/** @var Callback[]|Reference[] */
private $callbacks = [];
private array $callbacks = [];

/**
* @param mixed[] $data
Expand All @@ -56,6 +56,12 @@ public static function fromArray(array $data): Components
}
}

if (isset($data['examples'])) {
foreach ($data['examples'] as $exampleKey => $exampleData) {
$components->setExample($exampleKey, Example::fromArray($exampleData));
}
}

if (isset($data['requestBodies'])) {
foreach ($data['requestBodies'] as $requestBodyKey => $requestBodyData) {
$components->setRequestBody($requestBodyKey, RequestBody::fromArray($requestBodyData));
Expand Down Expand Up @@ -101,6 +107,14 @@ public function setParameter(string $name, $parameter): void
$this->parameters[$name] = $parameter;
}

/**
* @param Example|Reference $example
*/
public function setExample(string $name, $example): void
{
$this->examples[$name] = $example;
}

/**
* @param RequestBody|Reference $requestBody
*/
Expand Down Expand Up @@ -143,6 +157,10 @@ public function toArray(): array
$data['parameters'][$parameterKey] = $parameter->toArray();
}

foreach ($this->examples as $exampleKey => $example) {
$data['examples'][$exampleKey] = $example->toArray();
}

foreach ($this->requestBodies as $requestBodyKey => $requestBody) {
$data['requestBodies'][$requestBodyKey] = $requestBody->toArray();
}
Expand Down
6 changes: 3 additions & 3 deletions src/OpenApi/Schema/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ class Contact
{

/** @var string|null */
private $name;
private ?string $name = null;

/** @var string|null */
private $url;
private ?string $url = null;

/** @var string|null */
private $email;
private ?string $email = null;

/**
* @return mixed[]
Expand Down
106 changes: 105 additions & 1 deletion src/OpenApi/Schema/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,116 @@
class Example
{

private ?string $summary = null;

private ?string $description = null;

/** @var mixed|null */
private $value = null;

private ?string $externalValue = null;

/**
* @param mixed[] $data
* @return self
*/
public static function fromArray(array $data): self
{
$example = new Example();
$example->summary = $data['summary'] ?? null;
$example->description = $data['description'] ?? null;
$example->value = $data['value'] ?? null;
$example->externalValue = $data['externalValue'] ?? null;
return $example;
}

/**
* @return mixed[]
*/
public function toArray(): array
{
return [];
$data = [];
if ($this->summary !== null) {
$data['summary'] = $this->summary;
}

if ($this->description !== null) {
$data['description'] = $this->description;
}

if ($this->value !== null) {
$data['value'] = $this->value;
}

if ($this->externalValue !== null) {
$data['externalValue'] = $this->externalValue;
}

return $data;
}

/**
* @return string|null
*/
public function getSummary(): ?string
{
return $this->summary;
}

/**
* @param string|null $summary
*/
public function setSummary(?string $summary): void
{
$this->summary = $summary;
}

/**
* @return string|null
*/
public function getDescription(): ?string
{
return $this->description;
}

/**
* @param string|null $description
*/
public function setDescription(?string $description): void
{
$this->description = $description;
}

/**
* @return mixed|null
*/
public function getValue()
{
return $this->value;
}

/**
* @param mixed|null $value
*/
public function setValue($value): void
{
$this->value = $value;
}

/**
* @return string|null
*/
public function getExternalValue(): ?string
{
return $this->externalValue;
}

/**
* @param string|null $externalValue
*/
public function setExternalValue(?string $externalValue): void
{
$this->externalValue = $externalValue;
}

}
4 changes: 2 additions & 2 deletions src/OpenApi/Schema/ExternalDocumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class ExternalDocumentation
{

/** @var string|null */
private $description;
private ?string $description = null;

/** @var string */
private $url;
private string $url;

public function __construct(string $url)
{
Expand Down
4 changes: 2 additions & 2 deletions src/OpenApi/Schema/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class Header
{

/** @var string|null */
private $description;
private ?string $description = null;

/** @var Schema|Reference|null */
private $schema;
private $schema = null;

/**
* @param mixed[] $data
Expand Down
12 changes: 6 additions & 6 deletions src/OpenApi/Schema/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ class Info
{

/** @var string */
private $title;
private string $title;

/** @var string|null */
private $description;
private ?string $description = null;

/** @var string|null */
private $termsOfService;
private ?string $termsOfService = null;

/** @var Contact|null */
private $contact;
private ?Contact $contact = null;

/** @var License|null */
private $license;
private ?License $license = null;

/** @var string */
private $version;
private string $version;

public function __construct(string $title, string $version)
{
Expand Down
4 changes: 2 additions & 2 deletions src/OpenApi/Schema/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class License
{

/** @var string */
private $name;
private string $name;

/** @var string|null */
private $url;
private ?string $url = null;

public function __construct(string $name)
{
Expand Down
27 changes: 27 additions & 0 deletions src/OpenApi/Schema/MediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class MediaType
/** @var mixed */
private $example;

/** @var string[]|Example[]|Reference[] */
private array $examples = [];

/**
* @param mixed[] $data
*/
Expand All @@ -26,6 +29,16 @@ public static function fromArray(array $data): MediaType
}

$mediaType->setExample($data['example'] ?? null);
if (isset($data['examples'])) {
foreach ($data['examples'] as $name => $example) {
if (isset($example['$ref'])) {
$mediaType->addExample($name, new Reference($example['$ref']));
} else {
$mediaType->addExample($name, Example::fromArray($example));
}
}
}

return $mediaType;
}

Expand Down Expand Up @@ -61,6 +74,14 @@ public function setExample($example): void
$this->example = $example;
}

/**
* @param Example|Reference|string $example
*/
public function addExample(string $name, $example): void
{
$this->examples[$name] = $example;
}

/**
* @return mixed[]
*/
Expand All @@ -75,6 +96,12 @@ public function toArray(): array
$data['example'] = $this->example;
}

if ($this->examples !== []) {
$data['examples'] = array_map(static function ($example) {
return is_string($example) ? $example : $example->toArray();
}, $this->examples);
}

return $data;
}

Expand Down
2 changes: 1 addition & 1 deletion src/OpenApi/Schema/OAuthFlow.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class OAuthFlow
private string $refreshUrl;

/** @var array<string, string> */
private array $scopes;
private array $scopes = [];

/**
* @param string $authorizationUrl
Expand Down
16 changes: 8 additions & 8 deletions src/OpenApi/Schema/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ class OpenApi
{

/** @var string */
private $openapi;
private string $openapi;

/** @var Info */
private $info;
private Info $info;

/** @var Server[] */
private $servers = [];
private array $servers = [];

/** @var Paths */
private $paths;
private Paths $paths;

/** @var Components|null */
private $components;
private ?Components $components = null;

/** @var SecurityRequirement[] */
private $security = [];
private array $security = [];

/** @var Tag[] */
private $tags = [];
private array $tags = [];

/** @var ExternalDocumentation|null */
private $externalDocs;
private ?ExternalDocumentation $externalDocs = null;

public function __construct(string $openapi, Info $info, Paths $paths)
{
Expand Down
Loading

0 comments on commit 08e8857

Please sign in to comment.