diff --git a/src/Types/Tags.php b/src/Types/Tags.php index 930cf7b..a2b4f77 100644 --- a/src/Types/Tags.php +++ b/src/Types/Tags.php @@ -82,6 +82,16 @@ public function merge(self|Tag $other): self return self::fromArray(array_merge($this->tags, $other->tags)); } + public function firstOfKey(string $key): ?Tag + { + foreach ($this->tags as $tag) { + if ($tag->key === $key) { + return $tag; + } + } + return null; + } + public function contain(Tag $tag): bool { return array_key_exists($tag->toString(), $this->tags); diff --git a/tests/Unit/Types/TagsTest.php b/tests/Unit/Types/TagsTest.php index 78e6ebb..df79d30 100644 --- a/tests/Unit/Types/TagsTest.php +++ b/tests/Unit/Types/TagsTest.php @@ -208,6 +208,20 @@ public function test_merge(array $ids1, array $ids2, array $expectedResult): voi self::assertTagsMatch($expectedResult, Tags::fromArray($ids1)->merge(Tags::fromArray($ids2))); } + public function test_firstOfKey_returns_null_if_no_matching_tag_is_found(): void + { + $tags = Tags::fromArray(['foo:bar', 'bar:baz']); + self::assertNull($tags->firstOfKey('baz')); + } + + public function test_firstOfKey_returns_first_matching_tag_if_exists(): void + { + $tags = Tags::fromArray(['foo:bar', 'bar:baz', 'bar:aaa']); + $tag = $tags->firstOfKey('bar'); + self::assertInstanceOf(Tag::class, $tag); + self::assertSame('bar:aaa', $tag->toString()); + } + public function test_contains_allows_checking_single_tags(): void { $ids = Tags::fromArray(['foo:bar', 'baz:foos']);