Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support non-Eloquent resource #17

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Builders/Paths/Operation/RequestBodyBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class RequestBodyBuilder extends Builder
];

public function __construct(
Generator $generator,
SchemaBuilder $schemaBuilder
Generator $generator,
SchemaBuilder $schemaBuilder
) {
parent::__construct($generator);
$this->schemaBuilder = $schemaBuilder;
Expand Down
12 changes: 6 additions & 6 deletions src/Builders/Paths/Operation/ResponseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class ResponseBuilder extends Builder
];

public function __construct(
Generator $generator,
ComponentsContainer $components,
SchemaBuilder $schemaBuilder
Generator $generator,
ComponentsContainer $components,
SchemaBuilder $schemaBuilder
) {
parent::__construct($generator);
$this->components = $components;
Expand All @@ -72,9 +72,9 @@ public function build(Route $route): array
* @return \GoldSpecDigital\ObjectOrientedOAS\Objects\Schema
*/
public static function buildResponse(
SchemaContract $data,
Schema $meta = null,
Schema $links = null
SchemaContract $data,
Schema $meta = null,
Schema $links = null
): Schema {
$jsonapi = Schema::object('jsonapi')
->properties(Schema::string('version')
Expand Down
20 changes: 10 additions & 10 deletions src/Builders/Paths/Operation/SchemaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class SchemaBuilder extends Builder
* @param \LaravelJsonApi\OpenApiSpec\ComponentsContainer $components
*/
public function __construct(
Generator $generator,
ComponentsContainer $components
Generator $generator,
ComponentsContainer $components
) {
parent::__construct($generator);
$this->components = $components;
Expand Down Expand Up @@ -69,9 +69,9 @@ public function build(Route $route, bool $isRequest = false): SchemaContract
* @return \GoldSpecDigital\ObjectOrientedOAS\Contracts\SchemaContract
*/
protected function buildResponseSchema(
Route $route,
SchemaDescriptorContract $descriptor,
string $objectId
Route $route,
SchemaDescriptorContract $descriptor,
string $objectId
): SchemaContract {
$method = $route->action();

Expand Down Expand Up @@ -142,9 +142,9 @@ protected function buildResponseSchema(
* @return \GoldSpecDigital\ObjectOrientedOAS\Contracts\SchemaContract
*/
protected function buildRequestSchema(
Route $route,
SchemaDescriptorContract $descriptor,
string $objectId
Route $route,
SchemaDescriptorContract $descriptor,
string $objectId
): SchemaContract {
$method = $route->action();
if ($route->isRelation()) {
Expand Down Expand Up @@ -184,8 +184,8 @@ protected function buildRequestSchema(
* @return string
*/
public static function objectId(
Route $route,
bool $isRequest = false
Route $route,
bool $isRequest = false
): string {
if ($isRequest) {
$method = $route->action();
Expand Down
4 changes: 2 additions & 2 deletions src/Builders/Paths/OperationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class OperationBuilder extends Builder
];

public function __construct(
Generator $generator,
ComponentsContainer $components
Generator $generator,
ComponentsContainer $components
) {
parent::__construct($generator);
$this->components = $components;
Expand Down
4 changes: 2 additions & 2 deletions src/Builders/PathsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class PathsBuilder extends Builder
protected OperationBuilder $operation;

public function __construct(
Generator $generator,
ComponentsContainer $components
Generator $generator,
ComponentsContainer $components
) {
parent::__construct($generator);
$this->components = $components;
Expand Down
10 changes: 5 additions & 5 deletions src/Descriptors/Actions/ActionDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ abstract class ActionDescriptor implements ActionDescriptorContract
protected Route $route;

public function __construct(
ParameterBuilder $parameterBuilder,
RequestBodyBuilder $requestBodyBuilder,
ResponseBuilder $responseBuilder,
Generator $generator,
Route $route
ParameterBuilder $parameterBuilder,
RequestBodyBuilder $requestBodyBuilder,
ResponseBuilder $responseBuilder,
Generator $generator,
Route $route
) {
$this->parameterBuilder = $parameterBuilder;
$this->requestBodyBuilder = $requestBodyBuilder;
Expand Down
6 changes: 3 additions & 3 deletions src/Descriptors/Requests/RequestDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ abstract class RequestDescriptor extends Descriptor implements RequestDescriptor
protected SchemaBuilder $schemaBuilder;

public function __construct(
Generator $generator,
Route $route,
SchemaBuilder $schemaBuilder
Generator $generator,
Route $route,
SchemaBuilder $schemaBuilder
) {
parent::__construct($generator);
$this->route = $route;
Expand Down
8 changes: 4 additions & 4 deletions src/Descriptors/Responses/ResponseDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ abstract class ResponseDescriptor extends Descriptor implements ResponseDescript
protected bool $validates = false;

public function __construct(
Generator $generator,
Route $route,
SchemaBuilder $schemaBuilder,
Collection $defaults
Generator $generator,
Route $route,
SchemaBuilder $schemaBuilder,
Collection $defaults
) {
parent::__construct($generator);
$this->route = $route;
Expand Down
72 changes: 37 additions & 35 deletions src/Descriptors/Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
use GoldSpecDigital\ObjectOrientedOAS\Objects\Schema as OASchema;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use LaravelJsonApi\Contracts\Schema\Attribute as AttributeContract;
use LaravelJsonApi\Contracts\Schema\Field;
use LaravelJsonApi\Contracts\Schema\PolymorphicRelation;
use LaravelJsonApi\Contracts\Schema\Relation as RelationContract;
use LaravelJsonApi\Contracts\Schema\Schema as JASchema;
use LaravelJsonApi\Contracts\Schema\Sortable;
use LaravelJsonApi\Core\Resources\JsonApiResource;
use LaravelJsonApi\Core\Support\Str;
use LaravelJsonApi\Eloquent;
use LaravelJsonApi\Eloquent\Fields\ArrayHash;
use LaravelJsonApi\Eloquent\Fields\ArrayList;
use LaravelJsonApi\Eloquent\Fields\Attribute;
use LaravelJsonApi\Eloquent\Fields\Boolean;
use LaravelJsonApi\Eloquent\Fields\ID;
use LaravelJsonApi\Eloquent\Fields\Map;
use LaravelJsonApi\Eloquent\Fields\Number;
use LaravelJsonApi\Eloquent\Fields\Relations\Relation;
use LaravelJsonApi\Eloquent\Pagination\CursorPagination;
use LaravelJsonApi\Eloquent\Pagination\PagePagination;
use LaravelJsonApi\OpenApiSpec\Builders\Paths\Operation\SchemaBuilder;
Expand Down Expand Up @@ -53,10 +53,10 @@ class Schema extends Descriptor implements SchemaDescriptor, SortablesDescriptor
* @return \GoldSpecDigital\ObjectOrientedOAS\Objects\Schema
*/
public function fetch(
JASchema $schema,
string $objectId,
string $type,
string $name
JASchema $schema,
string $objectId,
string $type,
string $name
): OASchema {
$resource = $this->generator
->resources()
Expand Down Expand Up @@ -224,8 +224,8 @@ public function detachRelationship(Route $route): OASchema
* @throws \GoldSpecDigital\ObjectOrientedOAS\Exceptions\InvalidArgumentException
*/
public function fetchPolymorphicRelationship(
Route $route,
$objectId
Route $route,
$objectId
): OASchema {
$resource = $this->generator->resources()
->resource($route->schema()::model());
Expand Down Expand Up @@ -341,16 +341,16 @@ public function filters($route): array
* @return \Illuminate\Support\Collection
*/
protected function fields(
array $fields,
JsonApiResource $resource
array $fields,
JsonApiResource $resource
): Collection {
return collect($fields)
->mapToGroups(function (Field $field) {
switch (true) {
case $field instanceof Attribute:
case $field instanceof AttributeContract:
$key = 'attributes';
break;
case $field instanceof Relation:
case $field instanceof RelationContract:
$key = 'relationships';
break;
default:
Expand All @@ -375,8 +375,8 @@ protected function fields(
* @return Schema[]
*/
protected function attributes(
Collection $fields,
JsonApiResource $example
Collection $fields,
JsonApiResource $example
): array {
return $fields
->filter(fn ($field) => !($field instanceof ID))
Expand All @@ -402,11 +402,11 @@ protected function attributes(

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

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

Expand All @@ -423,28 +423,28 @@ protected function attributes(
* @todo Fix relation field names
*/
protected function relationships(
Collection $relationships,
JsonApiResource $example
Collection $relationships,
JsonApiResource $example
): array {
return $relationships
->map(function (Relation $relation) use ($example) {
->map(function (RelationContract $relation) use ($example) {
return $this->relationship($relation, $example);
})->toArray();
}

/**
* @param \LaravelJsonApi\Eloquent\Fields\Relations\Relation $relation
* @param \LaravelJsonApi\Core\Resources\JsonApiResource $example
* @param bool $includeData
* @param \LaravelJsonApi\Contracts\Schema\Relation $relation
* @param \LaravelJsonApi\Core\Resources\JsonApiResource $example
* @param bool $includeData
*
* @throws \GoldSpecDigital\ObjectOrientedOAS\Exceptions\InvalidArgumentException
*
* @return \GoldSpecDigital\ObjectOrientedOAS\Objects\Schema
*/
protected function relationship(
Relation $relation,
JsonApiResource $example,
bool $includeData = false
RelationContract $relation,
JsonApiResource $example,
bool $includeData = false
): OASchema {
$fieldId = $relation->name();

Expand All @@ -469,18 +469,18 @@ protected function relationship(
}

/**
* @param \LaravelJsonApi\Eloquent\Fields\Relations\Relation $relation
* @param \LaravelJsonApi\Core\Resources\JsonApiResource $example
* @param string $type
* @param \LaravelJsonApi\Contracts\Schema\Relation $relation
* @param \LaravelJsonApi\Core\Resources\JsonApiResource $example
* @param string $type
*
* @throws \GoldSpecDigital\ObjectOrientedOAS\Exceptions\InvalidArgumentException
*
* @return \GoldSpecDigital\ObjectOrientedOAS\Objects\Schema
*/
protected function relationshipData(
Relation $relation,
JsonApiResource $example,
string $type
RelationContract $relation,
JsonApiResource $example,
string $type
): OASchema {
if ($relation instanceof PolymorphicRelation) {
// @todo Add examples for each available type
Expand Down Expand Up @@ -519,12 +519,14 @@ protected function relationshipData(
* @return \GoldSpecDigital\ObjectOrientedOAS\Objects\Schema
*/
public function relationshipLinks(
$relation,
JsonApiResource $example,
string $type
$relation,
JsonApiResource $example,
string $type
): OASchema {
$name = Str::dasherize(
Str::plural($relation->relationName())
Str::plural(method_exists($relation, 'relationName')
? $relation->relationName()
: Str::camel($relation->name()))
);

/*
Expand Down
34 changes: 17 additions & 17 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,28 +279,28 @@ public function action(): string
}

public static function belongsTo(
IlluminateRoute $route,
Server $server
IlluminateRoute $route,
Server $server
): bool {
return Str::contains(
$route->getName(),
$server->name(),
);
}

protected function setUriForRoute(): void
{
$domain = URL::to('/');
$serverBasePath = str_replace(
$domain,
'',
$this->server->url(),
);

$this->uri = str_replace(
$serverBasePath,
'',
'/'.$this->route->uri(),
);
}
protected function setUriForRoute(): void
{
$domain = URL::to('/');
$serverBasePath = str_replace(
$domain,
'',
$this->server->url(),
);

$this->uri = str_replace(
$serverBasePath,
'',
'/'.$this->route->uri(),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,6 @@ public function down(): void
Schema::dropIfExists('tags');
Schema::dropIfExists('comments');
Schema::dropIfExists('videos');
// Schema::dropIfExists('posts');
// Schema::dropIfExists('posts');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something we should fix in the entire codebase. The entire project does not adhere to our code standards. So every PR gets a load of these formatting issues that need to be resolved. It's something that just needs to be done once. You can have a go at it if you want.

}
}