Skip to content
Merged
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
58 changes: 45 additions & 13 deletions src/PowerJoinClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Str;
use InvalidArgumentException;
use ReflectionClass;

class PowerJoinClause extends JoinClause
{
Expand Down Expand Up @@ -282,27 +283,58 @@ public function joinType(string $joinType): self

public function __call($name, $arguments)
{
$scope = 'scope'.ucfirst($name);

if (!$this->getModel()) {
return;
}

if (method_exists($this->getModel(), $scope)) {
if (method_exists($this->getModel(), 'scope'.ucfirst($name))) {
$scope = 'scope'.ucfirst($name);

return $this->getModel()->{$scope}($this, ...$arguments);
} else {
if (static::hasMacro($name)) {
return $this->macroCall($name, $arguments);
}
}

$eloquentBuilder = $this->getModel()->newEloquentBuilder($this);
if (method_exists($eloquentBuilder, $name)) {
$eloquentBuilder->setModel($this->getModel());
if ($this->hasLaravelScopeAttribute($name) && version_compare(app()->version(), '12.0.0', '>=')) {
return $this->getModel()->callNamedScope($name, array_merge([$this], $arguments));
}

return $eloquentBuilder->{$name}(...$arguments);
}
if (static::hasMacro($name)) {
return $this->macroCall($name, $arguments);
}

$eloquentBuilder = $this->getModel()->newEloquentBuilder($this);
if (method_exists($eloquentBuilder, $name)) {
$eloquentBuilder->setModel($this->getModel());

throw new InvalidArgumentException(sprintf('Method %s does not exist in PowerJoinClause class', $name));
return $eloquentBuilder->{$name}(...$arguments);
}

throw new InvalidArgumentException(sprintf('Method %s does not exist in PowerJoinClause class', $name));
}

/**
* Check if a method has the Laravel Scope attribute.
*/
protected function hasLaravelScopeAttribute(string $methodName): bool
{
if (!method_exists($this->getModel(), $methodName)) {
return false;
}

$reflection = new ReflectionClass($this->getModel());

if (!$reflection->hasMethod($methodName)) {
return false;
}

$method = $reflection->getMethod($methodName);
$attributes = $method->getAttributes();

foreach ($attributes as $attribute) {
if ($attribute->getName() === 'Illuminate\Database\Eloquent\Attributes\Scope') {
return true;
}
}

return false;
}
}
50 changes: 50 additions & 0 deletions tests/LaravelScopeAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Kirschbaum\PowerJoins\Tests;

use Kirschbaum\PowerJoins\Tests\Models\Address;
use Kirschbaum\PowerJoins\Tests\Models\City;
use Kirschbaum\PowerJoins\Tests\Models\Country;
use Kirschbaum\PowerJoins\Tests\Models\CountryEnum;

class LaravelScopeAttributeTest extends TestCase
{
/**
* @test
*/
public function it_can_use_laravel_scope_attribute_in_join_relationship_callback()
{
if (version_compare(app()->version(), '12.0.0', '<')) {
$this->markTestSkipped('Laravel 12+ is required for this test');
}

// Create test data
$denmark = Country::create(['name' => 'Denmark', 'iso' => 'DK']);
$usa = Country::create(['name' => 'United States', 'iso' => 'US']);

$copenhagen = City::create(['name' => 'Copenhagen', 'country_id' => $denmark->id]);
$newyork = City::create(['name' => 'New York', 'country_id' => $usa->id]);

$addressInDenmark = Address::create([
'kvh_code' => 'DK001',
'name' => 'Danish Address',
'city_id' => $copenhagen->id,
]);

$addressInUSA = Address::create([
'kvh_code' => 'US001',
'name' => 'US Address',
'city_id' => $newyork->id,
]);

// This should work but currently fails with "Method inCountry does not exist in PowerJoinClause class"
$result = Address::query()
->joinRelationship('city', function ($join) {
$join->inCountry(CountryEnum::DK);
})
->first();

$this->assertNotNull($result);
$this->assertEquals('DK001', $result->kvh_code);
}
}
7 changes: 7 additions & 0 deletions tests/Models/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kirschbaum\PowerJoins\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand All @@ -16,8 +17,14 @@ class Address extends Model
protected $fillable = [
'kvh_code',
'name',
'city_id',
];

public function city(): BelongsTo
{
return $this->belongsTo(City::class);
}

public function requested_addresses(): HasMany
{
return $this->hasMany(RequestedAddress::class, 'kvh_code', 'kvh_code');
Expand Down
35 changes: 35 additions & 0 deletions tests/Models/City.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Kirschbaum\PowerJoins\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class City extends Model
{
protected $table = 'cities';

protected $fillable = [
'name',
'country_id',
];

public function country(): BelongsTo
{
return $this->belongsTo(Country::class);
}

public function addresses(): HasMany
{
return $this->hasMany(Address::class);
}

#[\Illuminate\Database\Eloquent\Attributes\Scope]
public function inCountry($query, CountryEnum $country)
{
$countryId = Country::select('id')->where('iso', $country->value)->valueOrFail('id');

return $query->where('cities.country_id', $countryId);
}
}
21 changes: 21 additions & 0 deletions tests/Models/Country.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Kirschbaum\PowerJoins\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Country extends Model
{
protected $table = 'countries';

protected $fillable = [
'name',
'iso',
];

public function cities(): HasMany
{
return $this->hasMany(City::class);
}
}
10 changes: 10 additions & 0 deletions tests/Models/CountryEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Kirschbaum\PowerJoins\Tests\Models;

enum CountryEnum: string
{
case DK = 'DK';
case US = 'US';
case GB = 'GB';
}
15 changes: 15 additions & 0 deletions tests/database/migrations/2020_03_16_000000_create_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public function up()
$table->increments('id');
$table->string('kvh_code')->unique();
$table->string('name');
$table->unsignedInteger('city_id')->nullable();
$table->timestamps();
$table->softDeletes();
});
Expand All @@ -129,6 +130,20 @@ public function up()

$table->index('kvh_code');
});

Schema::create('countries', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('iso', 2)->unique();
$table->timestamps();
});

Schema::create('cities', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->unsignedInteger('country_id');
$table->timestamps();
});
}

/**
Expand Down