Skip to content

Commit

Permalink
Fix codestyle
Browse files Browse the repository at this point in the history
  • Loading branch information
= authored and scaytrase committed Feb 18, 2023
1 parent 4634ab6 commit 85257b8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
8 changes: 5 additions & 3 deletions src/Schema/Exception/TypeMismatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
namespace League\OpenAPIValidation\Schema\Exception;

use function gettype;
use function implode;
use function sprintf;

// Validation for 'type' keyword failed against a given data
class TypeMismatch extends KeywordMismatch
{
/**
* @param mixed $value
* @param string[] $expected
* @param mixed $value
*
* @return TypeMismatch
*/
public static function becauseTypeDoesNotMatch(array $expected, $value): self
{
$exception = new self(sprintf("Value expected to be '%s', but '%s' given.", implode(', ', $expected), gettype($value)));
$exception->data = $value;
$exception = new self(sprintf("Value expected to be '%s', but '%s' given.", implode(', ', $expected), gettype($value)));
$exception->data = $value;
$exception->keyword = 'type';

return $exception;
Expand Down
10 changes: 5 additions & 5 deletions src/Schema/Keywords/Nullable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use League\OpenAPIValidation\Schema\Exception\KeywordMismatch;

use function in_array;
use function is_string;

class Nullable extends BaseKeyword
{
/**
Expand All @@ -17,16 +20,13 @@ class Nullable extends BaseKeyword
*/
public function validate($data, bool $nullable): void
{
if (! $nullable && ($data === null) && !$this->nullableByType()) {
if (! $nullable && ($data === null) && ! $this->nullableByType()) {
throw KeywordMismatch::fromKeyword('nullable', $data, 'Value cannot be null');
}
}

/**
* @return bool
*/
public function nullableByType(): bool
{
return !is_string($this->parentSchema->type) && in_array('null', $this->parentSchema->type);
return ! is_string($this->parentSchema->type) && in_array('null', $this->parentSchema->type);
}
}
23 changes: 17 additions & 6 deletions src/Schema/Keywords/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use League\OpenAPIValidation\Schema\Exception\TypeMismatch;
use League\OpenAPIValidation\Schema\TypeFormats\FormatsContainer;
use RuntimeException;
use TypeError;

use function class_exists;
use function is_array;
Expand All @@ -32,68 +33,78 @@ class Type extends BaseKeyword
* An instance matches successfully if its primitive type is one of the
* types defined by keyword. Recall: "number" includes "integer".
*
* @param mixed $data
* @param string|array $types
* @param mixed $data
* @param string|string[] $types
*
* @throws TypeMismatch
*/
public function validate($data, $types, ?string $format = null): void
{
if (!is_array($types) && !is_string($types)) {
throw new \TypeError('$types only can be array or string');
if (! is_array($types) && ! is_string($types)) {
throw new TypeError('$types only can be array or string');
}
if (!is_array($types)) {

if (! is_array($types)) {
$types = [$types];
}

$matchedType = false;
foreach ($types as $type) {
switch ($type) {
case CebeType::OBJECT:
if (! is_object($data) && ! (is_array($data) && ArrayHelper::isAssoc($data)) && $data !== []) {
break;
}

$matchedType = $type;
break;
case CebeType::ARRAY:
if (! is_array($data) || ArrayHelper::isAssoc($data)) {
break;
}

$matchedType = $type;
break;
case CebeType::BOOLEAN:
if (! is_bool($data)) {
break;
}

$matchedType = $type;
break;
case CebeType::NUMBER:
if (is_string($data) || ! is_numeric($data)) {
break;
}

$matchedType = $type;
break;
case CebeType::INTEGER:
if (! is_int($data)) {
break;
}

$matchedType = $type;
break;
case CebeType::STRING:
if (! is_string($data)) {
break;
}

$matchedType = $type;
break;
case CebeType::NULL:
if (! is_null($data)) {
if ($data !== null) {
break;
}

$matchedType = $type;
break;
default:
throw InvalidSchema::becauseTypeIsNotKnown($type);
}
}

if ($matchedType === false) {
throw TypeMismatch::becauseTypeDoesNotMatch($types, $data);
}
Expand Down

0 comments on commit 85257b8

Please sign in to comment.