Skip to content

Commit

Permalink
Merge pull request #12 from phonetworks/id-headers-framework
Browse files Browse the repository at this point in the history
Id headers framework
  • Loading branch information
esokullu authored Sep 8, 2018
2 parents e824c3a + 9ca90be commit e40c85e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 70 deletions.
64 changes: 4 additions & 60 deletions src/Pho/Lib/Graph/ID.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,70 +77,14 @@ private function __construct(string $id)
public static function generate(EntityInterface $entity): ID
{
return new ID(
sprintf("%x%x%s",
self::header($entity),
rand(0,15),
sprintf("%s",
bin2hex(
random_bytes(15)
random_bytes(16)
)
)
);
}

/**
* Fetches entity header.
*
* Entity headers will be as follows:
*
* * 0: Graph
* * 1: Unidentified Node
* * 2: SubGraph Node
* * 3: Framework\Graph Node
* * 4: Actor Node
* * 5: Object Node
* * 6: Unidentified Edge
* * 7: Read Edge
* * 8: Write Edge
* * 9: Subscribe Edge
* * 10: Mention Edge
* * 11: Unidentified
*
* This method may be overriden by packages at higher levels.
* The purpose of headers is to enable easy/fast classification
* of entitities by looking up the first byte of the UUID.
*
* @param EntityInterface $entity
*
* @return int 1-11 depending on entity type
*/
protected static function header(EntityInterface $entity): int
{
if($entity instanceof Node) {
if($entity instanceof \Pho\Framework\Object)
return 5;
elseif($entity instanceof \Pho\Framework\Actor)
return 4;
elseif($entity instanceof \Pho\Framework\Graph)
return 3;
elseif($entity instanceof SubGraph)
return 2;
return 1;
}
elseif($entity instanceof Edge) {
// order is important
if($entity instanceof \Pho\Framework\ObjectOut\Mention)
return 10;
elseif($entity instanceof \Pho\Framework\ActorOut\Write)
return 8;
elseif($entity instanceof \Pho\Framework\ActorOut\Subscribe)
return 9;
elseif($entity instanceof \Pho\Framework\ActorOut\Read)
return 7;
return 6;
}
return 11;
}


/**
* Loads a Pho ID with the given string
*
Expand Down Expand Up @@ -216,4 +160,4 @@ public function toString(): string
return $this->value;
}

}
}
10 changes: 0 additions & 10 deletions tests/Pho/Lib/Graph/SimpleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,6 @@ public function testInvalidID() {
ID::fromString("invalid");
}

public function testIDTypes() {
$subgraph = new SubGraph($this->graph);
$node = new Node($subgraph);
$edge = new Edge($node, $subgraph);
$this->assertEquals(0, (int) $this->graph->id()->toString()[0]);
$this->assertEquals(1, (int) $node->id()->toString()[0]);
$this->assertEquals(2, (int) $subgraph->id()->toString()[0]);
$this->assertGreaterThanOrEqual(6, (int) $edge->id()->toString()[0]);
}

public function testGraphToArray() {
$node = new Node($this->graph);
$this->assertEquals($node->id(), $this->graph->toArray()["members"][0]);
Expand Down

0 comments on commit e40c85e

Please sign in to comment.