Class luya\admin\openapi\specs\ActiveRecordToSchema

Inheritanceluya\admin\openapi\specs\ActiveRecordToSchema
Available since version3.2.0
Source Code https://github.com/luyadev/luya-module-admin/blob/master/src/openapi/specs/ActiveRecordToSchema.php

Generate Schema Specs from Active Record.

Property Details

Hide inherited properties

$activeRecord protected property
protected yii\base\Model $activeRecord null
$baseSpecs protected property
$phpDocParser protected property
$senderActiveRecordClassName protected property

Contains the class which was the origin creator of the active record schema, this can be usused to determine circular references.

Method Details

Hide inherited methods

__construct() public method

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;
}

            
createSchema() public method

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/>
    ]);
}

            
getProperties() public method

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;
}

            
isCircularReference() protected method

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, '\\');
}