Skip to content
This repository was archived by the owner on Jan 20, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ build
vendor
.php_cs.cache
composer.lock
composer.phar
59 changes: 57 additions & 2 deletions src/NamedParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class NamedParameter implements ArrayInstantiationInterface
const TYPE_DATE = 'date';
const TYPE_BOOLEAN = 'boolean';
const TYPE_FILE = 'file';
const TYPE_DATE_ONLY = 'date-only';
const TYPE_TIME_ONLY = 'time-only';
const TYPE_DATETIME_ONLY = 'datetime-only';
const TYPE_DATETIME = 'datetime';
const TYPE_ARRAY = 'array';

// Validation exception codes
Expand Down Expand Up @@ -46,6 +50,10 @@ class NamedParameter implements ArrayInstantiationInterface
self::TYPE_DATE,
self::TYPE_BOOLEAN,
self::TYPE_FILE,
self::TYPE_DATETIME_ONLY,
self::TYPE_DATE_ONLY,
self::TYPE_TIME_ONLY,
self::TYPE_DATETIME,
self::TYPE_ARRAY,
];

Expand Down Expand Up @@ -175,12 +183,19 @@ class NamedParameter implements ArrayInstantiationInterface
*/
private $default;

/**
* DateTime format (for datetime type only)
*
* @var string
*/
private $format;

// ---

/**
* Create a new Query Parameter
*
* @param string $key
* @param string $key
*/
public function __construct($key)
{
Expand All @@ -207,7 +222,7 @@ public function getKey()
* [
* displayName: ?string
* description: ?string
* type: ?["string","number","integer","date","boolean","file"]
* type: ?["string","number","integer","date","boolean","file", ...]
* enum: ?array
* pattern: ?string
* validationPattern: ?string
Expand All @@ -220,6 +235,7 @@ public function getKey()
* repeat: ?boolean
* required: ?boolean
* default: ?string
* format: ?string
* ]
*
* @throws \Exception
Expand Down Expand Up @@ -297,6 +313,10 @@ public static function createFromArray($key, array $data = [])
}
}

if (isset($data['format'])) {
$namedParameter->setFormat($data['format']);
}

return $namedParameter;
}

Expand Down Expand Up @@ -618,6 +638,22 @@ public function getDefault()
return $this->default;
}

/**
* @return string
*/
public function getFormat()
{
return $this->format;
}

/**
* @param string $format
*/
public function setFormat($format)
{
$this->format = $format;
}

/**
* Set the default
*
Expand Down Expand Up @@ -705,6 +741,7 @@ public function validate($param)
}

// no break

case static::TYPE_STRING:

// Must be a string
Expand Down Expand Up @@ -794,6 +831,24 @@ public function validate($param)
case static::TYPE_FILE:
// File type cannot be reliably validated based on its type alone.

break;

case static::TYPE_TIME_ONLY:
case static::TYPE_DATE_ONLY:
case static::TYPE_DATETIME_ONLY:
case static::TYPE_DATETIME:
$typeObject = ApiDefinition::determineType(
$this->key,
[
'type' => $this->getType(),
'format' => $this->getFormat(),
]
);
$typeObject->validate($param);
if (!$typeObject->isValid()) {
throw new ValidationException($typeObject->getErrors()[0]);
}

break;
}

Expand Down
16 changes: 14 additions & 2 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,19 @@ private function addNamespacePrefix($nameSpace, array $definition)
}
} else {
if (!in_array($item, ApiDefinition::getStraightForwardTypes(), true)) {
$definition[$key] = $nameSpace . '.' . $item;
if (mb_strpos($item, '|') !== false) {
$definition[$key] = implode(
'|',
array_map(
function ($v) use ($nameSpace) {
return $nameSpace . '.' . trim($v);
},
explode('|', $item)
)
);
} else {
$definition[$key] = $nameSpace . '.' . $item;
}
}
}
} elseif (is_array($definition[$key])) {
Expand All @@ -518,7 +530,7 @@ private function addNamespacePrefix($nameSpace, array $definition)
/**
* Parse the traits
*
* @param mixed $ramlData
* @param array $ramlData
*
* @return array
*/
Expand Down
54 changes: 13 additions & 41 deletions src/Types/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Raml\Types;

use Raml\ApiDefinition;
use Raml\Type;
use Raml\TypeCollection;
use Raml\TypeInterface;
Expand All @@ -21,6 +22,9 @@ class ArrayType extends Type
'string',
'boolean',
'number',
'datetime-only',
'date-only',
'time-only',
];

/**
Expand Down Expand Up @@ -175,48 +179,16 @@ public function validate($value)

private function validateScalars($value)
{
foreach ($value as $valueItem) {
switch ($this->items) {
case 'integer':
if (!is_int($valueItem)) {
$this->errors[] = TypeValidationError::unexpectedArrayValueType(
$this->getName(),
'integer',
$valueItem
);
}
$typeObject = ApiDefinition::determineType('item', ['type' => $this->items]);

break;
case 'string':
if (!is_string($valueItem)) {
$this->errors[] = TypeValidationError::unexpectedArrayValueType(
$this->getName(),
'string',
$valueItem
);
}

break;
case 'boolean':
if (!is_bool($valueItem)) {
$this->errors[] = TypeValidationError::unexpectedArrayValueType(
$this->getName(),
'boolean',
$valueItem
);
}

break;
case 'number':
if (!is_float($valueItem) && !is_int($valueItem)) {
$this->errors[] = TypeValidationError::unexpectedArrayValueType(
$this->getName(),
'number',
$valueItem
);
}

break;
foreach ($value as $valueItem) {
$typeObject->validate($valueItem);
if (!$typeObject->isValid()) {
$this->errors[] = TypeValidationError::unexpectedArrayValueType(
$this->getName(),
$this->items,
$valueItem
);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Types/DatetimeOnlyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class DatetimeOnlyType extends Type
/**
* Create a new DateTimeOnlyType from an array of data
*
* @param string $name
* @param array $data
* @param string $name
* @param array $data
*
* @return self
*/
Expand Down
15 changes: 10 additions & 5 deletions src/Types/DatetimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
class DatetimeType extends Type
{
const DEFAULT_FORMAT = DATE_RFC3339;
/**
* DateTime format to use
*
Expand All @@ -20,8 +21,8 @@ class DatetimeType extends Type
/**
* Create a new DatetimeType from an array of data
*
* @param string $name
* @param array $data
* @param string $name
* @param array $data
*
* @return DatetimeType
*/
Expand Down Expand Up @@ -70,11 +71,15 @@ public function validate($value)
{
parent::validate($value);

$format = $this->format ?: DATE_RFC3339;
$format = $this->format ?: self::DEFAULT_FORMAT;
$d = DateTime::createFromFormat($format, $value);

if ($d && $d->format($format) !== $value) {
$this->errors[] = TypeValidationError::unexpectedValueType($this->getName(), 'datetime', $value);
if (!$d || $d->format($format) !== $value) {
$this->errors[] = TypeValidationError::unexpectedValueType(
$this->getName(),
'datetime with format ' . $this->format,
$value
);
}
}
}
4 changes: 2 additions & 2 deletions src/Types/TimeOnlyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public function validate($value)

$d = DateTime::createFromFormat(self::FORMAT, $value);

if ($d && $d->format(self::FORMAT) !== $value) {
$this->errors[] = TypeValidationError::unexpectedValueType($this->getName(), self::FORMAT, $value);
if (!$d || $d->format(self::FORMAT) !== $value) {
$this->errors[] = TypeValidationError::unexpectedValueType($this->getName(), 'time-only', $value);
}
}
}
Loading