Class luya\admin\openapi\specs\ActiveRecordToSchema
Inheritance | luya\admin\openapi\specs\ActiveRecordToSchema |
---|---|
Available since version | 3.2.0 |
Source Code | https://github.com/luyadev/luya-module-admin/blob/master/src/openapi/specs/ActiveRecordToSchema.php |
Generate Schema Specs from Active Record.
Protected Properties
Property | Type | Description | Defined By |
---|---|---|---|
$activeRecord | yii\base\Model | luya\admin\openapi\specs\ActiveRecordToSchema | |
$baseSpecs | luya\admin\openapi\specs\BaseSpecs | luya\admin\openapi\specs\ActiveRecordToSchema | |
$phpDocParser | luya\admin\openapi\phpdoc\PhpDocParser | luya\admin\openapi\specs\ActiveRecordToSchema | |
$senderActiveRecordClassName | string | Contains the class which was the origin creator of the active record schema, this can be usused to determine circular references. | luya\admin\openapi\specs\ActiveRecordToSchema |
Public Methods
Method | Description | Defined By |
---|---|---|
__construct() | luya\admin\openapi\specs\ActiveRecordToSchema | |
createSchema() | luya\admin\openapi\specs\ActiveRecordToSchema | |
getProperties() | Get Properties | luya\admin\openapi\specs\ActiveRecordToSchema |
Protected Methods
Method | Description | Defined By |
---|---|---|
isCircularReference() | luya\admin\openapi\specs\ActiveRecordToSchema |
Property Details
Contains the class which was the origin creator of the active record schema, this can be usused to determine circular references.
Method Details
public void __construct ( luya\admin\openapi\specs\BaseSpecs $baseSpecs, yii\base\Model $activeRecord, $senderActiveRecordClassName = null ) | ||
$baseSpecs | ||
$activeRecord | ||
$senderActiveRecordClassName |
public function __construct(BaseSpecs $baseSpecs, Model $activeRecord, $senderActiveRecordClassName = null)
{
$this->activeRecord = $activeRecord;
$this->baseSpecs = $baseSpecs;
$this->phpDocParser = new PhpDocParser(new ReflectionClass(get_class($activeRecord)));
$this->senderActiveRecordClassName = (array) $senderActiveRecordClassName;
}
public void createSchema ( $attributeName ) | ||
$attributeName |
public function createSchema($attributeName)
{
$property = $this->phpDocParser->getProperty($attributeName);
$type = $property->getType();
// handle php object type
if ($type->getIsClass() && !$this->isCircularReference($type->getClassName())) {
$object = $this->baseSpecs->createActiveRecordSchemaObjectFromClassName($type->getClassName(), array_merge([get_class($this->activeRecord)], $this->senderActiveRecordClassName));
if ($object) {
$config = $this->baseSpecs->createSchemaFromActiveRecordToSchemaObject($object, $type->getIsArray());
$config['title'] = $property->getDescription() ?: $type->getClassPhpDocParser()->getShortSummary();
$config['description'] = $type->getClassPhpDocParser()->getLongDescription(); // @TODO veryify if <br> or PHP_EOL (\n) works, redoc seems to work with <br/>
return new Schema($config);
}
}
if ($type->getIsScalar()) {
return new Schema([
'type' => $type->getNoramlizeName(),
'title' => $this->activeRecord->getAttributeLabel($attributeName),
'description' => implode('<br>', array_filter([$this->activeRecord->getAttributeHint($attributeName), $property->getDescription()])), // @TODO veryify if <br> or PHP_EOL (\n) works, redoc seems to work with <br/>
]);
}
return new Schema([
'type' => $type->getNoramlizeName(),
'items' => [
'type' => 'string'
],
'title' => $this->activeRecord->getAttributeLabel($attributeName),
'description' => implode('<br>', array_filter([$this->activeRecord->getAttributeHint($attributeName), $property->getDescription()])), // @TODO veryify if <br> or PHP_EOL (\n) works, redoc seems to work with <br/>
]);
}
Get Properties
public array getProperties ( $phpDocProperties = true ) | ||
$phpDocProperties |
public function getProperties($phpDocProperties = true)
{
$properties = [];
$attributeFields = [];
$fields = $this->activeRecord->fields();
if (!empty($fields)) {
foreach ($fields as $key => $value) {
if (is_numeric($key)) {
$attributeFields[] = $value;
} else {
$attributeFields[] = $key;
}
}
} else {
$attributeFields = $this->activeRecord->attributes();
}
foreach ($attributeFields as $attributeName) {
$properties[$attributeName] = $this->createSchema($attributeName);
}
if ($phpDocProperties) {
foreach ($this->phpDocParser->getProperties() as $prop) {
if (!array_key_exists($prop->getNormalizedName(), $properties)) {
$properties[$prop->getNormalizedName()] = $this->createSchema($prop->getNormalizedName());
}
}
}
return $properties;
}
protected void isCircularReference ( $class ) | ||
$class |
protected function isCircularReference($class)
{
// sender class is the same as the destination class, circular reference detected.
if (in_array($class, $this->senderActiveRecordClassName)) {
return true;
}
return trim(get_class($this->activeRecord), '\\') == trim($class, '\\');
}