Files
2026-09-25 11:54:28 -04:00

33 lines
839 B
PHP

<?php
declare(strict_types=1);
namespace KTXM\ProviderImap\Client\Protocol\Parser;
/**
* Parses the optional leading bracketed code in response text.
* Command-specific argument interpretation belongs to the caller.
*/
final class ResponseCodeParser
{
/**
* @return ?array{name:string, arguments:list<string>, text:string}
*/
public function parse(string $text): ?array
{
$text = trim($text);
if (preg_match('/^\[([A-Z0-9.-]+)(?:\s+([^\]]+))?\](?:\s*(.*))?$/i', $text, $matches) !== 1) {
return null;
}
$arguments = trim($matches[2] ?? '');
return [
'name' => strtoupper($matches[1]),
'arguments' => $arguments === '' ? [] : (preg_split('/\s+/', $arguments) ?: []),
'text' => trim($matches[3] ?? ''),
];
}
}