Skip to content
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
28 changes: 28 additions & 0 deletions includes/Services/MentionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Escalated\Services;

class MentionService
{
private const MENTION_REGEX = '/@(\w+(?:\.\w+)*)/';

public static function extractMentions(?string $text): array
{
if (empty($text)) {
return [];
}
preg_match_all(self::MENTION_REGEX, $text, $matches);

return array_values(array_unique($matches[1] ?? []));
}

public static function extractUsernameFromEmail(string $email): string
{
if (empty($email)) {
return '';
}
$parts = explode('@', $email);

return $parts[0] ?? $email;
}
}
49 changes: 49 additions & 0 deletions tests/Test_Mention_Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

use Escalated\Services\MentionService;
use PHPUnit\Framework\TestCase;

class Test_Mention_Service extends TestCase
{
public function test_single_mention()
{
$this->assertEquals(['john'], MentionService::extractMentions('Hello @john review'));
}

public function test_multiple_mentions()
{
$result = MentionService::extractMentions('@alice and @bob');
$this->assertContains('alice', $result);
$this->assertContains('bob', $result);
}

public function test_dotted_username()
{
$this->assertEquals(['john.doe'], MentionService::extractMentions('cc @john.doe'));
}

public function test_deduplicates()
{
$this->assertCount(1, MentionService::extractMentions('@alice @alice'));
}

public function test_empty()
{
$this->assertEmpty(MentionService::extractMentions(''));
}

public function test_null()
{
$this->assertEmpty(MentionService::extractMentions(null));
}

public function test_no_mentions()
{
$this->assertEmpty(MentionService::extractMentions('No mentions'));
}

public function test_username_from_email()
{
$this->assertEquals('john', MentionService::extractUsernameFromEmail('john@example.com'));
}
}
Loading