records and edgs
This commit is contained in:
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\Edge;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Support\Collection<int|string, Edge>
|
||||
*/
|
||||
final class EdgeCollection extends Collection
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
Edge ...$array
|
||||
) {
|
||||
parent::__construct($array);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Edge[]
|
||||
**/
|
||||
public function toArray(): array
|
||||
{
|
||||
return collect($this)->values()->toArray();
|
||||
}
|
||||
|
||||
public static function fromArray(array $data): EdgeCollection
|
||||
{
|
||||
$edges = array_map([Edge::class, 'fromArray'], $data);
|
||||
return new EdgeCollection(...$edges);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+47
-3
@@ -25,7 +25,31 @@ class EdgeRepo
|
||||
|
||||
}
|
||||
|
||||
public function update(string $from, EdgeCollection $edges): void
|
||||
/**
|
||||
* @param list<Edge> $edges
|
||||
* @return void
|
||||
* @throws LucentException
|
||||
*/
|
||||
public function insertMany(array $edges): void
|
||||
{
|
||||
$edgesDB = collect($edges)->map(fn($e) => $e->toDB())->toArray();
|
||||
try {
|
||||
DB::table("edges")->insert($edgesDB);
|
||||
} catch (PDOException $e) {
|
||||
if ($e->getCode() == 23505) {
|
||||
throw new LucentException("Edge already exists");
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $from
|
||||
* @param list<Edge> $edges
|
||||
* @return void
|
||||
*/
|
||||
public function replaceForRecord(string $from, array $edges): void
|
||||
{
|
||||
$edgesDB = collect($edges)->map(fn($e) => $e->toDB())->toArray();
|
||||
DB::table("edges")->where("source", $from)->delete();
|
||||
@@ -33,10 +57,19 @@ class EdgeRepo
|
||||
}
|
||||
|
||||
|
||||
public function findAll(): EdgeCollection
|
||||
/**
|
||||
* @return list<Edge>
|
||||
*/
|
||||
public function findAll(): array
|
||||
{
|
||||
$edges = DB::table("edges")->get();
|
||||
return new EdgeCollection(...$edges->map([$this, 'mapEdge'])->toArray());
|
||||
return $edges->map([$this, 'mapEdge'])->toArray();
|
||||
}
|
||||
|
||||
public function findForSource(string $recordId): array
|
||||
{
|
||||
$edges = DB::table("edges")->where("source", $recordId)->get();
|
||||
return $edges->map([$this, 'mapEdge'])->toArray();
|
||||
}
|
||||
|
||||
public function mapEdge(stdClass $edge): Edge
|
||||
@@ -65,4 +98,15 @@ class EdgeRepo
|
||||
->delete();
|
||||
}
|
||||
|
||||
public function findLastEdgeRank(string $source, string $field): string
|
||||
{
|
||||
$data = DB::table("edges")
|
||||
->where("source", $source)
|
||||
->where("field", $field)
|
||||
->orderBy("rank", "desc")
|
||||
->first();
|
||||
|
||||
return $data->rank ?? "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
<?php namespace Lucent\Edge;
|
||||
|
||||
use Lucent\Lexorank\Lexorank;
|
||||
use Lucent\LucentException;
|
||||
use Lucent\Record\InputData\EdgeInputData;
|
||||
|
||||
class EdgeService
|
||||
{
|
||||
@@ -8,9 +10,7 @@ class EdgeService
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws LucentException
|
||||
*/
|
||||
|
||||
public function create(
|
||||
string $source,
|
||||
string $target,
|
||||
@@ -35,12 +35,73 @@ class EdgeService
|
||||
return $edge;
|
||||
}
|
||||
|
||||
public function update(string $from, EdgeCollection $edges): void
|
||||
/**
|
||||
* @param string $source
|
||||
* @param string $sourceSchema
|
||||
* @param list<EdgeInputData> $edges
|
||||
* @return list<Edge>
|
||||
* @throws LucentException
|
||||
*/
|
||||
public function createManyForRecord(
|
||||
string $source,
|
||||
string $sourceSchema,
|
||||
array $edges,
|
||||
): array
|
||||
{
|
||||
$this->edgeRepo->update($from, $edges);
|
||||
$lastRank = Lexorank::fromString("0");
|
||||
$edgeCollection = [];
|
||||
foreach ($edges as $edge) {
|
||||
$lastRank = Lexorank::after($lastRank);
|
||||
$edgeCollection[] = new Edge(
|
||||
source: $source,
|
||||
target: $edge->target,
|
||||
sourceSchema: $sourceSchema,
|
||||
targetSchema: $edge->targetSchema,
|
||||
field: $edge->field,
|
||||
rank: $lastRank->get(),
|
||||
);
|
||||
}
|
||||
|
||||
$this->edgeRepo->insertMany($edgeCollection);
|
||||
return $edgeCollection;
|
||||
}
|
||||
|
||||
public function findAll(): EdgeCollection
|
||||
/**
|
||||
* @param string $source
|
||||
* @param string $sourceSchema
|
||||
* @param list<EdgeInputData> $edges
|
||||
* @return list<Edge>
|
||||
*/
|
||||
public function replaceManyForRecord(
|
||||
string $source,
|
||||
string $sourceSchema,
|
||||
array $edges,
|
||||
): array
|
||||
{
|
||||
$lastRank = Lexorank::fromString("0");
|
||||
$edgeCollection = [];
|
||||
foreach ($edges as $edge) {
|
||||
$lastRank = Lexorank::after($lastRank);
|
||||
$edgeCollection[] = new Edge(
|
||||
source: $source,
|
||||
target: $edge->target,
|
||||
sourceSchema: $sourceSchema,
|
||||
targetSchema: $edge->targetSchema,
|
||||
field: $edge->field,
|
||||
rank: $lastRank->get(),
|
||||
);
|
||||
}
|
||||
|
||||
$this->edgeRepo->replaceForRecord($source, $edgeCollection);
|
||||
return $edgeCollection;
|
||||
}
|
||||
|
||||
public function findForSource(string $recordId): array
|
||||
{
|
||||
return $this->edgeRepo->findForSource($recordId);
|
||||
}
|
||||
|
||||
public function findAll(): array
|
||||
{
|
||||
return $this->edgeRepo->findAll();
|
||||
}
|
||||
@@ -50,4 +111,9 @@ class EdgeService
|
||||
$this->edgeRepo->remove($edge);
|
||||
}
|
||||
|
||||
private function findLastEdgeRank(string $source, string $field): string
|
||||
{
|
||||
return $this->edgeRepo->findLastEdgeRank($source, $field);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php namespace Lucent\Edge\Event;
|
||||
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
|
||||
class EdgesUpdated
|
||||
{
|
||||
use Dispatchable;
|
||||
|
||||
public function __construct(
|
||||
public string $recordId,
|
||||
public string $field,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user