Skip to content

Commit

Permalink
refactor: clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
Rocksheep committed Oct 24, 2024
1 parent 085f054 commit 47934db
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions src/Descriptors/Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
use LaravelJsonApi\Eloquent;
use LaravelJsonApi\Eloquent\Fields\ArrayHash;
use LaravelJsonApi\Eloquent\Fields\ArrayList;
use LaravelJsonApi\Eloquent\Fields\Attribute as EloquentAttribute;
use LaravelJsonApi\Eloquent\Fields\Boolean;
use LaravelJsonApi\Eloquent\Fields\ID;
use LaravelJsonApi\Eloquent\Fields\Map;
use LaravelJsonApi\Eloquent\Fields\Number;
use LaravelJsonApi\Eloquent\Pagination\CursorPagination;
use LaravelJsonApi\Eloquent\Pagination\PagePagination;
use LaravelJsonApi\NonEloquent\Fields\Attribute;
use LaravelJsonApi\NonEloquent\Fields\Attribute as NonEloquentAttribute;
use LaravelJsonApi\OpenApiSpec\Builders\Paths\Operation\SchemaBuilder;
use LaravelJsonApi\OpenApiSpec\Contracts\Descriptors\Schema\PaginationDescriptor;
use LaravelJsonApi\OpenApiSpec\Contracts\Descriptors\Schema\SortablesDescriptor;
Expand Down Expand Up @@ -404,24 +405,17 @@ protected function attributes(

$schema = $fieldDataType->title($field->name());

try {
$column = $field instanceof Eloquent\Fields\Attribute ? $field->column() : $field->name();

if ($field instanceof Attribute) {
$attributes = $example->attributes(null);
if (isset($attributes[$column])) {
$schema = $schema->example($attributes[$column]);
}
} else {
if (isset($example[$column])) {
$schema = $schema->example($example[$column]);
}
if (method_exists($field, 'isReadOnly') && $field->isReadOnly(null)) {
$schema = $schema->readOnly(true);
}
$column = $field instanceof EloquentAttribute ? $field->column() : $field->name();

if ($field instanceof NonEloquentAttribute) {
$attributes = $example->attributes(null);
if (isset($attributes[$column])) {
$schema = $schema->example($attributes[$column]);
}
} else {
if (isset($example[$column])) {
$schema = $schema->example($example[$column]);
}
} catch (\Throwable $e) {
throw $e;
}

return $schema;
Expand Down Expand Up @@ -538,9 +532,11 @@ public function relationshipLinks(
string $type,
): OASchema {
$name = Str::dasherize(
Str::plural(method_exists($relation, 'relationName')
? $relation->relationName()
: Str::camel($relation->name()))
Str::plural(
method_exists($relation, 'relationName')
? $relation->relationName()
: Str::camel($relation->name())
)
);

/*
Expand Down

0 comments on commit 47934db

Please sign in to comment.