diff --git a/README.md b/README.md index 0facfb2..d7b5444 100644 --- a/README.md +++ b/README.md @@ -17,3 +17,23 @@ if (Key::verify($public_key, $signature, $hash)) { echo 'Signature belongs to hash'; } ``` + + +## Message usage + +``` +use nostriphant\NIP01\Message; + +$message = Message::count(1); +print $message; // prints "['COUNT',1]" + +``` + +``` +use nostriphant\NIP01\Message; +use nostriphant\NIP01\Event; + +$message = Message::event(new Event()); +print $message; // prints "['EVENT',{...}]" + +``` diff --git a/src/Message.php b/src/Message.php index cef467e..f1e4d49 100644 --- a/src/Message.php +++ b/src/Message.php @@ -23,4 +23,8 @@ public function __toString(): string { static function decode(string $json): self { return new self(...Nostr::decode($json)); } + + static function __callStatic(string $name, array $arguments): self { + return new self(strtoupper($name), ...$arguments); + } } diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 7e00625..0385cd5 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -1,6 +1,7 @@ 'c']); @@ -16,3 +17,24 @@ $message = Message::decode('["TYPE", "a", {"b":"c"}]'); expect($message())->toBe(['TYPE', 'a', ['b' => 'c']]); }); + + +it('can fabric a message', function () { + $message = Message::event('a', ['b' => 'c']); + expect($message())->toBe(['EVENT', 'a', ['b' => 'c']]); +}); + +it('can fabric a message with an Event', function () { + $message = Message::event($event = new Event( + id: '', + pubkey: '', + created_at: 1734349976, + kind: 1, + content: 'Hello World', + sig: '', + tags: [] + )); + + expect($message())->toBe(["EVENT", $event]); + expect('' . $message)->toBe('["EVENT",{"id":"","pubkey":"","created_at":1734349976,"kind":1,"content":"Hello World","sig":"","tags":[]}]'); +});