Skip to content

Commit f902635

Browse files
committed
Add description to properties
1 parent 93f47da commit f902635

File tree

7 files changed

+58
-1
lines changed

7 files changed

+58
-1
lines changed

src/Model/Property/Property.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class Property implements PropertyInterface
2424
protected $type = 'null';
2525
/** @var bool */
2626
protected $isPropertyRequired = true;
27+
/** @var string */
28+
protected $description = '';
2729

2830
/** @var Validator[] */
2931
protected $validator = [];
@@ -81,6 +83,24 @@ public function setType(string $type): PropertyInterface
8183
return $this;
8284
}
8385

86+
/**
87+
* @inheritdoc
88+
*/
89+
public function getDescription(): string
90+
{
91+
return $this->description;
92+
}
93+
94+
/**
95+
* @inheritdoc
96+
*/
97+
public function setDescription(string $description): PropertyInterface
98+
{
99+
$this->description = $description;
100+
101+
return $this;
102+
}
103+
84104
/**
85105
* @inheritdoc
86106
*/

src/Model/Property/PropertyInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ public function getType(): string;
3838
*/
3939
public function setType(string $type): PropertyInterface;
4040

41+
/**
42+
* Get a description for the property. If no description is available an empty string will be returned
43+
*
44+
* @return string
45+
*/
46+
public function getDescription(): string;
47+
48+
/**
49+
* Set a description for the property
50+
*
51+
* @param string $description
52+
*
53+
* @return PropertyInterface
54+
*/
55+
public function setDescription(string $description): PropertyInterface;
56+
4157
/**
4258
* Add a validator for the property
4359
*

src/Model/Property/PropertyProxy.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ public function setType(string $type): PropertyInterface
7575
return $this->getProperty()->setType($type);
7676
}
7777

78+
/**
79+
* @inheritdoc
80+
*/
81+
public function getDescription(): string
82+
{
83+
return $this->getProperty()->getDescription();
84+
}
85+
86+
/**
87+
* @inheritdoc
88+
*/
89+
public function setDescription(string $description): PropertyInterface
90+
{
91+
return $this->getProperty()->setDescription($description);
92+
}
93+
7894
/**
7995
* @inheritdoc
8096
*/

src/PropertyProcessor/Property/AbstractPropertyProcessor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PHPModelGenerator\PropertyProcessor\Property;
66

77
use PHPModelGenerator\Exception\InvalidArgumentException;
8+
use PHPModelGenerator\Exception\SchemaException;
89
use PHPModelGenerator\Model\Property\PropertyInterface;
910
use PHPModelGenerator\Model\Schema;
1011
use PHPModelGenerator\Model\Validator\PropertyValidator;
@@ -88,6 +89,8 @@ protected function addEnumValidator(PropertyInterface $property, array $allowedV
8889
/**
8990
* @param PropertyInterface $property
9091
* @param array $propertyData
92+
*
93+
* @throws SchemaException
9194
*/
9295
protected function addComposedValueValidator(PropertyInterface $property, array $propertyData): void
9396
{

src/PropertyProcessor/Property/AbstractValueProcessor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function __construct(
4343
public function process(string $propertyName, array $propertyData): PropertyInterface
4444
{
4545
$property = (new Property($propertyName, $this->type))
46+
->setDescription($propertyData['description'] ?? '')
4647
// the property is either required if defined as required
4748
// or if the property is related to a typed enum (strict type checks)
4849
->setRequired(

src/PropertyProcessor/Property/ConstProcessor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class ConstProcessor implements PropertyProcessorInterface
2323
public function process(string $propertyName, array $propertyData): PropertyInterface
2424
{
2525
return (new Property($propertyName, gettype($propertyData['const'])))
26+
->setDescription($propertyData['description'] ?? '')
2627
->addValidator(new PropertyValidator(
2728
'$value !== ' . var_export($propertyData['const'], true),
2829
InvalidArgumentException::class,

src/Templates/Model.phptpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ declare(strict_types = 1);
1717
class {{ class }}
1818
{
1919
{% foreach properties as property %}
20-
/** @var {% if property.getType() %}{{ property.getType() }}{% else %}mixed{% endif %} */
20+
/** @var {% if property.getType() %}{{ property.getType() }}{% else %}mixed{% endif %}{% if property.getDescription() %} {{ property.getDescription() }}{% endif %} */
2121
protected ${{ property.getAttribute() }};
2222
{% endforeach %}
2323
/** @var array */

0 commit comments

Comments
 (0)