init
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\Edge;
|
||||
|
||||
use Lucent\LucentException;
|
||||
use Lucent\Validator\Validator as LucentValidator;
|
||||
|
||||
final class Edge
|
||||
{
|
||||
/**
|
||||
* @throws LucentException
|
||||
*/
|
||||
public function __construct(
|
||||
|
||||
public string $source,
|
||||
public string $target,
|
||||
public string $sourceSchema,
|
||||
public string $targetSchema,
|
||||
public string $field,
|
||||
public string $rank = "a",
|
||||
public int $depth = 0,
|
||||
)
|
||||
{
|
||||
|
||||
|
||||
LucentValidator::single("source", $source, "required|uuid");
|
||||
LucentValidator::single("target", $target, "required|uuid");
|
||||
}
|
||||
|
||||
public function equal(Edge $edge): bool
|
||||
{
|
||||
return $this->targetSchema === $edge->targetSchema && $this->field === $edge->field && $this->target === $edge->target && $this->source === $edge->source;
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return json_decode(json_encode($this), true);
|
||||
}
|
||||
|
||||
public function toDB(): array
|
||||
{
|
||||
$data = $this->toArray();
|
||||
unset($data["depth"]);
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function fromArray(array $data): Edge
|
||||
{
|
||||
|
||||
return new Edge(
|
||||
|
||||
source: data_get($data, 'source'),
|
||||
target: data_get($data, 'target'),
|
||||
sourceSchema: data_get($data, 'sourceSchema'),
|
||||
targetSchema: data_get($data, 'targetSchema'),
|
||||
field: data_get($data, 'field'),
|
||||
rank: data_get($data, 'rank'),
|
||||
depth: data_get($data, 'depth', 0),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php namespace Lucent\Edge;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Lucent\LucentException;
|
||||
use PDOException;
|
||||
|
||||
class EdgeRepo
|
||||
{
|
||||
|
||||
|
||||
public static function insert(Edge $edge): void
|
||||
{
|
||||
try {
|
||||
DB::table("edges")->insert($edge->toDB());
|
||||
} catch (PDOException $e) {
|
||||
if ($e->getCode() == 23505) {
|
||||
throw new LucentException("Edge already exists");
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static function update(string $from, EdgeCollection $edges): void
|
||||
{
|
||||
$edgesDB = collect($edges)->map(fn($e) => $e->toDB())->toArray();
|
||||
DB::table("edges")->where("source", $from)->delete();
|
||||
DB::table("edges")->insert($edgesDB);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php namespace Lucent\Edge;
|
||||
|
||||
use Lucent\LucentException;
|
||||
|
||||
class EdgeService
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @throws LucentException
|
||||
*/
|
||||
public static function create(
|
||||
string $source,
|
||||
string $target,
|
||||
string $sourceSchema,
|
||||
string $targetSchema,
|
||||
string $field,
|
||||
string $rank,
|
||||
): Edge
|
||||
{
|
||||
|
||||
|
||||
$edge = new Edge(
|
||||
|
||||
source: $source,
|
||||
target: $target,
|
||||
sourceSchema: $sourceSchema,
|
||||
targetSchema: $targetSchema,
|
||||
field: $field,
|
||||
rank: $rank,
|
||||
);
|
||||
EdgeRepo::insert($edge);
|
||||
return $edge;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\Edge;
|
||||
|
||||
|
||||
final class QueryEdge
|
||||
{
|
||||
public function __construct(
|
||||
public string $fromSchema,
|
||||
public string $from,
|
||||
public string $schema,
|
||||
public string $field,
|
||||
public string $to,
|
||||
) {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user