Skip to content

Commit

Permalink
Merge pull request #2 from calcinai/oapi-3
Browse files Browse the repository at this point in the history
OAS 3.0 compatibility
  • Loading branch information
calcinai authored Jan 14, 2019
2 parents b5c9ee7 + 4b236e9 commit 543db8f
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 47 deletions.
2 changes: 1 addition & 1 deletion bin/gendarme
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ use Calcinai\Gendarme\Command\GenerateCommand;

$application = new Application();
$application->add(new GenerateCommand());
$application->run();
$application->run();
25 changes: 16 additions & 9 deletions src/Command/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @package gendarme
* @author Michael Calcinai <[email protected]>
*/

namespace Calcinai\Gendarme\Command;

use Calcinai\Gendarme\Generator;
Expand All @@ -15,9 +16,11 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class GenerateCommand extends Command {
class GenerateCommand extends Command
{

protected function configure() {
protected function configure()
{
$this
->setName('generate')
->setDescription('Generate classes representing the JSON schema')
Expand All @@ -44,21 +47,25 @@ protected function configure() {
InputOption::VALUE_OPTIONAL,
'The name of the root generated schema',
'Schema'
)
;
);
}

protected function execute(InputInterface $input, OutputInterface $output){


/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|void|null
* @throws \ReflectionException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$schema_file = $input->getArgument('schema');
$output_dir = $input->getArgument('output');

if(!file_exists($schema_file)){
if (!file_exists($schema_file)) {
throw new InvalidArgumentException('Invalid schema provided');
}

if(!file_exists($output_dir)){
if (!file_exists($output_dir)) {
throw new InvalidArgumentException('Invalid output directory provided');
}

Expand Down
13 changes: 8 additions & 5 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function generateClasses($schemas)
$this->writeClass($this->base_schema_class, $base_schema_namespace);

foreach ($schemas as $schema) {
if ($schema->type !== Schema::TYPE_OBJECT) {
if (!$schema->shouldBeDefinition()) {
continue;
}

Expand Down Expand Up @@ -179,7 +179,7 @@ private function buildModel(Schema $schema)
}, $child_schema->getHintableClasses());

//This should all line up.
$property_hints[$property_name] = $child_schema->getHintableClasses();
$property_hints[$property_name] = $child_schema->getHintableClasses(true);

//Append the enums
if (!empty($child_schema->getEnum())) {
Expand All @@ -188,7 +188,7 @@ private function buildModel(Schema $schema)

//All types for the doc block - TODO - clean up
$all_types = array_map(function ($type) use ($class_resolver) {
if (in_array($type, ['bool', 'int', 'string', 'mixed'])) {
if (in_array($type, Schema::$ALL_TYPES)) {
return $type;
}

Expand Down Expand Up @@ -247,7 +247,7 @@ private function buildModel(Schema $schema)


if ($schema->additional_properties instanceof Schema) {
$additional_properties = $schema->additional_properties->getHintableClasses();
$additional_properties = $schema->additional_properties->getHintableClasses(true);
} else {
$additional_properties = $schema->additional_properties;
}
Expand All @@ -259,7 +259,7 @@ private function buildModel(Schema $schema)
->setDocComment($this->formatDocComment(['Allowed additional properties', '@var array'])));

$parsed_pattern_props = array_map(function (Schema $child_schema) {
return $child_schema->getHintableClasses();
return $child_schema->getHintableClasses(true);
}, $schema->pattern_properties);


Expand Down Expand Up @@ -343,6 +343,7 @@ private function buildArraySetter($data_index, Param $parameter, $types, $descri

$setter_lines[] = sprintf('@param %s $%s', implode("|", $types), $parameter_name);
$setter_lines[] = '@return $this';
$setter_lines[] = '@throws \Exception';

$setter->setDocComment($this->formatDocComment($setter_lines));

Expand Down Expand Up @@ -386,6 +387,7 @@ private function buildSetter($data_index, Param $parameter, $types, $description

$setter_lines[] = sprintf('@param %s $%s', implode("|", $types), $parameter_name);
$setter_lines[] = '@return $this';
$setter_lines[] = '@throws \Exception';

$setter->setDocComment($this->formatDocComment($setter_lines));

Expand All @@ -394,6 +396,7 @@ private function buildSetter($data_index, Param $parameter, $types, $description

/**
* @param $data_index
* @param $property_name
* @param string[] $types
* @param string|null $description
* @return Method
Expand Down
6 changes: 3 additions & 3 deletions src/Keyword/Default_.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ class Default_ extends AbstractKeyword {
* @param Parser $parser
* @param Schema $schema
* @param $node
* @return Schema
* @return void|Schema
*/
public static function parse(Parser $parser, Schema $schema, $node) {

if(!is_scalar($node)){
if(is_scalar($node)){
return;
}

$schema->setDefault($node);
}
}
}
4 changes: 2 additions & 2 deletions src/Keyword/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Enum extends AbstractKeyword {
* @param Parser $parser
* @param Schema $schema
* @param $node
* @return Schema
* @return void
*/
public static function parse(Parser $parser, Schema $schema, $node) {
$schema->setEnum($node);
Expand All @@ -27,4 +27,4 @@ public static function parse(Parser $parser, Schema $schema, $node) {
$schema->setDefault(current($node));
}
}
}
}
4 changes: 2 additions & 2 deletions src/Keyword/KeywordInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface KeywordInterface {
* @param Parser $parser
* @param Schema $schema
* @param $node
* @return Schema
* @return void|Schema
*/
public static function parse(Parser $parser, Schema $schema, $node);
}
}
4 changes: 2 additions & 2 deletions src/Keyword/Required.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class Required extends AbstractKeyword {
* @param Parser $parser
* @param Schema $schema
* @param $node
* @return Schema
* @return void
*/
public static function parse(Parser $parser, Schema $schema, $node) {
//should't destroy what might already be there
$schema->addRequired($node);
}
}
}
4 changes: 2 additions & 2 deletions src/Keyword/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Type extends AbstractKeyword {
* @param Parser $parser
* @param Schema $schema
* @param $node
* @return Schema
* @return void|Schema
*/
public static function parse(Parser $parser, Schema $schema, $node) {

Expand All @@ -38,4 +38,4 @@ public static function parse(Parser $parser, Schema $schema, $node) {

}
}
}
}
53 changes: 39 additions & 14 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@

class Schema
{


const TYPE_ARRAY = 'array';
const TYPE_OBJECT = 'object';
const TYPE_ARRAY = 'array';
const TYPE_OBJECT = 'object';
const TYPE_BOOLEAN = 'boolean';
const TYPE_INTEGER = 'integer';
const TYPE_NULL = 'null';
const TYPE_NUMBER = 'number';
const TYPE_STRING = 'string';
const TYPE_NULL = 'null';
const TYPE_NUMBER = 'number';
const TYPE_STRING = 'string';
const TYPE_MIXED = 'mixed';


public $id;
Expand Down Expand Up @@ -98,6 +97,17 @@ class Schema
private $class_name;
private $relative_class_name;

public static $ALL_TYPES = [
self::TYPE_ARRAY,
self::TYPE_OBJECT,
self::TYPE_BOOLEAN,
self::TYPE_INTEGER,
self::TYPE_NULL,
self::TYPE_NUMBER,
self::TYPE_STRING,
self::TYPE_MIXED
];

public function __construct($id)
{
$this->id = $id;
Expand Down Expand Up @@ -355,7 +365,7 @@ public function setRequired($required)

public function addRequired($required)
{
if(!is_array($required)) {
if (!is_array($required)) {
$required = [$required];

}
Expand All @@ -368,6 +378,16 @@ public function getRequired()
return $this->required;
}

/**
* If this item should be represented as a definition/class
*
* @return bool
*/
public function shouldBeDefinition()
{
return $this->type === Schema::TYPE_OBJECT && !is_numeric($this->getClassName());
}

/**
* Used to get the actual type hints for generation.
*
Expand Down Expand Up @@ -416,18 +436,23 @@ public function getHintableClasses($include_scalar = false)
public static function getHintsFromSchema(Schema $item, $include_scalar)
{

if ($item->type === Schema::TYPE_OBJECT) {
//If it's a definition object, return that else the php type
if ($item->shouldBeDefinition()) {
return [$item->getRelativeClassName()];
} elseif ($include_scalar) {


//This is needed to catch differences between json and php types
switch ($item->type) {
case self::TYPE_STRING :
case self::TYPE_OBJECT :
case self::TYPE_ARRAY :
case self::TYPE_NULL:
case self::TYPE_BOOLEAN:
return ['bool'];
case self::TYPE_NUMBER :
case self::TYPE_INTEGER:
return ['int'];
case self::TYPE_STRING :
return ['string'];
return [$item->type];
case self::TYPE_NUMBER :
return [self::TYPE_INTEGER];
default:
return ['mixed'];
}
Expand Down
32 changes: 25 additions & 7 deletions src/Templates/BaseSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ abstract class BaseSchema implements \IteratorAggregate, \Countable, \JsonSerial
*/
protected static $additional_properties = [];


/**
* @param array $data
* @throws \Exception
*/
public function __construct($data = [])
{
$this->parseData($data);
Expand Down Expand Up @@ -123,8 +126,12 @@ private function validateProperty($property_name, $value)
return true;
}

foreach (static::$properties[$property_name] as $allowed_class) {
$fq_class = self::getFQCN($allowed_class);
foreach (static::$properties[$property_name] as $allowed_type) {
if (gettype($value) === $allowed_type) {
return true;
}

$fq_class = self::getFQCN($allowed_type);
if ($value instanceof $fq_class) {
return true;
}
Expand All @@ -139,8 +146,12 @@ private function validateProperty($property_name, $value)
return true;
}

foreach (static::$additional_properties as $allowed_class) {
$fq_class = self::getFQCN($allowed_class);
foreach (static::$additional_properties as $allowed_type) {
if (gettype($value) === $allowed_type) {
return true;
}

$fq_class = self::getFQCN($allowed_type);
if ($value instanceof $fq_class) {
return true;
}
Expand All @@ -164,8 +175,12 @@ private function validateProperty($property_name, $value)
return true;
}

foreach ($types as $allowed_class) {
$fq_class = self::getFQCN($allowed_class);
foreach ($types as $allowed_type) {
if (gettype($value) === $allowed_type) {
return true;
}

$fq_class = self::getFQCN($allowed_type);
if ($value instanceof $fq_class) {
return true;
}
Expand All @@ -181,6 +196,7 @@ private function validateProperty($property_name, $value)
* Parse a schema object into the correct objects
*
* @param $data
* @throws \Exception
*/
private function parseData($data)
{
Expand Down Expand Up @@ -223,6 +239,7 @@ private function parseData($data)
* @param $types_to_try
* @param $object
* @return BaseSchema
* @throws \Exception
*/
private static function tryToCast($types_to_try, $object)
{
Expand Down Expand Up @@ -260,6 +277,7 @@ protected static function getFQCN($relative_class)
/**
* @param array $data
* @return static
* @throws \Exception
*/
public static function create($data = [])
{
Expand Down

0 comments on commit 543db8f

Please sign in to comment.