Skip to content

Commit

Permalink
drop providers for model info and rely on native columns data
Browse files Browse the repository at this point in the history
  • Loading branch information
romalytvynenko committed Mar 9, 2024
1 parent 3c7718b commit d68f2b4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 80 deletions.
62 changes: 46 additions & 16 deletions src/Support/ResponseExtractor/ModelInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Dedoc\Scramble\Support\ResponseExtractor;

use BackedEnum;
use Dedoc\Scramble\Infer\Definition\ClassDefinition;
use Dedoc\Scramble\Infer\Definition\ClassPropertyDefinition;
use Dedoc\Scramble\Support\ResponseExtractor\ModelInfoProviders\NativeProvider;
use Dedoc\Scramble\Support\Type\ArrayType;
use Dedoc\Scramble\Support\Type\BooleanType;
use Dedoc\Scramble\Support\Type\FloatType;
Expand All @@ -22,7 +22,11 @@
use ReflectionClass;
use ReflectionMethod;
use SplFileObject;
use UnitEnum;

/**
* All the code here was written by the great Laravel team and community. Cudos to them.
*/
class ModelInfo
{
public static array $cache = [];
Expand Down Expand Up @@ -150,34 +154,60 @@ public function type()
*/
protected function getAttributes($model)
{
$provider = new NativeProvider();
$connection = $model->getConnection();
$schema = $connection->getSchemaBuilder();
$table = $model->getTable();
$columns = $schema->getColumns($table);
$indexes = $schema->getIndexes($table);

$attributes = collect($provider->getAttributes($model))
->map(function (array $attribute) use ($model) {
$attribute['hidden'] = $this->attributeIsHidden($attribute['name'], $model);
$attribute['cast'] = $this->getCastType($attribute['name'], $model);
return collect($columns)
->values()
->map(fn ($column) => [
'name' => $column['name'],
'type' => $column['type'],
'increments' => $column['auto_increment'],
'nullable' => $column['nullable'],
'default' => $this->getColumnDefault($column, $model),
'unique' => $this->columnIsUnique($column['name'], $indexes),
'fillable' => $model->isFillable($column['name']),
'appended' => null,
'hidden' => $this->attributeIsHidden($column['name'], $model),
'cast' => $this->getCastType($column['name'], $model),
])
->merge($this->getVirtualAttributes($model, $columns))
->keyBy('name');
}

return $attribute;
})
->toArray();
private function getColumnDefault($column, Model $model)
{
$attributeDefault = $model->getAttributes()[$column['name']] ?? null;

return collect($attributes)
->merge($this->getVirtualAttributes($model, $attributes))
->keyBy('name');
return match (true) {
$attributeDefault instanceof BackedEnum => $attributeDefault->value,
$attributeDefault instanceof UnitEnum => $attributeDefault->name,
default => $attributeDefault ?? $column['default'],
};
}

private function columnIsUnique($column, array $indexes)
{
return collect($indexes)->contains(
fn ($index) => count($index['columns']) === 1 && $index['columns'][0] === $column && $index['unique']
);
}

/**
* Get the virtual (non-column) attributes for the given model.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param array[] $attributes
* @param array[] $columns
* @return \Illuminate\Support\Collection
*/
protected function getVirtualAttributes($model, $attributes)
protected function getVirtualAttributes($model, $columns)
{
$class = new ReflectionClass($model);

$keyedAttributes = collect($attributes)->keyBy('name');
$keyedColumns = collect($columns)->keyBy('name');

return collect($class->getMethods())
->reject(
Expand All @@ -194,7 +224,7 @@ protected function getVirtualAttributes($model, $attributes)
return [];
}
})
->reject(fn ($cast, $name) => $keyedAttributes->has($name))
->reject(fn ($cast, $name) => $keyedColumns->has($name))
->map(fn ($cast, $name) => [
'name' => $name,
'type' => null,
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit d68f2b4

Please sign in to comment.