From 4242e669a8bf27dbaedc98cf12e8e78edf953130 Mon Sep 17 00:00:00 2001 From: chris cnizzardini Date: Wed, 11 Nov 2020 13:11:49 -0500 Subject: [PATCH] Change Schema::properties to accept both Schema and SchemaProperty --- src/Lib/OpenApi/Schema.php | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/Lib/OpenApi/Schema.php b/src/Lib/OpenApi/Schema.php index 558972be..b38d270b 100644 --- a/src/Lib/OpenApi/Schema.php +++ b/src/Lib/OpenApi/Schema.php @@ -44,7 +44,9 @@ class Schema implements JsonSerializable private $required = []; /** - * @var \SwaggerBake\Lib\OpenApi\SchemaProperty[] + * A mixed array of Schema and SchemaProperty + * + * @var array */ private $properties = []; @@ -243,7 +245,9 @@ public function pushRequired(string $propertyName) } /** - * @return \SwaggerBake\Lib\OpenApi\SchemaProperty[] + * A mixed array of Schema and SchemaProperty + * + * @return array */ public function getProperties(): array { @@ -251,7 +255,7 @@ public function getProperties(): array } /** - * @param \SwaggerBake\Lib\OpenApi\SchemaProperty[] $properties Array of SchemaProperty + * @param array $properties A mixed array of Schema and SchemaProperty * @return $this */ public function setProperties(array $properties) @@ -265,13 +269,31 @@ public function setProperties(array $properties) } /** - * @param \SwaggerBake\Lib\OpenApi\SchemaProperty $property SchemaProperty + * @param mixed $property instance of SchemaProperty or Schema * @return $this */ - public function pushProperty(SchemaProperty $property) + public function pushProperty($property) { + if (!$property instanceof SchemaProperty && !$property instanceof Schema) { + throw new \InvalidArgumentException( + 'Must be instance of SchemaProperty or Schema' + ); + } + $this->properties[$property->getName()] = $property; + if (empty($property->getName())) { + throw new \LogicException( + 'Name must be set on ' . get_class($property) + ); + } + + if ($property instanceof Schema) { + $this->properties[$property->getName()] = $property; + + return $this; + } + if ($property->isRequired()) { $this->required[$property->getName()] = $property->getName(); } elseif (isset($this->required[$property->getName()])) {