Skip to content
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
21 changes: 17 additions & 4 deletions src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ abstract class Filter implements JsonSerializable

public $relatedRepositoryKey;

public $relatedRepositoryTitle;

public Repository $repository;

public function __construct()
Expand Down Expand Up @@ -114,19 +116,31 @@ public function setRelatedRepositoryKey(string $repositoryKey): self
return $this;
}

public function setRelatedRepositoryTitle(string $title): self
{
$this->relatedRepositoryTitle = $title;

return $this;
}

public function setRepository(Repository $repository): self
{
$this->repository = $repository;

return $this;
}

public function getRelatedRepositoryUrl(): ?string
public function getRelatedRepository(): ?array
{
return ($key = $this->getRelatedRepositoryKey())
? with(Restify::repositoryForKey($key), function ($repository = null) {
if (is_subclass_of($repository, Repository::class)) {
return Restify::path($repository::uriKey());
return [
'key' => $repository::uriKey(),
'url' => Restify::path($repository::uriKey()),
'display_key' => $this->relatedRepositoryTitle ?? $repository::$title,
'label' => $repository::label(),
];
}
})
: null;
Expand Down Expand Up @@ -161,8 +175,7 @@ public function jsonSerialize()
], function (array $initial) {
return $this->relatedRepositoryKey
? array_merge($initial, [
'related_repository_key' => $this->getRelatedRepositoryKey(),
'related_repository_url' => $this->getRelatedRepositoryUrl(),
'repository' => $this->getRelatedRepository(),
])
: $initial;
});
Expand Down
28 changes: 27 additions & 1 deletion tests/Feature/Filters/FilterDefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,33 @@ public function test_filters_can_have_definition()

$this->getJson('posts/filters?only=matches,searchables,sortables')
->assertJsonFragment([
'related_repository_key' => 'users',
'key' => 'users',
]);
}

public function test_match_definitions_includes_title()
{
PostRepository::$match = [
'user_id' => MatchFilter::make()
->setType('int')
->setRelatedRepositoryKey(UserRepository::uriKey()),

'title' => 'string',
];

$this->getJson('posts/filters?only=matches')
->dump()
->assertJsonStructure([
'data' => [
[
'repository' => [
'key',
'url',
'display_key',
'label',
],
],
],
]);
}
}