From ae0c1b141888b6551a575358a98326664278f25f Mon Sep 17 00:00:00 2001 From: Mike Macgirvin Date: Thu, 21 Mar 2024 13:12:55 +1100 Subject: [PATCH] ActorId service --- src/Lib/ActorId.php | 46 ++++++++++++++++++++++++++++++++++ tests/unit/Lib/ActorIdTest.php | 19 ++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/Lib/ActorId.php create mode 100644 tests/unit/Lib/ActorIdTest.php diff --git a/src/Lib/ActorId.php b/src/Lib/ActorId.php new file mode 100644 index 000000000..91f916081 --- /dev/null +++ b/src/Lib/ActorId.php @@ -0,0 +1,46 @@ +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; + } + +} diff --git a/tests/unit/Lib/ActorIdTest.php b/tests/unit/Lib/ActorIdTest.php new file mode 100644 index 000000000..1806a0fe3 --- /dev/null +++ b/tests/unit/Lib/ActorIdTest.php @@ -0,0 +1,19 @@ +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(); + } + + + +}