Skip to content

Commit

Permalink
Adding extra data to filters. (#311)
Browse files Browse the repository at this point in the history
* Adding extra data to filters.

* Apply fixes from StyleCI (#312)

* tests
  • Loading branch information
binaryk authored Dec 17, 2020
1 parent 3db960a commit 609bed8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
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',
],
],
],
]);
}
}

0 comments on commit 609bed8

Please sign in to comment.