Skip to content

Commit

Permalink
Allow to use default route key name in custom router param (#31)
Browse files Browse the repository at this point in the history
Allow to use default route key name in custom router param
  • Loading branch information
antonkomarev authored May 26, 2021
1 parent 94c9731 commit 20c0b94
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to `cybercog/laravel-optimus` will be documented in this fil

## [Unreleased]

## [3.7.0] - 2021-05-26

### Added

- ([#30]) Added custom route key support
- ([#31]) Added default custom route key support

## [3.6.0] - 2020-12-31

### Added
Expand Down Expand Up @@ -87,7 +94,8 @@ All notable changes to `cybercog/laravel-optimus` will be documented in this fil

Initial release

[Unreleased]: https://github.com/cybercog/laravel-optimus/compare/3.6.0...master
[Unreleased]: https://github.com/cybercog/laravel-optimus/compare/3.7.0...master
[3.7.0]: https://github.com/cybercog/laravel-optimus/compare/3.6.0...3.7.0
[3.6.0]: https://github.com/cybercog/laravel-optimus/compare/3.5.0...3.6.0
[3.5.0]: https://github.com/cybercog/laravel-optimus/compare/3.4.2...3.5.0
[3.4.2]: https://github.com/cybercog/laravel-optimus/compare/3.4.1...3.4.2
Expand All @@ -100,6 +108,8 @@ Initial release
[2.1.0]: https://github.com/cybercog/laravel-optimus/compare/2.0.0...2.1.0
[2.0.0]: https://github.com/cybercog/laravel-optimus/compare/1.0.0...2.0.0

[#31]: https://github.com/cybercog/laravel-optimus/pull/31
[#30]: https://github.com/cybercog/laravel-optimus/pull/30
[#27]: https://github.com/cybercog/laravel-optimus/pull/27
[#25]: https://github.com/cybercog/laravel-optimus/pull/25
[#23]: https://github.com/cybercog/laravel-optimus/pull/23
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ If you discover any security related issues, please email anton@komarev.com inst

## Contributors

| <a href="https://github.com/antonkomarev">![@antonkomarev](https://avatars.githubusercontent.com/u/1849174?s=110)<br />Anton Komarev</a> | <a href="https://github.com/ivanvermeyen">![@ivanvermeyen](https://avatars.githubusercontent.com/u/3598622?s=110)<br />Ivan Vermeyen</a> | <a href="https://github.com/tur-nr">![@tur-nr](https://avatars.githubusercontent.com/u/817134?s=110)<br />Christopher Turner</a> |
| :---: | :---: | :---: |
| <a href="https://github.com/antonkomarev">![@antonkomarev](https://avatars.githubusercontent.com/u/1849174?s=110)<br />Anton Komarev</a> | <a href="https://github.com/ivanvermeyen">![@ivanvermeyen](https://avatars.githubusercontent.com/u/3598622?s=110)<br />Ivan Vermeyen</a> | <a href="https://github.com/tur-nr">![@tur-nr](https://avatars.githubusercontent.com/u/817134?s=110)<br />Christopher Turner</a> | <a href="https://github.com/ahmedbally">![@ahmedbally](https://avatars.githubusercontent.com/u/20849424?s=110)<br />Ahmed Bally</a> |
| :---: | :---: | :---: | :---: |

[Laravel Optimus contributors list](../../contributors)

Expand Down
8 changes: 6 additions & 2 deletions src/Traits/OptimusEncodedRouteKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ public function getRouteKey()
*/
public function resolveRouteBinding($value, $field = null)
{
if ($field === null) {
$field = $this->getRouteKeyName();
}

if (is_string($value) && ctype_digit($value)) {
$value = (int) $value;
}

if (is_int($value) && is_null($field)) {
if (is_int($value) && $field === $this->getRouteKeyName()) {
$value = $this->getOptimus()->decode($value);
}

return $this->where($field ?? $this->getRouteKeyName(), $value)->first();
return $this->where($field, $value)->first();
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/Traits/OptimusEncodedRouteKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ public function testResolveModelWithEncodedKeyWithCustomKey(): void
$this->assertEquals($user->id, $resolvedUser->id);
}

public function testResolveModelWithEncodedKeyWithCustomIdKey(): void
{
$user = $this->createUserWithDefaultOptimusConnection();
$encodedId = $user->getRouteKey();
$resolvedUser = $user->resolveRouteBinding($encodedId, 'id');

$this->assertSame($user->id, $resolvedUser->id);
}

public function testEncodedKeyIsUsedForRouteModelBinding(): void
{
$user = $this->createUserWithDefaultOptimusConnection();
Expand Down

0 comments on commit 20c0b94

Please sign in to comment.