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
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
19 changes: 11 additions & 8 deletions src/ApiDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@
*/
class ApiDefinition implements ArrayInstantiationInterface
{
/* public */ const PROTOCOL_HTTP = 'HTTP';
/* public */ const PROTOCOL_HTTPS = 'HTTPS';
/**
* @var string
*/
const PROTOCOL_HTTP = 'HTTP';

/**
* @var string
*/
const PROTOCOL_HTTPS = 'HTTPS';

/**
* The API Title (required)
Expand Down Expand Up @@ -365,7 +372,7 @@ public function getResourceByPath($path)
*/
public function getResourcesAsUri(RouteFormatterInterface $formatter = null)
{
if (!$formatter) {
if ($formatter === null) {
$formatter = new NoRouteFormatter();
}

Expand Down Expand Up @@ -818,10 +825,6 @@ private function setProtocolsFromBaseUri()
{
$schema = strtoupper(parse_url($this->baseUri, PHP_URL_SCHEME));

if (empty($schema)) {
$this->protocols = [self::PROTOCOL_HTTPS, self::PROTOCOL_HTTP];
} else {
$this->protocols = [$schema];
}
$this->protocols = empty($schema) ? [self::PROTOCOL_HTTPS, self::PROTOCOL_HTTP] : [$schema];
}
}
10 changes: 3 additions & 7 deletions src/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,7 @@ public static function createFromArray($method, array $data = [], ApiDefinition
if (isset($data['body'])) {
foreach ($data['body'] as $key => $bodyData) {
if (is_array($bodyData)) {
if (in_array($key, WebFormBody::$validMediaTypes, true)) {
$body = WebFormBody::createFromArray($key, $bodyData);
} else {
$body = Body::createFromArray($key, $bodyData);
}
$body = in_array($key, WebFormBody::$validMediaTypes, true) ? WebFormBody::createFromArray($key, $bodyData) : Body::createFromArray($key, $bodyData);

$method->addBody($body);
}
Expand Down Expand Up @@ -346,7 +342,7 @@ public function addProtocol($protocol)
throw new \InvalidArgumentException(sprintf('"%s" is not a valid protocol', $protocol));
}

if (in_array($protocol, $this->protocols, true) === false) {
if (!in_array($protocol, $this->protocols, true)) {
$this->protocols[] = $protocol;
}
}
Expand Down Expand Up @@ -500,7 +496,7 @@ public function addSecurityScheme(SecurityScheme $securityScheme, $merge = true)
assert($body instanceof WebFormBody);
$params = $body->getParameters();

foreach ($params as $parameterName => $namedParameter) {
foreach ($params as $namedParameter) {
$body->addParameter($namedParameter);
}
}
Expand Down
80 changes: 74 additions & 6 deletions src/NamedParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,103 @@
class NamedParameter implements ArrayInstantiationInterface
{
// Type constants
/**
* @var string
*/
const TYPE_STRING = 'string';
/**
* @var string
*/
const TYPE_NUMBER = 'number';
/**
* @var string
*/
const TYPE_INTEGER = 'integer';
/**
* @var string
*/
const TYPE_DATE = 'date';
/**
* @var string
*/
const TYPE_BOOLEAN = 'boolean';
/**
* @var string
*/
const TYPE_FILE = 'file';
/**
* @var string
*/
const TYPE_DATE_ONLY = 'date-only';
/**
* @var string
*/
const TYPE_TIME_ONLY = 'time-only';
/**
* @var string
*/
const TYPE_DATETIME_ONLY = 'datetime-only';
/**
* @var string
*/
const TYPE_DATETIME = 'datetime';
/**
* @var string
*/
const TYPE_ARRAY = 'array';

// Validation exception codes
/**
* @var int
*/
const VAL_NOTBOOLEAN = 1;
/**
* @var int
*/
const VAL_NOTDATE = 2;
/**
* @var int
*/
const VAL_NOTSTRING = 3;
/**
* @var int
*/
const VAL_NOTINT = 4;
/**
* @var int
*/
const VAL_NOTNUMBER = 5;
/**
* @var int
*/
const VAL_NOTFILE = 6; // Unused
/**
* @var int
*/
const VAL_ISREQUIRED = 7;
/**
* @var int
*/
const VAL_TOOSHORT = 8;
/**
* @var int
*/
const VAL_TOOLONG = 9;
/**
* @var int
*/
const VAL_NUMLESSTHAN = 10;
/**
* @var int
*/
const VAL_GREATERTHAN = 11;
/**
* @var int
*/
const VAL_PATTERNFAIL = 12;
/**
* @var int
*/
const VAL_NOTENUMVALUE = 13;

/**
Expand Down Expand Up @@ -736,7 +808,7 @@ public function validate($param)
case static::TYPE_DATE:

// Must be a valid date
if (\DateTime::createFromFormat('D, d M Y H:i:s T', $param) === false) {
if (!\DateTime::createFromFormat('D, d M Y H:i:s T', $param)) {
throw new ValidationException($this->getKey() . ' is not a valid date', static::VAL_NOTDATE);
}

Expand Down Expand Up @@ -927,11 +999,7 @@ public function getMatchPattern()

break;
case self::TYPE_STRING:
if ($this->getMinLength() || $this->getMaxLength()) {
$pattern = '((?!\/).){' . $this->getMinLength() . ',' . $this->getMaxLength() . '}';
} else {
$pattern = '([^/]+)';
}
$pattern = $this->getMinLength() || $this->getMaxLength() ? '((?!\/).){' . $this->getMinLength() . ',' . $this->getMaxLength() . '}' : '([^/]+)';

break;
default:
Expand Down
51 changes: 17 additions & 34 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ private function replaceSchemas($array, $schemas)
*/
private function recurseAndParseSchemas(array $array, $rootDir)
{
foreach ($array as $key => &$value) {
foreach ($array as &$value) {
if (is_array($value)) {
if (isset($value['schema'])) {
$fileDir = $this->getCachedFilePath($value['schema']);
Expand Down Expand Up @@ -385,12 +385,7 @@ private function parseSecuritySettings($schemesArray)
$securitySchemes = [];

foreach ($schemesArray as $key => $securitySchemeData) {
// Create the default parser.
if (isset($this->securitySettingsParsers['*'])) {
$parser = $this->securitySettingsParsers['*'];
} else {
$parser = false;
}
$parser = isset($this->securitySettingsParsers['*']) ? $this->securitySettingsParsers['*'] : false;

$securitySchemes[$key] = $securitySchemeData;
$securityScheme = $securitySchemes[$key];
Expand Down Expand Up @@ -431,8 +426,8 @@ private function parseResourceTypes($ramlData)
}

foreach ($ramlData as $key => $value) {
if (strpos($key, '/') === 0) {
$name = (isset($value['displayName'])) ? $value['displayName'] : substr($key, 1);
if (mb_strpos($key, '/') === 0) {
$name = (isset($value['displayName'])) ? $value['displayName'] : mb_substr($key, 1);
$ramlData[$key] = $this->replaceTypes($value, $keyedResourceTypes, $key, $name, $key);
}
}
Expand Down Expand Up @@ -504,19 +499,15 @@ private function addNamespacePrefix($nameSpace, array $definition)
}
} else {
if (!in_array($item, ApiDefinition::getStraightForwardTypes(), true)) {
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;
}
$definition[$key] = mb_strpos($item, '|') !== false ? implode(
'|',
array_map(
function ($v) use ($nameSpace) {
return $nameSpace . '.' . trim($v);
},
explode('|', $item)
)
) : $nameSpace . '.' . $item;
}
}
} elseif (is_array($definition[$key])) {
Expand Down Expand Up @@ -624,12 +615,12 @@ private function loadAndParseFile($fileName, $rootDir)
$rootDir = realpath($rootDir);
$fullPath = realpath($rootDir . '/' . $fileName);

if (is_readable($fullPath) === false) {
if (!is_readable($fullPath)) {
throw new FileNotFoundException($fileName);
}
} else {
$fullPath = $rootDir . '/' . $fileName;
if (filter_var($fullPath, FILTER_VALIDATE_URL) === false && is_readable($fullPath) === false) {
if (!filter_var($fullPath, FILTER_VALIDATE_URL) && !is_readable($fullPath)) {
throw new FileNotFoundException($fileName);
}
}
Expand Down Expand Up @@ -660,11 +651,7 @@ private function loadAndParseFile($fileName, $rootDir)
);
$fileData = $this->includeAndParseFiles($fileData, $rootDir);
} else {
if (in_array($fileExtension, array_keys($this->fileLoaders), true)) {
$loader = $this->fileLoaders[$fileExtension];
} else {
$loader = $this->fileLoaders['*'];
}
$loader = array_key_exists($fileExtension, $this->fileLoaders) ? $this->fileLoaders[$fileExtension] : $this->fileLoaders['*'];

$fileData = $loader->loadFile($fullPath);
$this->cachedFilesPaths[md5($fileData)] = $fullPath;
Expand Down Expand Up @@ -800,11 +787,7 @@ private function replaceTypes($raml, $types, $path, $name, $parentKey = null)
}
$newValue = $this->replaceTypes($value, $types, $path, $newName, $key);

if (isset($newArray[$key]) && is_array($newArray[$key])) {
$newArray[$key] = array_replace_recursive($newArray[$key], $newValue);
} else {
$newArray[$key] = $newValue;
}
$newArray[$key] = isset($newArray[$key]) && is_array($newArray[$key]) ? array_replace_recursive($newArray[$key], $newValue) : $newValue;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Response implements ArrayInstantiationInterface, MessageSchemaInterface
*/
public function __construct($statusCode)
{
$this->statusCode = (int) $statusCode;
$this->statusCode = $statusCode;
$this->bodyList = [];
$this->headers = [];
}
Expand Down
26 changes: 1 addition & 25 deletions src/SecurityScheme/SecuritySchemeDescribedBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@
*/
class SecuritySchemeDescribedBy implements ArrayInstantiationInterface
{
/**
* The key of the security scheme
*
* @var string
*/
private $key;

/**
* A list of non default headers (optional)
*
Expand All @@ -29,7 +22,6 @@ class SecuritySchemeDescribedBy implements ArrayInstantiationInterface
* @var NamedParameter[]
*/
private $headers = [];

/**
* List of query parameters supported by this method
*
Expand All @@ -38,31 +30,19 @@ class SecuritySchemeDescribedBy implements ArrayInstantiationInterface
* @var NamedParameter[]
*/
private $queryParameters = [];

/**
* A list of possible responses from this method
*
* @var Response[]
*/
private $responses = [];

/**
* A list of the bodies of this method
*
* @var BodyInterface[]
*/
private $bodyList = [];

/**
* Create a new security scheme description
*
* @param string $key
*/
public function __construct($key)
{
$this->key = $key;
}

/**
* Create a new SecuritySchemeDescribedBy from an array of data
*
Expand All @@ -82,11 +62,7 @@ public static function createFromArray($key, array $data = [])

if (isset($data['body'])) {
foreach ($data['body'] as $key => $bodyData) {
if (in_array($key, \Raml\WebFormBody::$validMediaTypes, true)) {
$body = \Raml\WebFormBody::createFromArray($key, $bodyData);
} else {
$body = \Raml\Body::createFromArray($key, $bodyData);
}
$body = in_array($key, \Raml\WebFormBody::$validMediaTypes, true) ? \Raml\WebFormBody::createFromArray($key, $bodyData) : \Raml\Body::createFromArray($key, $bodyData);

$describedBy->addBody($body);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class DefaultSecuritySettings implements SecuritySettingsInterface, \ArrayAccess
{
/**
* Supports all types
*
* @var string
*/
const TYPE = '*';

Expand All @@ -21,7 +23,7 @@ class DefaultSecuritySettings implements SecuritySettingsInterface, \ArrayAccess
/**
* Flesh out the settings
*
* @param array $data
* @param array $data
* @param SecuritySettingsInterface $sourceSettings
*
* @throws \Exception
Expand Down
Loading