Skip to content

Commit

Permalink
Merge pull request #21 from telkins/dev/custom-edge-model
Browse files Browse the repository at this point in the history
Dev/custom edge model
  • Loading branch information
telkins authored Sep 30, 2022
2 parents 1a5126d + 3771f6b commit f79b584
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,31 @@ $ancestors = MyModel::dagAncestorsOf($myModel->id, 'my-source', 1)->get();

Not providing the `$maxHops` parameter means that all descendants, ancestors, or relations will be returned.

### Custom DAG edge model

You can use your own model class if you need to customise the behavior of the DAG edge model.

Your custom model class must extend the `Telkins\Models\DagEdge` class:

```php
namespace App\Models;

use Telkins\Models\DagEdge as BaseModel;

class MyDagEdge extends BaseModel
{
...
```

You can then specify the fully qualified class name of your custom model in the package config file.

```php
// config/laravel-dag-manager.php
...
'edge_model' => \App\Models\MyDagEdge::class,
...
```

## Testing

```bash
Expand Down
10 changes: 10 additions & 0 deletions config/laravel-dag-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@

'table_name' => 'dag_edges',

/**
*-------------------------------------------------------------------------
* Edge Model
*-------------------------------------------------------------------------
*
* The fully qualified class name of the DAG edge model.
*/

'edge_model' => \Telkins\Dag\Models\DagEdge::class,

];
7 changes: 6 additions & 1 deletion src/Concerns/UsesDagConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public function defaultTableName(): ?string
{
return config('laravel-dag-manager.table_name');
}
}

public function dagEdgeModel(): ?string
{
return config('laravel-dag-manager.edge_model');
}
}
19 changes: 15 additions & 4 deletions src/Tasks/AddDagEdge.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Telkins\Dag\Concerns\UsesDagConfig;
use Telkins\Dag\Exceptions\CircularReferenceException;
use Telkins\Dag\Exceptions\TooManyHopsException;
use Telkins\Dag\Models\DagEdge;

class AddDagEdge
{
use UsesDagConfig;

protected ?string $connection;
protected int $endVertex;
protected int $maxHops;
Expand Down Expand Up @@ -51,7 +54,9 @@ public function execute(): ?Collection

protected function edgeExists(): bool
{
return DagEdge::where([
$edgeClass = $this->dagEdgeModel();

return $edgeClass::where([
['start_vertex', $this->startVertex],
['end_vertex', $this->endVertex],
['hops', 0],
Expand All @@ -68,7 +73,9 @@ protected function guardAgainstCircularRelation(): void
throw new CircularReferenceException();
}

if (DagEdge::where([
$edgeClass = $this->dagEdgeModel();

if ($edgeClass::where([
['start_vertex', $this->endVertex],
['end_vertex', $this->startVertex],
['source', $this->source],
Expand All @@ -92,7 +99,9 @@ protected function createEdge(): DagEdge

protected function createDirectEdge(): DagEdge
{
$edge = DagEdge::create([
$edgeClass = $this->dagEdgeModel();

$edge = $edgeClass::create([
'start_vertex' => $this->startVertex,
'end_vertex' => $this->endVertex,
'hops' => 0,
Expand Down Expand Up @@ -187,7 +196,9 @@ protected function executeInsert(Builder $select): void

protected function getNewlyInsertedEdges(DagEdge $edge): Collection
{
return DagEdge::where('direct_edge_id', $edge->id)
$edgeClass = $this->dagEdgeModel();

return $edgeClass::where('direct_edge_id', $edge->id)
->orderBy('hops')
->get();
}
Expand Down
7 changes: 6 additions & 1 deletion src/Tasks/RemoveDagEdge.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Telkins\Dag\Concerns\UsesDagConfig;
use Telkins\Dag\Models\DagEdge;

class RemoveDagEdge
{
use UsesDagConfig;

protected ?string $connection;
protected int $endVertex;
protected string $source;
Expand All @@ -30,7 +33,9 @@ public function __construct(int $startVertex, int $endVertex, string $source, st
*/
public function execute(): bool
{
$edge = DagEdge::where([
$edgeClass = $this->dagEdgeModel();

$edge = $edgeClass::where([
['start_vertex', $this->startVertex],
['end_vertex', $this->endVertex],
['hops', 0],
Expand Down

0 comments on commit f79b584

Please sign in to comment.