Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into PostProcessorHooks
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/SchemaProcessor/RenderQueue.php
  • Loading branch information
wol-soft committed Feb 5, 2021
2 parents e424065 + c0a2fc7 commit b80597d
Show file tree
Hide file tree
Showing 67 changed files with 921 additions and 284 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ php:
- 7.2
- 7.3
- 7.4
- 8.0
- nightly

install:
Expand All @@ -28,7 +29,7 @@ before_script:
- mkdir -p build/logs

script:
- ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml --testdox
- XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml --testdox

after_script:
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ The directory `./tests/manual` contains some easy examples which show the usage.
Let's have a look into an easy example. We create a simple model for a person with a name and an optional age. Our resulting JSON-Schema:
```json
{
"id": "Person",
"$id": "Person",
"type": "object",
"properties": {
"name": {
Expand Down
1 change: 0 additions & 1 deletion docs/source/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ The Ebay OpenAPIv3 spec for the sell-inventory API is an around 6000 lines API d
file_get_contents('https://developer.ebay.com/api-docs/master/sell/inventory/openapi/3/sell_inventory_v1_oas3.json')
);
$start = microtime(true);
$generator
->generateModelDirectory($resultDir)
->generateModels(new OpenAPIv3Provider($file), $resultDir);
Expand Down
6 changes: 5 additions & 1 deletion src/Model/GeneratorConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,14 @@ public function getClassNameGenerator(): ClassNameGeneratorInterface

/**
* @param ClassNameGeneratorInterface $classNameGenerator
*
* @return $this
*/
public function setClassNameGenerator(ClassNameGeneratorInterface $classNameGenerator): void
public function setClassNameGenerator(ClassNameGeneratorInterface $classNameGenerator): self
{
$this->classNameGenerator = $classNameGenerator;

return $this;
}

/**
Expand Down
97 changes: 97 additions & 0 deletions src/Model/Property/AbstractProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

namespace PHPModelGenerator\Model\Property;

use PHPModelGenerator\Exception\SchemaException;
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
use PHPModelGenerator\Model\SchemaDefinition\JsonSchemaTrait;

/**
* Class AbstractProperty
*
* @package PHPModelGenerator\Model\Property
*/
abstract class AbstractProperty implements PropertyInterface
{
use JsonSchemaTrait;

/** @var string */
protected $name = '';
/** @var string */
protected $attribute = '';

/**
* Property constructor.
*
* @param string $name
* @param JsonSchema $jsonSchema
*
* @throws SchemaException
*/
public function __construct(string $name, JsonSchema $jsonSchema)
{
$this->name = $name;
$this->jsonSchema = $jsonSchema;

$this->attribute = $this->processAttributeName($name);
}

/**
* @inheritdoc
*/
public function getName(): string
{
return $this->name;
}

/**
* @inheritdoc
*/
public function getAttribute(): string
{
return ($this->isInternal() ? '_' : '') . $this->attribute;
}

/**
* Convert a name of a JSON-field into a valid PHP variable name to be used as class attribute
*
* @param string $name
*
* @return string
*
* @throws SchemaException
*/
protected function processAttributeName(string $name): string
{
$attributeName = preg_replace_callback(
'/([a-z][a-z0-9]*)([A-Z])/',
function ($matches) {
return "{$matches[1]}-{$matches[2]}";
},
$name
);

$elements = array_map(
function ($element) {
return ucfirst(strtolower($element));
},
preg_split('/[^a-z0-9]/i', $attributeName)
);

$attributeName = lcfirst(join('', $elements));

if (empty($attributeName)) {
throw new SchemaException(
sprintf(
"Property name '%s' results in an empty attribute name in file %s",
$name,
$this->jsonSchema->getFile()
)
);
}

return $attributeName;
}
}
15 changes: 13 additions & 2 deletions src/Model/Property/CompositionPropertyDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace PHPModelGenerator\Model\Property;

use PHPModelGenerator\Exception\SchemaException;
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
use PHPModelGenerator\Model\SchemaDefinition\ResolvedDefinitionsCollection;

/**
Expand All @@ -26,11 +28,20 @@ class CompositionPropertyDecorator extends PropertyProxy
/**
* CompositionPropertyDecorator constructor.
*
* @param string $propertyName
* @param JsonSchema $jsonSchema
* @param PropertyInterface $property
*
* @throws SchemaException
*/
public function __construct(PropertyInterface $property)
public function __construct(string $propertyName, JsonSchema $jsonSchema, PropertyInterface $property)
{
parent::__construct(new ResolvedDefinitionsCollection([self::PROPERTY_KEY => $property]), self::PROPERTY_KEY);
parent::__construct(
$propertyName,
$jsonSchema,
new ResolvedDefinitionsCollection([self::PROPERTY_KEY => $property]),
self::PROPERTY_KEY
);
}

/**
Expand Down
80 changes: 7 additions & 73 deletions src/Model/Property/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PHPModelGenerator\Exception\SchemaException;
use PHPModelGenerator\Model\Schema;
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
use PHPModelGenerator\Model\SchemaDefinition\JsonSchemaTrait;
use PHPModelGenerator\Model\Validator;
use PHPModelGenerator\Model\Validator\PropertyValidatorInterface;
use PHPModelGenerator\PropertyProcessor\Decorator\Property\PropertyDecoratorInterface;
Expand All @@ -18,14 +17,8 @@
*
* @package PHPModelGenerator\Model\Property
*/
class Property implements PropertyInterface
class Property extends AbstractProperty
{
use JsonSchemaTrait;

/** @var string */
protected $name = '';
/** @var string */
protected $attribute = '';
/** @var string */
protected $type = 'null';
/** @var string|null */
Expand Down Expand Up @@ -62,28 +55,10 @@ class Property implements PropertyInterface
*/
public function __construct(string $name, string $type, JsonSchema $jsonSchema, string $description = '')
{
$this->name = $name;
parent::__construct($name, $jsonSchema);

$this->type = $type;
$this->jsonSchema = $jsonSchema;
$this->description = $description;

$this->attribute = $this->processAttributeName($name);
}

/**
* @inheritdoc
*/
public function getName(): string
{
return $this->name;
}

/**
* @inheritdoc
*/
public function getAttribute(): string
{
return ($this->isInternal() ? '_' : '') . $this->attribute;
}

/**
Expand Down Expand Up @@ -229,50 +204,9 @@ public function resolveDecorator(string $input, bool $nestedProperty): string
/**
* @inheritdoc
*/
public function hasDecorators(): bool
{
return count($this->decorators) > 0;
}

/**
* Convert a name of a JSON-field into a valid PHP variable name to be used as class attribute
*
* @param string $name
*
* @return string
*
* @throws SchemaException
*/
protected function processAttributeName(string $name): string
public function getDecorators(): array
{
$attributeName = preg_replace_callback(
'/([a-z][a-z0-9]*)([A-Z])/',
function ($matches) {
return "{$matches[1]}-{$matches[2]}";
},
$name
);

$elements = array_map(
function ($element) {
return ucfirst(strtolower($element));
},
preg_split('/[^a-z0-9]/i', $attributeName)
);

$attributeName = lcfirst(join('', $elements));

if (empty($attributeName)) {
throw new SchemaException(
sprintf(
"Property name '%s' results in an empty attribute name in file %s",
$name,
$this->jsonSchema->getFile()
)
);
}

return $attributeName;
return $this->decorators;
}

/**
Expand Down Expand Up @@ -308,9 +242,9 @@ public function setDefaultValue($defaultValue): PropertyInterface
/**
* @inheritdoc
*/
public function getDefaultValue()
public function getDefaultValue(): ?string
{
return var_export($this->defaultValue, true);
return $this->defaultValue !== null ? var_export($this->defaultValue, true) : null;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Model/Property/PropertyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ public function addDecorator(PropertyDecoratorInterface $decorator): PropertyInt
public function resolveDecorator(string $input, bool $nestedProperty): string;

/**
* @return bool
* @return PropertyDecoratorInterface[]
*/
public function hasDecorators(): bool;
public function getDecorators(): array;

/**
* @param bool $isPropertyRequired
Expand Down Expand Up @@ -159,9 +159,9 @@ public function setInternal(bool $isPropertyInternal): PropertyInterface;
public function setDefaultValue($defaultValue): PropertyInterface;

/**
* @return mixed
* @return string|null
*/
public function getDefaultValue();
public function getDefaultValue(): ?string;

/**
* @return bool
Expand Down
Loading

0 comments on commit b80597d

Please sign in to comment.