Skip to content

Commit

Permalink
Merge pull request #6 from AuroraWebSoftware/added-ignore-scopes-in-c…
Browse files Browse the repository at this point in the history
…onnectives-func

Added ignore scopes in connectives func
  • Loading branch information
emreakay authored Feb 28, 2024
2 parents 0764c06 + abfe7fb commit 74b9969
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/Traits/Connective.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function connections(string|array|null $connectionTypes = null, string|ar
/**
* @return ConnectiveCollection<ConnectiveContract>|null
*/
public function connectives(string|array|null $connectionTypes = null, string|array|null $modelTypes = null): ?ConnectiveCollection
public function connectives(string|array|null $connectionTypes = null, string|array|null $modelTypes = null, ?array $ignoreScopes = []): ?ConnectiveCollection
{
$connections = $this->connections($connectionTypes, $modelTypes);
$collection = ConnectiveCollection::make();
Expand All @@ -86,8 +86,16 @@ public function connectives(string|array|null $connectionTypes = null, string|ar
$toModelType = $connection->to_model_type;
$toModelId = $connection->to_model_id;

$toModelInstance = $toModelType::find($toModelId);
$collection->push($toModelInstance);
if ($ignoreScopes && is_array($ignoreScopes)) {
$toModelInstance = $toModelType::withoutGlobalScopes($ignoreScopes)->find($toModelId);
} else {
$toModelInstance = $toModelType::find($toModelId);
}

if ($toModelInstance != null) {
$collection->push($toModelInstance);
}

}

return $collection;
Expand Down Expand Up @@ -122,7 +130,7 @@ public function inverseConnections(string|array|null $connectionTypes = null, st
/**
* @return ConnectiveCollection<ConnectiveContract>|null
*/
public function inverseConnectives(string|array|null $connectionTypes = null, string|array|null $modelTypes = null): ?ConnectiveCollection
public function inverseConnectives(string|array|null $connectionTypes = null, string|array|null $modelTypes = null, ?array $ignoreScopes = []): ?ConnectiveCollection
{
$incomingConnections = $this->inverseConnections($connectionTypes, $modelTypes);
$collection = ConnectiveCollection::make();
Expand All @@ -131,8 +139,16 @@ public function inverseConnectives(string|array|null $connectionTypes = null, st
$fromModelType = $incomingConnection->from_model_type;
$fromModelId = $incomingConnection->from_model_id;

$fromModelInstance = $fromModelType::find($fromModelId);
$collection->push($fromModelInstance);
if ($ignoreScopes && is_array($ignoreScopes)) {
$fromModelInstance = $fromModelType::withoutGlobalScopes($ignoreScopes)->find($fromModelId);
} else {
$fromModelInstance = $fromModelType::find($fromModelId);
}

if ($fromModelInstance != null) {
$collection->push($fromModelInstance);
}

}

return $collection;
Expand Down
34 changes: 34 additions & 0 deletions tests/PackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,40 @@

});

it('can get connectives some scopes can be excluded in a model that', function () {

/**
* @var ConnectiveContract & \AuroraWebSoftware\Connective\Tests\Models\Connective $connective2
*/
$connective2 = \AuroraWebSoftware\Connective\Tests\Models\Connective::create([
'name' => 'name72',
]);

/**
* @var ConnectiveContract & \AuroraWebSoftware\Connective\Tests\Models\Connective $connective3
*/
$connective3 = \AuroraWebSoftware\Connective\Tests\Models\Connective::create([
'name' => 'name73',
]);

/**
* @var ConnectiveContract & \AuroraWebSoftware\Connective\Tests\Models\Connective $connective4
*/
$connective4 = \AuroraWebSoftware\Connective\Tests\Models\Connective::create([
'name' => 'name74',
]);

\AuroraWebSoftware\Connective\Tests\Models\Connective::addGlobalScope('name', function (\Illuminate\Database\Eloquent\Builder $builder) {
$builder->where('name', 'name73');
});

$connective2->connectTo($connective3, 'a');
$connective2->connectTo($connective4, 'a');

expect($connective2->connectives('a'))->toHaveCount(1);
expect($connective2->connectives('a', null, ['name']))->toHaveCount(2);
});

it('can get inverse connections of a connective model', function () {

/**
Expand Down

0 comments on commit 74b9969

Please sign in to comment.