Skip to content

Commit b80597d

Browse files
committed
Merge remote-tracking branch 'origin/master' into PostProcessorHooks
# Conflicts: # src/SchemaProcessor/RenderQueue.php
2 parents e424065 + c0a2fc7 commit b80597d

File tree

67 files changed

+921
-284
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+921
-284
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ php:
1010
- 7.2
1111
- 7.3
1212
- 7.4
13+
- 8.0
1314
- nightly
1415

1516
install:
@@ -28,7 +29,7 @@ before_script:
2829
- mkdir -p build/logs
2930

3031
script:
31-
- ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml --testdox
32+
- XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml --testdox
3233

3334
after_script:
3435
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ The directory `./tests/manual` contains some easy examples which show the usage.
9999
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:
100100
```json
101101
{
102-
"id": "Person",
102+
"$id": "Person",
103103
"type": "object",
104104
"properties": {
105105
"name": {

docs/source/examples.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ The Ebay OpenAPIv3 spec for the sell-inventory API is an around 6000 lines API d
2121
file_get_contents('https://developer.ebay.com/api-docs/master/sell/inventory/openapi/3/sell_inventory_v1_oas3.json')
2222
);
2323
24-
$start = microtime(true);
2524
$generator
2625
->generateModelDirectory($resultDir)
2726
->generateModels(new OpenAPIv3Provider($file), $resultDir);

src/Model/GeneratorConfiguration.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,14 @@ public function getClassNameGenerator(): ClassNameGeneratorInterface
127127

128128
/**
129129
* @param ClassNameGeneratorInterface $classNameGenerator
130+
*
131+
* @return $this
130132
*/
131-
public function setClassNameGenerator(ClassNameGeneratorInterface $classNameGenerator): void
133+
public function setClassNameGenerator(ClassNameGeneratorInterface $classNameGenerator): self
132134
{
133135
$this->classNameGenerator = $classNameGenerator;
136+
137+
return $this;
134138
}
135139

136140
/**
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPModelGenerator\Model\Property;
6+
7+
use PHPModelGenerator\Exception\SchemaException;
8+
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
9+
use PHPModelGenerator\Model\SchemaDefinition\JsonSchemaTrait;
10+
11+
/**
12+
* Class AbstractProperty
13+
*
14+
* @package PHPModelGenerator\Model\Property
15+
*/
16+
abstract class AbstractProperty implements PropertyInterface
17+
{
18+
use JsonSchemaTrait;
19+
20+
/** @var string */
21+
protected $name = '';
22+
/** @var string */
23+
protected $attribute = '';
24+
25+
/**
26+
* Property constructor.
27+
*
28+
* @param string $name
29+
* @param JsonSchema $jsonSchema
30+
*
31+
* @throws SchemaException
32+
*/
33+
public function __construct(string $name, JsonSchema $jsonSchema)
34+
{
35+
$this->name = $name;
36+
$this->jsonSchema = $jsonSchema;
37+
38+
$this->attribute = $this->processAttributeName($name);
39+
}
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
public function getName(): string
45+
{
46+
return $this->name;
47+
}
48+
49+
/**
50+
* @inheritdoc
51+
*/
52+
public function getAttribute(): string
53+
{
54+
return ($this->isInternal() ? '_' : '') . $this->attribute;
55+
}
56+
57+
/**
58+
* Convert a name of a JSON-field into a valid PHP variable name to be used as class attribute
59+
*
60+
* @param string $name
61+
*
62+
* @return string
63+
*
64+
* @throws SchemaException
65+
*/
66+
protected function processAttributeName(string $name): string
67+
{
68+
$attributeName = preg_replace_callback(
69+
'/([a-z][a-z0-9]*)([A-Z])/',
70+
function ($matches) {
71+
return "{$matches[1]}-{$matches[2]}";
72+
},
73+
$name
74+
);
75+
76+
$elements = array_map(
77+
function ($element) {
78+
return ucfirst(strtolower($element));
79+
},
80+
preg_split('/[^a-z0-9]/i', $attributeName)
81+
);
82+
83+
$attributeName = lcfirst(join('', $elements));
84+
85+
if (empty($attributeName)) {
86+
throw new SchemaException(
87+
sprintf(
88+
"Property name '%s' results in an empty attribute name in file %s",
89+
$name,
90+
$this->jsonSchema->getFile()
91+
)
92+
);
93+
}
94+
95+
return $attributeName;
96+
}
97+
}

src/Model/Property/CompositionPropertyDecorator.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace PHPModelGenerator\Model\Property;
66

7+
use PHPModelGenerator\Exception\SchemaException;
8+
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
79
use PHPModelGenerator\Model\SchemaDefinition\ResolvedDefinitionsCollection;
810

911
/**
@@ -26,11 +28,20 @@ class CompositionPropertyDecorator extends PropertyProxy
2628
/**
2729
* CompositionPropertyDecorator constructor.
2830
*
31+
* @param string $propertyName
32+
* @param JsonSchema $jsonSchema
2933
* @param PropertyInterface $property
34+
*
35+
* @throws SchemaException
3036
*/
31-
public function __construct(PropertyInterface $property)
37+
public function __construct(string $propertyName, JsonSchema $jsonSchema, PropertyInterface $property)
3238
{
33-
parent::__construct(new ResolvedDefinitionsCollection([self::PROPERTY_KEY => $property]), self::PROPERTY_KEY);
39+
parent::__construct(
40+
$propertyName,
41+
$jsonSchema,
42+
new ResolvedDefinitionsCollection([self::PROPERTY_KEY => $property]),
43+
self::PROPERTY_KEY
44+
);
3445
}
3546

3647
/**

src/Model/Property/Property.php

Lines changed: 7 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPModelGenerator\Exception\SchemaException;
88
use PHPModelGenerator\Model\Schema;
99
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
10-
use PHPModelGenerator\Model\SchemaDefinition\JsonSchemaTrait;
1110
use PHPModelGenerator\Model\Validator;
1211
use PHPModelGenerator\Model\Validator\PropertyValidatorInterface;
1312
use PHPModelGenerator\PropertyProcessor\Decorator\Property\PropertyDecoratorInterface;
@@ -18,14 +17,8 @@
1817
*
1918
* @package PHPModelGenerator\Model\Property
2019
*/
21-
class Property implements PropertyInterface
20+
class Property extends AbstractProperty
2221
{
23-
use JsonSchemaTrait;
24-
25-
/** @var string */
26-
protected $name = '';
27-
/** @var string */
28-
protected $attribute = '';
2922
/** @var string */
3023
protected $type = 'null';
3124
/** @var string|null */
@@ -62,28 +55,10 @@ class Property implements PropertyInterface
6255
*/
6356
public function __construct(string $name, string $type, JsonSchema $jsonSchema, string $description = '')
6457
{
65-
$this->name = $name;
58+
parent::__construct($name, $jsonSchema);
59+
6660
$this->type = $type;
67-
$this->jsonSchema = $jsonSchema;
6861
$this->description = $description;
69-
70-
$this->attribute = $this->processAttributeName($name);
71-
}
72-
73-
/**
74-
* @inheritdoc
75-
*/
76-
public function getName(): string
77-
{
78-
return $this->name;
79-
}
80-
81-
/**
82-
* @inheritdoc
83-
*/
84-
public function getAttribute(): string
85-
{
86-
return ($this->isInternal() ? '_' : '') . $this->attribute;
8762
}
8863

8964
/**
@@ -229,50 +204,9 @@ public function resolveDecorator(string $input, bool $nestedProperty): string
229204
/**
230205
* @inheritdoc
231206
*/
232-
public function hasDecorators(): bool
233-
{
234-
return count($this->decorators) > 0;
235-
}
236-
237-
/**
238-
* Convert a name of a JSON-field into a valid PHP variable name to be used as class attribute
239-
*
240-
* @param string $name
241-
*
242-
* @return string
243-
*
244-
* @throws SchemaException
245-
*/
246-
protected function processAttributeName(string $name): string
207+
public function getDecorators(): array
247208
{
248-
$attributeName = preg_replace_callback(
249-
'/([a-z][a-z0-9]*)([A-Z])/',
250-
function ($matches) {
251-
return "{$matches[1]}-{$matches[2]}";
252-
},
253-
$name
254-
);
255-
256-
$elements = array_map(
257-
function ($element) {
258-
return ucfirst(strtolower($element));
259-
},
260-
preg_split('/[^a-z0-9]/i', $attributeName)
261-
);
262-
263-
$attributeName = lcfirst(join('', $elements));
264-
265-
if (empty($attributeName)) {
266-
throw new SchemaException(
267-
sprintf(
268-
"Property name '%s' results in an empty attribute name in file %s",
269-
$name,
270-
$this->jsonSchema->getFile()
271-
)
272-
);
273-
}
274-
275-
return $attributeName;
209+
return $this->decorators;
276210
}
277211

278212
/**
@@ -308,9 +242,9 @@ public function setDefaultValue($defaultValue): PropertyInterface
308242
/**
309243
* @inheritdoc
310244
*/
311-
public function getDefaultValue()
245+
public function getDefaultValue(): ?string
312246
{
313-
return var_export($this->defaultValue, true);
247+
return $this->defaultValue !== null ? var_export($this->defaultValue, true) : null;
314248
}
315249

316250
/**

src/Model/Property/PropertyInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ public function addDecorator(PropertyDecoratorInterface $decorator): PropertyInt
126126
public function resolveDecorator(string $input, bool $nestedProperty): string;
127127

128128
/**
129-
* @return bool
129+
* @return PropertyDecoratorInterface[]
130130
*/
131-
public function hasDecorators(): bool;
131+
public function getDecorators(): array;
132132

133133
/**
134134
* @param bool $isPropertyRequired
@@ -159,9 +159,9 @@ public function setInternal(bool $isPropertyInternal): PropertyInterface;
159159
public function setDefaultValue($defaultValue): PropertyInterface;
160160

161161
/**
162-
* @return mixed
162+
* @return string|null
163163
*/
164-
public function getDefaultValue();
164+
public function getDefaultValue(): ?string;
165165

166166
/**
167167
* @return bool

0 commit comments

Comments
 (0)