Skip to content

Commit b884d80

Browse files
committed
getPropertyAnnotation with FormattedDescription
1 parent f96257d commit b884d80

File tree

4 files changed

+36
-21
lines changed

4 files changed

+36
-21
lines changed

src/generator/default/dbmodel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
*
2424
<?php foreach ($model->dbAttributes() as $attribute): ?>
25-
* @property <?= $attribute->getFormattedDescription() ?>
25+
* @property <?= $attribute->getPropertyAnnotation() ?>
2626

2727
<?php endforeach; ?>
2828
*

src/lib/helpers/FormatHelper.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace cebe\yii2openapi\lib\helpers;
4+
5+
class FormatHelper
6+
{
7+
/**
8+
* @param $description
9+
* @return string
10+
*/
11+
public static function getFormattedDescription($description): string
12+
{
13+
$descriptionArr = explode("\n", trim($description));
14+
$descriptionArr = array_map(function($item) {
15+
return $item !== '' ? ' ' . $item : $item;
16+
}, $descriptionArr);
17+
return implode("\n *", $descriptionArr);
18+
}
19+
}

src/lib/items/Attribute.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace cebe\yii2openapi\lib\items;
99

10+
use cebe\yii2openapi\lib\helpers\FormatHelper;
1011
use yii\helpers\VarDumper;
1112
use \Yii;
1213
use cebe\yii2openapi\lib\openapi\PropertySchema;
@@ -295,11 +296,16 @@ public function getMinLength():?int
295296
return $this->limits['minLength'];
296297
}
297298

298-
public function getFormattedDescription():string
299+
/**
300+
* @return string
301+
*/
302+
public function getPropertyAnnotation(): string
299303
{
300-
$comment = $this->columnName.' '.$this->description;
301-
$type = $this->phpType;
302-
return $type.' $'.str_replace("\n", "\n * ", rtrim($comment));
304+
$annotation = $this->phpType . ' $' . $this->columnName;
305+
if (!empty($this->description)) {
306+
$annotation .= FormatHelper::getFormattedDescription($this->description);
307+
}
308+
return $annotation;
303309
}
304310

305311
public function toColumnSchema():ColumnSchema

src/lib/items/DbModel.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace cebe\yii2openapi\lib\items;
99

10+
use cebe\yii2openapi\lib\helpers\FormatHelper;
1011
use cebe\yii2openapi\lib\ValidationRulesBuilder;
1112
use Yii;
1213
use yii\base\BaseObject;
@@ -234,25 +235,14 @@ private function getScenariosByOpenapiSchema(): array
234235
return $scenarios;
235236
}
236237

237-
public function getModelClassDescription_()
238-
{
239-
return !empty($this->description) ?
240-
str_replace("\n", "\n *", ' ' . trim($this->description))
241-
: ' This is the model class for table "'.$this->tableName.'".';
242-
}
243-
244-
/** @noinspection PhpParamsInspection */
245-
public function getModelClassDescription()
238+
/**
239+
* @return string
240+
*/
241+
public function getModelClassDescription(): string
246242
{
247243
if (empty($this->description)) {
248244
return ' This is the model class for table "'.$this->tableName.'".';
249245
}
250-
251-
$descriptionArr = explode("\n", $this->description);
252-
$descriptionArr = array_map('trim', $descriptionArr);
253-
$descriptionArr = array_map(function($item) {
254-
return $item !== '' ? ' ' . $item : $item;
255-
}, $descriptionArr);
256-
return implode("\n *", $descriptionArr);
246+
return FormatHelper::getFormattedDescription($this->description);
257247
}
258248
}

0 commit comments

Comments
 (0)