Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support re-importing objects exported via var_dump #56

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Objects/BaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,14 @@ public function __get(string $name)

throw new PropertyDoesNotExistException("[{$name}] is not a valid property.");
}

public static function __set_state(array $properties)
{
$obj = new static($properties['objectId']);

$obj->ref = $properties['ref'];
$obj->extensions = $properties['extensions'];

return $obj;
}
}
14 changes: 14 additions & 0 deletions src/Objects/Components.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,18 @@ protected function generate(): array
'callbacks' => $callbacks ?: null,
]);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->schemas(...($properties['schemas'] ?? []))
->responses(...($properties['responses'] ?? []))
->parameters(...($properties['parameters'] ?? []))
->examples(...($properties['examples'] ?? []))
->requestBodies(...($properties['requestBodies'] ?? []))
->headers(...($properties['headers'] ?? []))
->securitySchemes(...($properties['securitySchemes'] ?? []))
->links(...($properties['links'] ?? []))
->callbacks(...($properties['callbacks'] ?? []));
}
}
8 changes: 8 additions & 0 deletions src/Objects/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,12 @@ protected function generate(): array
'email' => $this->email,
]);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->name($properties['name'])
->url($properties['url'])
->email($properties['email']);
}
}
7 changes: 7 additions & 0 deletions src/Objects/Discriminator.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,11 @@ protected function generate(): array
'mapping' => $this->mapping,
]);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->propertyName($properties['propertyName'])
->mapping($properties['mapping']);
}
}
10 changes: 10 additions & 0 deletions src/Objects/Encoding.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,14 @@ protected function generate(): array
'allowReserved' => $this->allowReserved,
]);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->contentType($properties['contentType'])
->headers(...($properties['headers'] ?? []))
->style($properties['style'])
->explode($properties['explode'])
->allowReserved($properties['allowReserved']);
}
}
9 changes: 9 additions & 0 deletions src/Objects/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,13 @@ protected function generate(): array
'externalValue' => $this->externalValue,
]);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->summary($properties['summary'])
->description($properties['description'])
->value($properties['value'])
->externalValue($properties['externalValue']);
}
}
7 changes: 7 additions & 0 deletions src/Objects/ExternalDocs.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,11 @@ protected function generate(): array
'url' => $this->url,
]);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->description($properties['description'])
->url($properties['url']);
}
}
16 changes: 16 additions & 0 deletions src/Objects/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,20 @@ protected function generate(): array
'content' => $content ?: null,
]);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->description($properties['description'])
->required($properties['required'])
->deprecated($properties['deprecated'])
->allowEmptyValue($properties['allowEmptyValue'])
->style($properties['style'])
->explode($properties['explode'])
->allowReserved($properties['allowReserved'])
->schema($properties['schema'])
->example($properties['example'])
->examples(...($properties['examples'] ?? []))
->content(...($properties['content'] ?? []));
}
}
11 changes: 11 additions & 0 deletions src/Objects/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,15 @@ protected function generate(): array
'version' => $this->version,
]);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->title($properties['title'])
->description($properties['description'])
->termsOfService($properties['termsOfService'])
->contact($properties['contact'])
->license($properties['license'])
->version($properties['version']);
}
}
7 changes: 7 additions & 0 deletions src/Objects/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,11 @@ protected function generate(): array
'url' => $this->url,
]);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->name($properties['name'])
->url($properties['url']);
}
}
9 changes: 9 additions & 0 deletions src/Objects/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,13 @@ protected function generate(): array
'server' => $this->server,
]);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->operationRef($properties['operationRef'])
->operationId($properties['operationId'])
->description($properties['description'])
->server($properties['server']);
}
}
10 changes: 10 additions & 0 deletions src/Objects/MediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,14 @@ protected function generate(): array
'encoding' => $encodings ?: null,
]);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->mediaType($properties['mediaType'])
->schema($properties['schema'])
->example($properties['example'])
->examples(...($properties['examples'] ?? []))
->encoding(...($properties['encoding'] ?? []));
}
}
6 changes: 6 additions & 0 deletions src/Objects/Not.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ protected function generate(): array
'not' => $this->schema,
]);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->schema($properties['schema']);
}
}
20 changes: 18 additions & 2 deletions src/Objects/OAuthFlow.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ public function refreshUrl(?string $refreshUrl): self
*/
public function scopes(?array $scopes): self
{
$instance = clone $this;

if ($scopes === null) {
$instance->scopes = null;

return $instance;
}

// Ensure the scopes are string => string.
foreach ($scopes as $key => $value) {
if (is_string($key) && is_string($value)) {
Expand All @@ -114,8 +122,6 @@ public function scopes(?array $scopes): self
throw new InvalidArgumentException('Each scope must have a string key and a string value.');
}

$instance = clone $this;

$instance->scopes = $scopes;

return $instance;
Expand All @@ -133,4 +139,14 @@ protected function generate(): array
'scopes' => $this->scopes,
]);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->flow($properties['flow'])
->authorizationUrl($properties['authorizationUrl'])
->tokenUrl($properties['tokenUrl'])
->refreshUrl($properties['refreshUrl'])
->scopes($properties['scopes']);
}
}
19 changes: 19 additions & 0 deletions src/Objects/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,23 @@ protected function generate(): array
'callbacks' => $callbacks ?: null,
]);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->action($properties['action'])
->tags(...($properties['tags'] ?? []))
->summary($properties['summary'])
->description($properties['description'])
->externalDocs($properties['externalDocs'])
->operationId($properties['operationId'])
->parameters(...($properties['parameters'] ?? []))
->requestBody($properties['requestBody'])
->responses(...($properties['responses'] ?? []))
->deprecated($properties['deprecated'])
->security(...($properties['security'] ?? []))
->noSecurity($properties['noSecurity'])
->servers(...($properties['servers'] ?? []))
->callbacks(...($properties['callbacks'] ?? []));
}
}
19 changes: 19 additions & 0 deletions src/Objects/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,23 @@ protected function generate(): array
'content' => $content ?: null,
]);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->name($properties['name'])
->in($properties['in'])
->description($properties['description'])
->required($properties['required'])
->deprecated($properties['deprecated'])
->allowEmptyValue($properties['allowEmptyValue'])
->style($properties['style'])
->explode($properties['explode'])
->allowReserved($properties['allowReserved'])
->schema($properties['schema'])
->example($properties['example'])
->examples(...$properties['examples'] ?? [])
->content(...$properties['content'] ?? []);

}
}
11 changes: 11 additions & 0 deletions src/Objects/PathItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,15 @@ protected function generate(): array
])
);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->route($properties['route'])
->summary($properties['summary'])
->description($properties['description'])
->operations(...$properties['operations'] ?? [])
->servers(...$properties['servers'] ?? [])
->parameters(...$properties['parameters'] ?? []);
}
}
8 changes: 8 additions & 0 deletions src/Objects/RequestBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,12 @@ protected function generate(): array
'required' => $this->required,
]);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->description($properties['description'])
->content(...($properties['content'] ?? []))
->required($properties['required']);
}
}
10 changes: 10 additions & 0 deletions src/Objects/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,14 @@ protected function generate(): array
'links' => $links ?: null,
]);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->statusCode($properties['statusCode'])
->description($properties['description'])
->headers(...$properties['headers'] ?? [])
->content(...$properties['content'] ?? [])
->links(...$properties['links'] ?? []);
}
}
38 changes: 37 additions & 1 deletion src/Objects/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public function type(?string $type): self
* @param \GoldSpecDigital\ObjectOrientedOAS\Contracts\SchemaContract $items
* @return static
*/
public function items(SchemaContract $items): self
public function items(?SchemaContract $items): self
{
$instance = clone $this;

Expand Down Expand Up @@ -783,4 +783,40 @@ protected function generate(): array
'deprecated' => $this->deprecated,
]);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->title($properties['title'])
->description($properties['description'])
->enum(...($properties['enum'] ?? []))
->default($properties['default'])
->format($properties['format'])
->type($properties['type'])
->items($properties['items'])
->maxItems($properties['maxItems'])
->minItems($properties['minItems'])
->uniqueItems($properties['uniqueItems'])
->pattern($properties['pattern'])
->maxLength($properties['maxLength'])
->minLength($properties['minLength'])
->maximum($properties['maximum'])
->exclusiveMaximum($properties['exclusiveMaximum'])
->minimum($properties['minimum'])
->exclusiveMinimum($properties['exclusiveMinimum'])
->multipleOf($properties['multipleOf'])
->required(...($properties['required'] ?? []))
->properties(...($properties['properties'] ?? []))
->additionalProperties($properties['additionalProperties'])
->maxProperties($properties['maxProperties'])
->minProperties($properties['minProperties'])
->nullable($properties['nullable'])
->discriminator($properties['discriminator'])
->readOnly($properties['readOnly'])
->writeOnly($properties['writeOnly'])
->xml($properties['xml'])
->externalDocs($properties['externalDocs'])
->example($properties['example'])
->deprecated($properties['deprecated']);
}
}
6 changes: 6 additions & 0 deletions src/Objects/SchemaComposition.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ protected function generate(): array
$this->compositionType() => $this->schemas,
]);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->schemas(...($properties['schemas'] ?? []));
}
}
7 changes: 7 additions & 0 deletions src/Objects/SecurityRequirement.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,11 @@ protected function generate(): array
$this->securityScheme => $this->scopes ?? [],
]);
}

public static function __set_state(array $properties)
{
return parent::__set_state($properties)
->securityScheme($properties['securityScheme'])
->scopes(...($properties['scopes'] ?? []));
}
}
Loading