Skip to content

Commit

Permalink
Change Schema::properties to accept both Schema and SchemaProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
cnizzardini committed Nov 11, 2020
1 parent bfb23e3 commit 4242e66
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/Lib/OpenApi/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down Expand Up @@ -243,15 +245,17 @@ public function pushRequired(string $propertyName)
}

/**
* @return \SwaggerBake\Lib\OpenApi\SchemaProperty[]
* A mixed array of Schema and SchemaProperty
*
* @return array
*/
public function getProperties(): array
{
return $this->properties;
}

/**
* @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)
Expand All @@ -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()])) {
Expand Down

0 comments on commit 4242e66

Please sign in to comment.