Skip to content

Commit 609bed8

Browse files
authored
Adding extra data to filters. (#311)
* Adding extra data to filters. * Apply fixes from StyleCI (#312) * tests
1 parent 3db960a commit 609bed8

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

src/Filter.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ abstract class Filter implements JsonSerializable
2626

2727
public $relatedRepositoryKey;
2828

29+
public $relatedRepositoryTitle;
30+
2931
public Repository $repository;
3032

3133
public function __construct()
@@ -114,19 +116,31 @@ public function setRelatedRepositoryKey(string $repositoryKey): self
114116
return $this;
115117
}
116118

119+
public function setRelatedRepositoryTitle(string $title): self
120+
{
121+
$this->relatedRepositoryTitle = $title;
122+
123+
return $this;
124+
}
125+
117126
public function setRepository(Repository $repository): self
118127
{
119128
$this->repository = $repository;
120129

121130
return $this;
122131
}
123132

124-
public function getRelatedRepositoryUrl(): ?string
133+
public function getRelatedRepository(): ?array
125134
{
126135
return ($key = $this->getRelatedRepositoryKey())
127136
? with(Restify::repositoryForKey($key), function ($repository = null) {
128137
if (is_subclass_of($repository, Repository::class)) {
129-
return Restify::path($repository::uriKey());
138+
return [
139+
'key' => $repository::uriKey(),
140+
'url' => Restify::path($repository::uriKey()),
141+
'display_key' => $this->relatedRepositoryTitle ?? $repository::$title,
142+
'label' => $repository::label(),
143+
];
130144
}
131145
})
132146
: null;
@@ -161,8 +175,7 @@ public function jsonSerialize()
161175
], function (array $initial) {
162176
return $this->relatedRepositoryKey
163177
? array_merge($initial, [
164-
'related_repository_key' => $this->getRelatedRepositoryKey(),
165-
'related_repository_url' => $this->getRelatedRepositoryUrl(),
178+
'repository' => $this->getRelatedRepository(),
166179
])
167180
: $initial;
168181
});

tests/Feature/Filters/FilterDefinitionTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,33 @@ public function test_filters_can_have_definition()
3131

3232
$this->getJson('posts/filters?only=matches,searchables,sortables')
3333
->assertJsonFragment([
34-
'related_repository_key' => 'users',
34+
'key' => 'users',
35+
]);
36+
}
37+
38+
public function test_match_definitions_includes_title()
39+
{
40+
PostRepository::$match = [
41+
'user_id' => MatchFilter::make()
42+
->setType('int')
43+
->setRelatedRepositoryKey(UserRepository::uriKey()),
44+
45+
'title' => 'string',
46+
];
47+
48+
$this->getJson('posts/filters?only=matches')
49+
->dump()
50+
->assertJsonStructure([
51+
'data' => [
52+
[
53+
'repository' => [
54+
'key',
55+
'url',
56+
'display_key',
57+
'label',
58+
],
59+
],
60+
],
3561
]);
3662
}
3763
}

0 commit comments

Comments
 (0)