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

feat: added support for passing locale on multiple HasTags methods #488

Closed
wants to merge 5 commits into from
Closed
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ NewsItem::withAllTags(['first tag', 'second tag'])->get();
// retrieve models that don't have any of the given tags
NewsItem::withoutTags(['first tag', 'second tag'])->get();

// force tag locale (useful when you have a multi-language app but don't want to translate tags)
NewsItem::withAnyTags(['first tag', 'second tag'], locale: 'en')->get();
NewsItem::withAllTags(['first tag', 'second tag'], locale: 'en')->get();
NewsItem::withoutTags(['first tag', 'second tag'], locale: 'en')->get();

// translating a tag
$tag = Tag::findOrCreate('my tag');
$tag->setTranslation('name', 'fr', 'mon tag');
Expand Down
29 changes: 19 additions & 10 deletions src/HasTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ public function scopeWithAllTags(
Builder $query,
string | array | ArrayAccess | Tag $tags,
string $type = null,
string $locale = null
): Builder {
$tags = static::convertToTags($tags, $type);
$tags = static::convertToTags($tags, $type, $locale);

collect($tags)->each(function ($tag) use ($query) {
$query->whereHas('tags', function (Builder $query) use ($tag) {
Expand All @@ -106,8 +107,9 @@ public function scopeWithAnyTags(
Builder $query,
string | array | ArrayAccess | Tag $tags,
string $type = null,
string $locale = null
): Builder {
$tags = static::convertToTags($tags, $type);
$tags = static::convertToTags($tags, $type, $locale);

return $query
->whereHas('tags', function (Builder $query) use ($tags) {
Expand All @@ -120,9 +122,10 @@ public function scopeWithAnyTags(
public function scopeWithoutTags(
Builder $query,
string | array | ArrayAccess | Tag $tags,
string $type = null
string $type = null,
string $locale = null
): Builder {
$tags = static::convertToTags($tags, $type);
$tags = static::convertToTags($tags, $type, $locale);

return $query
->whereDoesntHave('tags', function (Builder $query) use ($tags) {
Expand All @@ -132,9 +135,12 @@ public function scopeWithoutTags(
});
}

public function scopeWithAllTagsOfAnyType(Builder $query, $tags): Builder
{
$tags = static::convertToTagsOfAnyType($tags);
public function scopeWithAllTagsOfAnyType(
Builder $query,
$tags,
string $locale = null
): Builder {
$tags = static::convertToTagsOfAnyType($tags, $locale);

collect($tags)
->each(function ($tag) use ($query) {
Expand All @@ -147,9 +153,12 @@ public function scopeWithAllTagsOfAnyType(Builder $query, $tags): Builder
return $query;
}

public function scopeWithAnyTagsOfAnyType(Builder $query, $tags): Builder
{
$tags = static::convertToTagsOfAnyType($tags);
public function scopeWithAnyTagsOfAnyType(
Builder $query,
$tags,
string $locale = null
): Builder {
$tags = static::convertToTagsOfAnyType($tags, $locale);

$tagIds = collect($tags)->pluck('id');

Expand Down
44 changes: 44 additions & 0 deletions tests/HasTagsTranslatedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,47 @@
expect($translated['name'][$locale])->toEqual($translated['name_translated']);
expect($translated['slug'][$locale])->toEqual($translated['slug_translated']);
});

it('retrieves English tags with tagsTranslated method regardless of application locale', function () {
$testModel = TestModel::create(['name' => 'default']);
$testModel->attachTag('Test Tag');

app()->setLocale('fr');

$tags = $testModel->tagsTranslated('en')->get();
expect($tags->first()->name_translated)->toEqual('Test Tag');
});

it('filters models with any English tags regardless of application locale', function () {
$testModel = TestModel::create(['name' => 'default']);
$testModel->attachTag('Test Tag');

app()->setLocale('fr');

$modelsWithEnglishTag = TestModel::withAnyTags('Test Tag', null, 'en')->get();
expect($modelsWithEnglishTag)->toHaveCount(1);
expect($modelsWithEnglishTag->first()->name)->toEqual('default');
});

it('filters models with all specified English tags regardless of application locale', function () {
$testModel = TestModel::create(['name' => 'default']);
$testModel->attachTag('Test Tag');
$testModel->attachTag('Another Tag');

app()->setLocale('fr');

$modelsWithAllTags = TestModel::withAllTags(['Test Tag', 'Another Tag'], null, 'en')->get();
expect($modelsWithAllTags)->toHaveCount(1);
expect($modelsWithAllTags->first()->name)->toEqual('default');
});

it('excludes models with specified English tags regardless of application locale', function () {
TestModel::create(['name' => 'other'])->attachTag('Different Tag');
$testModel = TestModel::create(['name' => 'default']);
$testModel->attachTag('Test Tag');

app()->setLocale('fr');

$modelsWithoutTag = TestModel::withoutTags('Test Tag', null, 'en')->get();
expect($modelsWithoutTag)->each->not->toHaveProperty('name', 'default');
});