Skip to content

Commit

Permalink
Provide PHPDoc comments and types
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Jul 5, 2020
1 parent a233e6f commit 8f9ef9b
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 73 deletions.
139 changes: 75 additions & 64 deletions src/Base/Body.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ abstract class Body
*/
protected $schema;

/**
* @var array
*/
protected $structure;

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

/**
Expand Down Expand Up @@ -56,8 +62,8 @@ public function __construct(Schema $schema, $name, $structure, $allowNullValues

/**
* @param Schema $schema
* @param $name
* @param $structure
* @param string $name
* @param array $structure
* @param bool $allowNullValues
* @return OpenApiResponseBody|SwaggerResponseBody
* @throws GenericSwaggerException
Expand All @@ -78,35 +84,34 @@ public static function getInstance(Schema $schema, $name, $structure, $allowNull
abstract public function match($body);

/**
* @param $name
* @param $schema
* @param $body
* @param $type
* @param string $name
* @param array $schemaArray
* @param string $body
* @param string $type
* @return bool
* @throws NotMatchedException
*/
protected function matchString($name, $schema, $body, $type)
protected function matchString($name, $schemaArray, $body, $type)
{
if ($type !== 'string') {
return null;
}

if (isset($schema['enum']) && !in_array($body, $schema['enum'])) {
if (isset($schemaArray['enum']) && !in_array($body, $schemaArray['enum'])) {
throw new NotMatchedException("Value '$body' in '$name' not matched in ENUM. ", $this->structure);
}

return true;
}

/**
* @param $name
* @param $schema
* @param $body
* @param $type
* @param string $name
* @param array $schemaArray
* @param string $body
* @param string $type
* @return bool
* @throws NotMatchedException
*/
protected function matchFile($name, $schema, $body, $type)
protected function matchFile($name, $schemaArray, $body, $type)
{
if ($type !== 'file') {
return null;
Expand All @@ -116,9 +121,9 @@ protected function matchFile($name, $schema, $body, $type)
}

/**
* @param $name
* @param $body
* @param $type
* @param string $name
* @param string $body
* @param string $type
* @return bool
* @throws NotMatchedException
*/
Expand All @@ -136,9 +141,9 @@ protected function matchNumber($name, $body, $type)
}

/**
* @param $name
* @param $body
* @param $type
* @param string $name
* @param string $body
* @param string $type
* @return bool
* @throws NotMatchedException
*/
Expand All @@ -156,50 +161,56 @@ protected function matchBool($name, $body, $type)
}

/**
* @param $name
* @param $schema
* @param $body
* @param $type
* @param string $name
* @param array $schemaArray
* @param string $body
* @param string $type
* @return bool
* @throws DefinitionNotFoundException
* @throws GenericSwaggerException
* @throws InvalidDefinitionException
* @throws InvalidRequestException
* @throws NotMatchedException
*/
protected function matchArray($name, $schema, $body, $type)
protected function matchArray($name, $schemaArray, $body, $type)
{
if ($type !== 'array') {
return null;
}

foreach ((array)$body as $item) {
if (!isset($schema['items'])) { // If there is no type , there is no test.
if (!isset($schemaArray['items'])) { // If there is no type , there is no test.
continue;
}
$this->matchSchema($name, $schema['items'], $item);
$this->matchSchema($name, $schemaArray['items'], $item);
}
return true;
}

protected function matchTypes($name, $schema, $body)
/**
* @param string $name
* @param array $schemaArray
* @param string $body
* @return mixed|null
*/
protected function matchTypes($name, $schemaArray, $body)
{
if (!isset($schema['type'])) {
if (!isset($schemaArray['type'])) {
return null;
}

$type = $schema['type'];
$nullable = isset($schema['nullable']) ? (bool)$schema['nullable'] : $this->schema->isAllowNullValues();
$type = $schemaArray['type'];
$nullable = isset($schemaArray['nullable']) ? (bool)$schemaArray['nullable'] : $this->schema->isAllowNullValues();

$validators = [
function () use ($name, $body, $type, $nullable)
{
return $this->matchNull($name, $body, $type, $nullable);
},

function () use ($name, $schema, $body, $type)
function () use ($name, $schemaArray, $body, $type)
{
return $this->matchString($name, $schema, $body, $type);
return $this->matchString($name, $schemaArray, $body, $type);
},

function () use ($name, $body, $type)
Expand All @@ -212,14 +223,14 @@ function () use ($name, $body, $type)
return $this->matchBool($name, $body, $type);
},

function () use ($name, $schema, $body, $type)
function () use ($name, $schemaArray, $body, $type)
{
return $this->matchArray($name, $schema, $body, $type);
return $this->matchArray($name, $schemaArray, $body, $type);
},

function () use ($name, $schema, $body, $type)
function () use ($name, $schemaArray, $body, $type)
{
return $this->matchFile($name, $schema, $body, $type);
return $this->matchFile($name, $schemaArray, $body, $type);
},
];

Expand All @@ -234,19 +245,19 @@ function () use ($name, $schema, $body, $type)
}

/**
* @param $name
* @param $schema
* @param $body
* @param string $name
* @param array $schemaArray
* @param string $body
* @return bool|null
* @throws DefinitionNotFoundException
* @throws GenericSwaggerException
* @throws InvalidDefinitionException
* @throws InvalidRequestException
* @throws NotMatchedException
*/
public function matchObjectProperties($name, $schema, $body)
public function matchObjectProperties($name, $schemaArray, $body)
{
if (!isset($schema[self::SWAGGER_PROPERTIES])) {
if (!isset($schemaArray[self::SWAGGER_PROPERTIES])) {
return null;
}

Expand All @@ -257,11 +268,11 @@ public function matchObjectProperties($name, $schema, $body)
);
}

if (!isset($schema[self::SWAGGER_REQUIRED])) {
$schema[self::SWAGGER_REQUIRED] = [];
if (!isset($schemaArray[self::SWAGGER_REQUIRED])) {
$schemaArray[self::SWAGGER_REQUIRED] = [];
}
foreach ($schema[self::SWAGGER_PROPERTIES] as $prop => $def) {
$required = array_search($prop, $schema[self::SWAGGER_REQUIRED]);
foreach ($schemaArray[self::SWAGGER_PROPERTIES] as $prop => $def) {
$required = array_search($prop, $schemaArray[self::SWAGGER_REQUIRED]);

if (!array_key_exists($prop, $body)) {
if ($required !== false) {
Expand All @@ -272,17 +283,17 @@ public function matchObjectProperties($name, $schema, $body)
}

$this->matchSchema($prop, $def, $body[$prop]);
unset($schema[self::SWAGGER_PROPERTIES][$prop]);
unset($schemaArray[self::SWAGGER_PROPERTIES][$prop]);
if ($required !== false) {
unset($schema[self::SWAGGER_REQUIRED][$required]);
unset($schemaArray[self::SWAGGER_REQUIRED][$required]);
}
unset($body[$prop]);
}

if (count($schema[self::SWAGGER_REQUIRED]) > 0) {
if (count($schemaArray[self::SWAGGER_REQUIRED]) > 0) {
throw new NotMatchedException(
"The required property(ies) '"
. implode(', ', $schema[self::SWAGGER_REQUIRED])
. implode(', ', $schemaArray[self::SWAGGER_REQUIRED])
. "' does not exists in the body.",
$this->structure
);
Expand All @@ -301,7 +312,7 @@ public function matchObjectProperties($name, $schema, $body)

/**
* @param string $name
* @param $schema
* @param array $schemaArray
* @param array $body
* @return bool
* @throws DefinitionNotFoundException
Expand All @@ -310,25 +321,25 @@ public function matchObjectProperties($name, $schema, $body)
* @throws InvalidRequestException
* @throws NotMatchedException
*/
protected function matchSchema($name, $schema, $body)
protected function matchSchema($name, $schemaArray, $body)
{
// Match Single Types
if ($this->matchTypes($name, $schema, $body)) {
if ($this->matchTypes($name, $schemaArray, $body)) {
return true;
}

if(!isset($schema['$ref']) && isset($schema['content'])) {
$schema['$ref'] = $schema['content'][key($schema['content'])]['schema']['$ref'];
if(!isset($schemaArray['$ref']) && isset($schemaArray['content'])) {
$schemaArray['$ref'] = $schemaArray['content'][key($schemaArray['content'])]['schema']['$ref'];
}

// Get References and try to match it again
if (isset($schema['$ref'])) {
$defintion = $this->schema->getDefinition($schema['$ref']);
return $this->matchSchema($schema['$ref'], $defintion, $body);
if (isset($schemaArray['$ref'])) {
$defintion = $this->schema->getDefinition($schemaArray['$ref']);
return $this->matchSchema($schemaArray['$ref'], $defintion, $body);
}

// Match object properties
if ($this->matchObjectProperties($name, $schema, $body)) {
if ($this->matchObjectProperties($name, $schemaArray, $body)) {
return true;
}

Expand All @@ -338,18 +349,18 @@ protected function matchSchema($name, $schema, $body)
* To make that hack works, we need such condition
* @link https://stackoverflow.com/questions/32841298/swagger-2-0-what-schema-to-accept-any-complex-json-value
*/
if ($schema === []) {
if ($schemaArray === []) {
return true;
}

throw new GenericSwaggerException("Not all cases are defined. Please open an issue about this. Schema: $name");
}

/**
* @param $name
* @param $body
* @param $type
* @param $nullable
* @param string $name
* @param string $body
* @param string $type
* @param bool $nullable
* @return bool
* @throws NotMatchedException
*/
Expand Down
10 changes: 5 additions & 5 deletions src/Base/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public static function getInstance($data, $extraArgs = false)
}

/**
* @param $path
* @param $method
* @param string $path
* @param string $method
* @return mixed
* @throws DefinitionNotFoundException
* @throws HttpMethodNotFoundException
Expand Down Expand Up @@ -125,9 +125,9 @@ public function getPathDefinition($path, $method)
}

/**
* @param $path
* @param $method
* @param $status
* @param string $path
* @param string $method
* @param string $status
* @return Body
* @throws DefinitionNotFoundException
* @throws HttpMethodNotFoundException
Expand Down
2 changes: 1 addition & 1 deletion src/OpenApi/OpenApiRequestBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class OpenApiRequestBody extends Body
{
/**
* @param $body
* @param string $body
* @return bool
* @throws GenericSwaggerException
* @throws InvalidDefinitionException
Expand Down
2 changes: 1 addition & 1 deletion src/OpenApi/OpenApiResponseBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class OpenApiResponseBody extends Body
{
/**
* @param $body
* @param string $body
* @return bool
* @throws GenericSwaggerException
* @throws InvalidRequestException
Expand Down
2 changes: 1 addition & 1 deletion src/Swagger/SwaggerRequestBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class SwaggerRequestBody extends Body
{
/**
* @param $body
* @param string $body
* @return bool
* @throws GenericSwaggerException
* @throws InvalidDefinitionException
Expand Down
2 changes: 1 addition & 1 deletion src/Swagger/SwaggerResponseBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class SwaggerResponseBody extends Body
{
/**
* @param $body
* @param string $body
* @return bool
* @throws GenericSwaggerException
* @throws InvalidRequestException
Expand Down

0 comments on commit 8f9ef9b

Please sign in to comment.