mirror of
https://codeberg.org/streams/streams.git
synced 2024-09-19 16:35:19 +00:00
ActorId service
This commit is contained in:
parent
a03e422547
commit
ae0c1b1418
2 changed files with 65 additions and 0 deletions
46
src/Lib/ActorId.php
Normal file
46
src/Lib/ActorId.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
19
tests/unit/Lib/ActorIdTest.php
Normal file
19
tests/unit/Lib/ActorIdTest.php
Normal 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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in a new issue