Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Add Tags::firstOfKey() convenience method #10

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Types/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions tests/Unit/Types/TagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down