Skip to content

Commit

Permalink
add withRegion/Language methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Gummibeer committed Apr 22, 2022
1 parent 5b958b0 commit cef812b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ return [
After that you can configure your language and region to be used by the package for some of the API requests.
By default we use `app()->getLocale()` for the language and a hardcoded `US` region.
It's recommended to call this in your `AppServiceProvider` but you can call the methods from everywhere in your codecase.
In case you want to run a specific callback with a region or language without changing the globally used ones you can use the `with` methods.
These will set the region or language to teh given one for the callback and automatically restore the old one after running the callback.

```php
use Astrotomic\Tmdb\Facades\Tmdb;

Tmdb::useLanguage('de');
Tmdb::useRegion('DE');

Tmdb::withLanguage('de', fn() => \Astrotomic\Tmdb\Models\Movie::find(335983));
Tmdb::withRegion('DE', fn() => \Astrotomic\Tmdb\Models\Movie::upcoming(20));
```

## Usage
Expand Down
2 changes: 2 additions & 0 deletions src/Facades/Tmdb.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
/**
* @method static \Astrotomic\Tmdb\Tmdb useRegion(string $region)
* @method static \Astrotomic\Tmdb\Tmdb useLanguage(string $language)
* @method static mixed withRegion(string $region, \Closure $callback)
* @method static mixed withLanguage(string $language, \Closure $callback)
* @method static string region()
* @method static string language()
*/
Expand Down
28 changes: 28 additions & 0 deletions src/Tmdb.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Astrotomic\Tmdb;

use Closure;

class Tmdb
{
protected string $region = 'US';
Expand All @@ -21,6 +23,32 @@ public function useLanguage(string $language): static
return $this;
}

public function withRegion(string $region, Closure $callback): mixed
{
$old = $this->region;

$this->useRegion($region);

$return = $callback();

$this->region = $old;

return $return ?? $this;
}

public function withLanguage(?string $language, Closure $callback): mixed
{
$old = $this->language;

$this->useLanguage($language);

$return = $callback();

$this->language = $old;

return $return ?? $this;
}

public function region(): string
{
return $this->region;
Expand Down
30 changes: 26 additions & 4 deletions tests/Unit/TmdbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,35 @@
it('can use a given language', function (): void {
$tmdb = new Tmdb();

expect($tmdb->useLanguage('de'))
->language()->toBe('de');
$tmdb->useLanguage('de');

expect($tmdb->language())->toBe('de');
});

it('can use a given region', function (): void {
$tmdb = new Tmdb();

expect($tmdb->useRegion('DE'))
->region()->toBe('DE');
$tmdb->useRegion('DE');

expect($tmdb->region())->toBe('DE');
});

it('can run a callback with a given language', function (): void {
$tmdb = new Tmdb();

$tmdb->useLanguage('en');

$tmdb->withLanguage('de', fn () => expect($tmdb->language())->toBe('de'));

expect($tmdb->language())->toBe('en');
});

it('can run a callback with a given region', function (): void {
$tmdb = new Tmdb();

$tmdb->useRegion('US');

$tmdb->withRegion('DE', fn () => expect($tmdb->region())->toBe('DE'));

expect($tmdb->region())->toBe('US');
});

0 comments on commit cef812b

Please sign in to comment.