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

Support Route Scope Binding #38

Merged
merged 3 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 25 additions & 0 deletions src/Traits/OptimusEncodedRouteKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,31 @@ public function resolveRouteBinding($value, $field = null)
return $this->where($field, $value)->first();
}

/**
* Retrieve the model for a bound value.
*
* @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation $query
* @param mixed $value
* @param string|null $field
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function resolveRouteBindingQuery($query, $value, $field = null)
{
if ($field === null) {
$field = $query->getRouteKeyName();
}

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

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

return $query->where($field ?? $this->getRouteKeyName(), $value);
ahmedbally marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Get the Optimus instance.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddParentIdToUsersTable extends Migration
{
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->foreignId('parent_id')
->nullable();
});
}

public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['parent_id']);
});
}
}
12 changes: 12 additions & 0 deletions tests/Stubs/Models/UserWithCustomOptimusConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Cog\Laravel\Optimus\Traits\OptimusEncodedRouteKey;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

final class UserWithCustomOptimusConnection extends Model
{
Expand All @@ -14,4 +16,14 @@ final class UserWithCustomOptimusConnection extends Model
protected $table = 'users';

protected $guarded = [];

public function nestedUsers(): HasMany
{
return $this->hasMany(UserWithCustomOptimusConnection::class, 'parent_id');
}

public function parentUser(): BelongsTo
{
return$this->belongsTo(UserWithCustomOptimusConnection::class, 'parent_id');
}
}
12 changes: 12 additions & 0 deletions tests/Stubs/Models/UserWithDefaultOptimusConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Cog\Laravel\Optimus\Traits\OptimusEncodedRouteKey;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

final class UserWithDefaultOptimusConnection extends Model
{
Expand All @@ -12,4 +14,14 @@ final class UserWithDefaultOptimusConnection extends Model
protected $table = 'users';

protected $guarded = [];

public function nestedUsers(): HasMany
{
return $this->hasMany(UserWithDefaultOptimusConnection::class, 'parent_id');
}

public function parentUser(): BelongsTo
{
return$this->belongsTo(UserWithDefaultOptimusConnection::class, 'parent_id');
}
}
19 changes: 16 additions & 3 deletions tests/Traits/OptimusEncodedRouteKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ protected function setUp(): void
parent::setUp();

$this->loadLaravelMigrations(config('database.default'));
$this->loadMigrationsFrom(__DIR__.'/../Stubs/Migrations');
$this->configurePrimeNumbers();
}

Expand All @@ -51,6 +52,18 @@ public function testOptimusConnectionCanBeConfigured(): void
$this->assertNotEquals($incorrectEncodedId, $routeKey);
}

public function testResolveChildRouteWithEncodedKey(): void
{
$user = $this->createUserWithDefaultOptimusConnection();
$nestedUser = $this->createUserWithDefaultOptimusConnection([
'email' => 'test1@user.com',
]);
$nestedUser->parentUser()->associate($user)->save();
$resolvedNestedUser = $user->resolveChildRouteBinding('nested_user', $nestedUser->getRouteKey(), 'id');

$this->assertEquals($nestedUser->id, $resolvedNestedUser->id);
}

public function testResolveModelWithEncodedKey(): void
{
$user = $this->createUserWithDefaultOptimusConnection();
Expand Down Expand Up @@ -172,13 +185,13 @@ public function testExistingIntegerValuesBelow256AreResolved()
*
* @return \Cog\Tests\Laravel\Optimus\Stubs\Models\UserWithDefaultOptimusConnection
*/
protected function createUserWithDefaultOptimusConnection(): UserWithDefaultOptimusConnection
protected function createUserWithDefaultOptimusConnection(array $data = []): UserWithDefaultOptimusConnection
{
return UserWithDefaultOptimusConnection::create([
return UserWithDefaultOptimusConnection::create(array_merge([
'name' => 'Default Test User',
'email' => 'test@user.com',
'password' => 'p4ssw0rd',
]);
], $data));
}

/**
Expand Down