public function __construct(array $values = array()) { parent::__construct($values); if ($this->paramType && !in_array($this->paramType, array('path', 'query', 'body', 'header', 'form'))) { Logger::warning('Unexpected paramType "' . $this->paramType . '", expecting "path", "query", "body", "header" or "form" in ' . $this->_context); } }
public function validate() { if (empty($this->scope)) { Logger::warning('Required field "scope" is missing for "' . $this->identity() . '" in ' . $this->_context); return false; } return true; }
public function validate() { if (in_array($this->type, array('basicAuth', 'apiKey', 'oauth2')) === false) { Logger::warning('Unexpected ' . $this->identity() . '->type "' . $this->type . '", expection "basicAuth", "apiKey" or "oauth2" in ' . $this->_context); return false; } if ($this->type === 'apiKey' && (empty($this->passAs) || empty($this->keyname))) { Logger::notice('Fields "passAs" and "keyname" are required for ' . $this->identity() . '->type "apiKey" in ' . $this->_context); } return true; }
/** * * @param Property|Parameter|Operation $annotation * @return bool */ public static function validateContainer($annotation) { // Interpret `items="$ref:Model"` as `@SWG\Items(type="Model")` if (is_string($annotation->items) && preg_match('/\\$ref:[\\s]*(.+)[\\s]*$/', $annotation->items, $matches)) { $annotation->items = new Items(); $annotation->items->type = array_pop($matches); } // Validate if items are inside a container type. if ($annotation->items !== null) { if ($annotation->type !== 'array') { Logger::warning('Unexcepted items for type "' . $annotation->type . '" in ' . $annotation->identity() . ', expecting "array"'); $annotation->items = null; } else { Swagger::checkDataType($annotation->items->type, $annotation->items->_context); } } return true; }
/** * * @param Context $context * @return AbstractAnnotation[] */ protected function parseContext($context) { try { self::$context = $context; $annotations = $this->docParser->parse($context->comment, $context); self::$context = null; } catch (\Exception $e) { self::$context = null; if (preg_match('/^(.+) at position ([0-9]+) in ' . preg_quote($context, '/') . '\\.$/', $e->getMessage(), $matches)) { $errorMessage = $matches[1]; $errorPos = $matches[2]; $atPos = strpos($context->comment, '@'); $context->line += substr_count($context->comment, "\n", 0, $atPos + $errorPos); $lines = explode("\n", substr($context->comment, $atPos, $errorPos)); $context->character = strlen(array_pop($lines)) + 1; // position starts at 0 character starts at 1 Logger::warning(new \Exception($errorMessage . ' in ' . $context, $e->getCode(), $e)); } else { Logger::warning($e); } return array(); } foreach ($annotations as $annotation) { foreach ($this->processors as $processor) { $processor->process($annotation, $context); } if ($annotation instanceof AbstractAnnotation) { if ($annotation->hasPartialId()) { if ($this->hasPartial($annotation->_partialId)) { Logger::notice('partial="' . $annotation->_partialId . '" is not unique. another was found in ' . $annotation->_context); } $this->setPartial($annotation->_partialId, $annotation); } elseif ($annotation instanceof Resource) { $this->resources[] = $annotation; } elseif ($annotation instanceof Model) { $this->models[] = $annotation; } } } return $annotations; }
public function validate() { if (empty($this->resourcePath)) { Logger::warning('@SWG\\Resource() is missing "resourcePath" in ' . $this->_context); return false; } if ($this->swaggerVersion) { if (version_compare($this->swaggerVersion, '1.2', '<')) { Logger::warning('swaggerVersion: ' . $this->swaggerVersion . ' is no longer supported. Use 1.2 or higher'); $this->swaggerVersion = null; } } $validApis = array(); foreach ($this->apis as $api) { $append = true; foreach ($validApis as $validApi) { if ($api->path === $validApi->path) { // The same api path? $append = false; // merge operations foreach ($api->operations as $operation) { $validApi->operations[] = $operation; } // merge description if ($validApi->description === null) { $validApi->description = $api->description; } elseif ($api->description !== null && $api->description !== $validApi->description) { Logger::notice('Competing description for ' . $validApi->identity() . ' in ' . $validApi->_context . ' and ' . $api->_context); } break; } } if ($api->validate() && $append) { $validApis[] = $api; } } if (count($validApis) === 0 && count($this->_partials) === 0) { Logger::warning($this->identity() . ' doesn\'t have any valid api calls'); return false; } $this->apis = $validApis; Produces::validateContainer($this); Consumes::validateContainer($this); return true; }
/** * @param $resourceName * @return mixed */ public function getResource($resourceName, $options = array()) { self::parseOptions($options, array('output' => 'array', 'json_pretty_print' => true, 'defaultBasePath' => null, 'defaultApiVersion' => null, 'defaultSwaggerVersion' => '1.2')); if (array_key_exists($resourceName, $this->registry) === false) { Logger::warning('Resource "' . $resourceName . '" not found, try "' . implode('", "', $this->getResourceNames()) . '"'); return false; } $resource = $this->registry[$resourceName]; // Apply defaults if ($resource->basePath === null) { $resource->basePath = $options['defaultBasePath']; } if ($resource->apiVersion === null) { $resource->apiVersion = $options['defaultApiVersion']; } if ($resource->swaggerVersion === null) { $resource->swaggerVersion = $options['defaultSwaggerVersion']; } // Sort operation paths alphabetically with shortest first $apis = $resource->apis; $paths = array(); foreach ($apis as $key => $api) { $paths[$key] = $api->path; } array_multisort($paths, SORT_ASC, $apis); $resource->apis = $apis; switch ($options['output']) { case 'array': return self::export($resource); case 'json': return $this->jsonEncode($resource, $options['json_pretty_print']); case 'object': return $resource; } }
/** * Merge the properties from the given object into this annotation. * Prevents overwriting properties that are already configured. * * @param object $object */ public function mergeProperties($object) { $defaultValues = get_class_vars(get_class($this)); $currentValues = get_object_vars($this); foreach ($object as $property => $value) { if ($property === '_context') { continue; } if ($currentValues[$property] === $defaultValues[$property]) { // Overwrite default values $this->{$property} = $value; continue; } if ($property === '_unmerged') { $this->_unmerged = array_merge($this->_unmerged, $value); continue; } if ($currentValues[$property] !== $value) { // New value is not the same? if ($defaultValues[$property] === $value) { // but is the same as the default? continue; // Keep current, no notice } $identity = method_exists($object, 'identity') ? $object->identity() : get_class($object); $context1 = $this->_context; $context2 = property_exists($object, '_context') ? $object->_context : 'unknown'; if (is_object($this->{$property}) && $this->{$property} instanceof AbstractAnnotation) { $context1 = $this->{$property}->_context; } Logger::warning('Multiple definitions for ' . $identity . '->' . $property . "\n Using: " . $context1 . "\n Skipping: " . $context2); } } }
/** * Use doctrine to parse the comment block and return the detected annotations. * * @param string $comment a T_DOC_COMMENT. * @param Context $context * @return array Annotations */ public function fromComment($comment, $context = null) { if ($context === null) { $context = new Context(['comment' => $comment]); } else { $context->comment = $comment; } try { self::$context = $context; if ($context->is('annotations') === false) { $context->annotations = []; } $comment = preg_replace_callback('/^[\\t ]*\\*[\\t ]+/m', function ($match) { // Replace leading tabs with spaces. // Workaround for http://www.doctrine-project.org/jira/browse/DCOM-255 return str_replace("\t", ' ', $match[0]); }, $comment); $annotations = $this->docParser->parse($comment, $context); self::$context = null; return $annotations; } catch (Exception $e) { self::$context = null; if (preg_match('/^(.+) at position ([0-9]+) in ' . preg_quote($context, '/') . '\\.$/', $e->getMessage(), $matches)) { $errorMessage = $matches[1]; $errorPos = $matches[2]; $atPos = strpos($comment, '@'); $context->line += substr_count($comment, "\n", 0, $atPos + $errorPos); $lines = explode("\n", substr($comment, $atPos, $errorPos)); $context->character = strlen(array_pop($lines)) + 1; // position starts at 0 character starts at 1 Logger::warning(new Exception($errorMessage . ' in ' . $context, $e->getCode(), $e)); } else { Logger::warning($e); } return []; } }
/** * Log warning, correct errors where possible. * @return bool Return false when the annotation is invalid and can't be used. */ public function validate() { Logger::warning(get_class($this) . ' doesn\'t implement the validate() method'); return false; }
/** * Customize the way json_encode() renders the annotations. * @return array */ public function jsonSerialize() { $data = new stdClass(); // Strip undefined and null values. $classVars = get_class_vars(get_class($this)); foreach (get_object_vars($this) as $property => $value) { if ($value !== UNDEFINED) { if ($classVars[$property] === UNDEFINED) { // When default is undefined, null is allowed. $data->{$property} = $value; } elseif ($value !== null) { $data->{$property} = $value; } } } // Strip properties that are for internal (swagger-php) use. foreach (static::$_blacklist as $property) { unset($data->{$property}); } // Inject vendor properties. unset($data->x); if (is_array($this->x)) { foreach ($this->x as $property => $value) { $prefixed = 'x-' . $property; $data->{$prefixed} = $value; } } // Map nested keys foreach (static::$_nested as $nested) { if (is_string($nested) || count($nested) === 1) { continue; } $property = $nested[0]; if ($this->{$property} === null) { continue; } $keyField = $nested[1]; $object = new stdClass(); foreach ($this->{$property} as $item) { $key = $item->{$keyField}; if ($key && empty($object->{$key})) { $object->{$key} = $item->jsonSerialize(); unset($object->{$key}->{$keyField}); } } $data->{$property} = $object; } // $file if (isset($data->file)) { $file = __DIR__ . '/../../../../../' . $data->file; if (is_file($file)) { $_schema = json_decode(preg_replace('/([^\\"])(null)([^\\"])/s', '$1"null"$3', file_get_contents($file))); $schema = new stdClass(); switch ($_schema->type) { case 'object': $schema->properties = $_schema->properties; break; case 'array': //$schema->type = 'array'; $schema = array(); foreach ($_schema->items as $item) { $addItem = new stdClass(); $addItem->properties = $item; $schema[] = $item; } break; case null: Logger::warning('Esta usuando null en el esquema y no esta soportado :), utilizar "null" entre comillas'); break; default: Logger::warning('El tipo usado en el esquema no esta soportado :), deberas revisar el repositorio swagger-php'); break; } $data = $schema; } else { Logger::notice("File not Found: " . $data->file); } } // $ref if (isset($data->ref)) { $dollarRef = '$ref'; $data->{$dollarRef} = $data->ref; unset($data->ref); } return $data; }