Skip to content

Commit 2ede6ba

Browse files
committed
Add JSONModelInterface to schema instead of adding it in the template
1 parent 07881e4 commit 2ede6ba

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/Model/Schema.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PHPModelGenerator\Model;
66

7+
use PHPModelGenerator\Interfaces\JSONModelInterface;
78
use PHPModelGenerator\Model\Property\PropertyInterface;
89
use PHPModelGenerator\Model\SchemaDefinition\SchemaDefinitionDictionary;
910
use PHPModelGenerator\Model\Validator\PropertyValidatorInterface;
@@ -55,6 +56,8 @@ public function __construct(string $classPath, string $className, SchemaDefiniti
5556
$this->className = $className;
5657
$this->classPath = $classPath;
5758
$this->schemaDefinitionDictionary = $dictionary ?? new SchemaDefinitionDictionary('');
59+
60+
$this->addInterface(JSONModelInterface::class);
5861
}
5962

6063
/**

src/Templates/Model.phptpl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,9 @@ declare(strict_types = 1);
1616
* Class {{ class }}
1717
{% if namespace %} * @package {{ namespace }} {% endif %}
1818
*/
19-
class {{ class }} implements \PHPModelGenerator\Interfaces\JSONModelInterface
20-
{% foreach schema.getInterfaces() as interface %}
21-
, {{ viewHelper.getSimpleClassName(interface) }}
22-
{% endforeach %}
19+
class {{ class }} {% if schema.getInterfaces() %}implements {{ viewHelper.joinClassNames(schema.getInterfaces()) }}{% endif %}
2320
{
24-
{% foreach schema.getTraits() as trait %}
25-
use {{ viewHelper.getSimpleClassName(trait) }};
26-
{% endforeach %}
21+
{% if schema.getTraits() %}use {{ viewHelper.joinClassNames(schema.getTraits()) }};{% endif %}
2722

2823
{% foreach schema.getProperties() as property %}
2924
/**{% if viewHelper.getTypeHintAnnotation(property, true) %} @var {{ viewHelper.getTypeHintAnnotation(property, true) }}{% endif %}{% if property.getDescription() %} {{ property.getDescription() }}{% endif %} */

src/Utils/RenderHelper.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ public function getSimpleClassName(string $fqcn): string
6060
return end($parts);
6161
}
6262

63+
/**
64+
* @param array $fqcns
65+
*
66+
* @return string
67+
*/
68+
public function joinClassNames(array $fqcns): string
69+
{
70+
return join(', ', array_map([$this, 'getSimpleClassName'], $fqcns));
71+
}
72+
6373
/**
6474
* Resolve all associated decorators of a property
6575
*

0 commit comments

Comments
 (0)