Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Mar 14, 2024
1 parent 33b9b6b commit 31c19b9
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/HasJsonRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ trait HasJsonRelationships
*/
public function getAttribute($key)
{
$attribute = preg_split('/(->|\[\])/', $key)[0];
$attribute = preg_split('/(->|\[])/', $key)[0];

if (array_key_exists($attribute, $this->attributes)) {
return $this->getAttributeValue($key);
Expand Down
20 changes: 20 additions & 0 deletions src/Relations/BelongsToJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ class BelongsToJson extends BelongsTo implements ConcatenableRelation
use IsConcatenableBelongsToJsonRelation;
use IsJsonRelation;

/**
* Create a new belongs to JSON relationship instance.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $child
* @param string $foreignKey
* @param string $ownerKey
* @param string $relationName
* @return void
*/
public function __construct(Builder $query, Model $child, $foreignKey, $ownerKey, $relationName)
{
$segments = explode('[]->', $foreignKey);

$this->path = $segments[0];
$this->key = $segments[1] ?? null;

parent::__construct($query, $child, $foreignKey, $ownerKey, $relationName);
}

/**
* Get the results of the relationship.
*
Expand Down
19 changes: 19 additions & 0 deletions src/Relations/HasManyJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ class HasManyJson extends HasMany implements ConcatenableRelation
use IsConcatenableHasManyJsonRelation;
use IsJsonRelation;

/**
* Create a new has many JSON relationship instance.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $parent
* @param string $foreignKey
* @param string $localKey
* @return void
*/
public function __construct(Builder $query, Model $parent, $foreignKey, $localKey)
{
$segments = explode('[]->', $foreignKey);

$this->path = $segments[0];
$this->key = $segments[1] ?? null;

parent::__construct($query, $parent, $foreignKey, $localKey);
}

/**
* Get the results of the relationship.
*
Expand Down
17 changes: 0 additions & 17 deletions src/Relations/Traits/IsJsonRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,6 @@ trait IsJsonRelation
*/
protected $key;

/**
* Create a new JSON relationship instance.
*
* @return void
*/
public function __construct()
{
$args = func_get_args();

$foreignKey = explode('[]->', $args[2]);

$this->path = $foreignKey[0];
$this->key = $foreignKey[1] ?? null;

parent::__construct(...$args);
}

/**
* Hydrate the pivot relationship on the models.
*
Expand Down
24 changes: 12 additions & 12 deletions tests/Concatenation/HasManyThroughJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function testLazyLoading()
{
$projects = Role::find(2)->projects;

$this->assertEquals([71, 73], $projects->pluck('id')->all());
$this->assertEquals([91, 93], $projects->pluck('id')->all());
}

public function testLazyLoadingWithObjects()
Expand All @@ -26,7 +26,7 @@ public function testLazyLoadingWithObjects()

$projects = Role::find(2)->projects2;

$this->assertEquals([71, 73], $projects->pluck('id')->all());
$this->assertEquals([91, 93], $projects->pluck('id')->all());
$pivot = $projects[0]->pivot;
$this->assertInstanceOf(Pivot::class, $pivot);
$this->assertTrue($pivot->exists);
Expand All @@ -35,7 +35,7 @@ public function testLazyLoadingWithObjects()

public function testLazyLoadingWithReverseRelationship()
{
$roles = Project::find(71)->roles;
$roles = Project::find(91)->roles;

$this->assertEquals([1, 2], $roles->pluck('id')->all());
}
Expand All @@ -46,7 +46,7 @@ public function testLazyLoadingWithReverseRelationshipAndObjects()
$this->markTestSkipped();
}

$roles = Project::find(71)->roles2;
$roles = Project::find(91)->roles2;

$this->assertEquals([1, 2], $roles->pluck('id')->all());
$pivot = $roles[0]->pivot;
Expand Down Expand Up @@ -76,9 +76,9 @@ public function testEagerLoading()
{
$roles = Role::with('projects')->get();

$this->assertEquals([71], $roles[0]->projects->pluck('id')->all());
$this->assertEquals([71, 73], $roles[1]->projects->pluck('id')->all());
$this->assertEquals([73], $roles[2]->projects->pluck('id')->all());
$this->assertEquals([91], $roles[0]->projects->pluck('id')->all());
$this->assertEquals([91, 93], $roles[1]->projects->pluck('id')->all());
$this->assertEquals([93], $roles[2]->projects->pluck('id')->all());
$this->assertEquals([], $roles[3]->projects->pluck('id')->all());
}

Expand All @@ -90,9 +90,9 @@ public function testEagerLoadingWithObjects()

$roles = Role::with('projects2')->get();

$this->assertEquals([71], $roles[0]->projects2->pluck('id')->all());
$this->assertEquals([71, 73], $roles[1]->projects2->pluck('id')->all());
$this->assertEquals([73], $roles[2]->projects2->pluck('id')->all());
$this->assertEquals([91], $roles[0]->projects2->pluck('id')->all());
$this->assertEquals([91, 93], $roles[1]->projects2->pluck('id')->all());
$this->assertEquals([93], $roles[2]->projects2->pluck('id')->all());
$this->assertEquals([], $roles[3]->projects2->pluck('id')->all());
$pivot = $roles[1]->projects2[0]->pivot;
$this->assertInstanceOf(Pivot::class, $pivot);
Expand Down Expand Up @@ -148,7 +148,7 @@ public function testExistenceQueryWithReverseRelationship()
{
$projects = Project::has('roles')->get();

$this->assertEquals([71, 73], $projects->pluck('id')->all());
$this->assertEquals([91, 93], $projects->pluck('id')->all());
}

public function testExistenceQueryWithReverseRelationshipAndObjects()
Expand All @@ -159,6 +159,6 @@ public function testExistenceQueryWithReverseRelationshipAndObjects()

$projects = Project::has('roles2')->get();

$this->assertEquals([71, 73], $projects->pluck('id')->all());
$this->assertEquals([91, 93], $projects->pluck('id')->all());
}
}
2 changes: 0 additions & 2 deletions tests/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
use Staudenmeir\EloquentJsonRelations\HasJsonRelationships;

class Permission extends Model
{
use HasJsonRelationships;
use HasRelationships;

public $timestamps = false;
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,15 @@ protected function seed(): void
Permission::create(['id' => 85, 'role_id' => 4]);

Project::create([
'id' => 71,
'id' => 91,
'user_id' => 21,
]);
Project::create([
'id' => 72,
'id' => 92,
'user_id' => 22,
]);
Project::create([
'id' => 73,
'id' => 93,
'user_id' => 23,
]);

Expand Down

0 comments on commit 31c19b9

Please sign in to comment.