| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services\Notifications; |
| 4 |
|
| 5 |
class MentionParser |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Extract agent IDs from message content. |
| 9 |
* |
| 10 |
* Handles two mention formats: |
| 11 |
* - Rich editor: <span class="fs_agent_mention" data-mention-username="7"> |
| 12 |
* - Plain-text editor: @[7:Display Name] |
| 13 |
* |
| 14 |
* @param string $content |
| 15 |
* @return array Integer agent IDs. |
| 16 |
*/ |
| 17 |
public function extractMentionIds($content) |
| 18 |
{ |
| 19 |
$content = (string) $content; |
| 20 |
|
| 21 |
if (!$content) { |
| 22 |
return []; |
| 23 |
} |
| 24 |
|
| 25 |
$ids = []; |
| 26 |
|
| 27 |
// Rich editor — span with numeric data-mention-username. |
| 28 |
if (stripos($content, 'data-mention-username') !== false) { |
| 29 |
preg_match_all( |
| 30 |
'/<span\b[^>]*\bclass=["\'][^"\']*\bfs_agent_mention\b[^"\']*["\'][^>]*\bdata-mention-username=["\'](\d+)["\'][^>]*>/i', |
| 31 |
$content, |
| 32 |
$spanMatches |
| 33 |
); |
| 34 |
|
| 35 |
foreach ($spanMatches[1] ?? [] as $id) { |
| 36 |
$ids[] = (int) $id; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
// Plain-text editor — @[agentId:Display Name] token. |
| 41 |
if (strpos($content, '@[') !== false) { |
| 42 |
preg_match_all('/@\[(\d+):[^\]]+\]/', $content, $tokenMatches); |
| 43 |
|
| 44 |
foreach ($tokenMatches[1] ?? [] as $id) { |
| 45 |
$ids[] = (int) $id; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return array_values(array_unique(array_filter($ids))); |
| 50 |
} |
| 51 |
} |
| 52 |
|