ActorId service

This commit is contained in:
Mike Macgirvin 2024-03-21 13:12:55 +11:00
parent a03e422547
commit ae0c1b1418
2 changed files with 65 additions and 0 deletions

46
src/Lib/ActorId.php Normal file
View file

@ -0,0 +1,46 @@
<?php
namespace Code\Lib;
class ActorId
{
protected $id;
protected $type;
public const ACTORID_TYPE_UNKNOWN = 0;
public const ACTORID_TYPE_URL = 1;
public const ACTORID_TYPE_DID = 2;
public function __construct($id)
{
$this->id = $id;
if (str_contains($this->id, 'did:ap:key:')) {
$this->type = ActorId::ACTORID_TYPE_DID;
if (str_starts_with($this->id, 'http')) {
$this->id = substr($this->id, strpos($this->id, 'did:ap:key:'));
$this->id = substr($this->id, 0, strpos($this->id, '?') ? strpos($this->id, '?') : null);
$this->id = substr($this->id, 0, strpos($this->id, '#') ? strpos($this->id, '#') : null);
}
}
elseif (str_starts_with($this->id, 'http')) {
$this->type = ActorId::ACTORID_TYPE_URL;
}
else {
$this->type = ActorId::ACTORID_TYPE_UNKNOWN;
}
return $this;
}
public function getId()
{
return $this->id;
}
public function getType()
{
return $this->type;
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace Code\Tests\Unit\Lib;
use Code\Lib\ActorId;
class ActorIdTest extends UnitTestCase
{
public function testActorId()
{
$this->assertEquals('did:ap:key:abc12345', (new \Code\Lib\ActorId('https://resolver.com/resolver/did:ap:key:abc12345?foo'))->getId());
$this->assertEquals('did:ap:key:abc12345', (new \Code\Lib\ActorId('https://resolver.com/resolver/did:ap:key:abc12345#foo'))->getId());
$this->assertEquals('https://example.com/actor', (new \Code\Lib\ActorId('https://example.com/actor'))->getId();
$this->assertEquals('did:ap:key:abc12345', (new \Code\Lib\ActorId('did:ap:key:abc12345'))->getId();
}
}