Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save chadyred/0c9974661ba0ba8eb21bb1ed19d8c72a to your computer and use it in GitHub Desktop.

Select an option

Save chadyred/0c9974661ba0ba8eb21bb1ed19d8c72a to your computer and use it in GitHub Desktop.

Revisions

  1. chadyred created this gist Sep 12, 2024.
    73 changes: 73 additions & 0 deletions none-linear-tree-node-printer-edges-verticle.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    <?php

    class Edge
    {
    public function __construct(public readonly Verticle $linkedFrom, public readonly Verticle $linkedTo)
    {
    }
    }

    class Verticle
    {
    public function __construct(public readonly int $version)
    {
    }

    public function equals(Verticle $that)
    {
    return $this === $that;
    }
    }

    class NodeMapper
    {
    /**
    * @param array<Edge> $edges
    */
    public function __construct(public readonly array $edges)
    {
    }

    public function printEdgeRelationMapping()
    {
    $previousTo = null;

    foreach ($this->edges as $key => $edge) {
    $from = $edge->linkedFrom;
    $to = $edge->linkedTo;
    if ($previousTo?->equals($from)) {
    echo $to->version; // From shoul not be dipslay because it is the same as before
    } else {
    echo $from->version . ' -> ' . $to->version;
    }

    if (array_key_last($this->edges) !== $key) {
    echo ' -> ';
    }

    $previousTo = $to;
    }
    }
    }


    $v1 = new Verticle(1);
    $v2 = new Verticle(2);
    $v3 = new Verticle(3);
    $v4 = new Verticle(4);
    $v5 = new Verticle(5);

    # v1 -> v2 -> v4 -> V3 -> v5 -> v1

    $e1 = new Edge($v1, $v2);
    $e2 = new Edge($v2, $v4);
    $e3 = new Edge($v4, $v3);
    $e4 = new Edge($v5, $v1);

    $nodeMapper = new NodeMapper([$e1,
    $e2,
    $e3,
    $e4
    ]);

    $nodeMapper->printEdgeRelationMapping();