8efb82ca43
Three pilot-surfaced issues in one pass: 1. PhoneParser regex used '\s' in the candidate character class, so a digit on a following flattened line bled into the phone. '+7977 6277470\n1' (phone + trailing checkbox value) parsed as 12 digits, producing '+797762774701'. Replaced '\s' with a literal space so newlines/tabs stop the candidate. 2. Custom-themed forms don't use 'your-name' / 'your-email' keys (washanyanya.ru has text-211 / text-212 / name_user). Added FieldDetector::find_name + find_email as fallbacks. Classifier and MessageFormatter now call them if no standard key matches, so 'Александр' in text-211 is recognised as a cyrillic name and scored / displayed accordingly. 3. FlamingoHelper copied '_from_email' into 'your-email' without validating it. Flamingo derives that meta from whatever field it considers "first"; on phone-first forms it ends up holding the phone number. Now we only copy _from_email if it contains '@', and dropped the _from_name shortcut entirely — FieldDetector handles the name via the actual field values. Tests: +1 FieldDetectorTest (13 cases); +2 PhoneParserTest cases for newline/tab bleeds; +1 ClassifierTest case; +1 MessageFormatterTest case for custom field keys. Full suite 53/53 green. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
78 lines
2.5 KiB
PHP
78 lines
2.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Cf7stg;
|
|
|
|
final class PhoneParser
|
|
{
|
|
/**
|
|
* Extract and normalize the first phone number from arbitrary text.
|
|
*
|
|
* @return array{e164:string,pretty:string,is_russian:bool}|null
|
|
*/
|
|
public static function parse(string $text): ?array
|
|
{
|
|
$text = (string)$text;
|
|
if ($text === '') {
|
|
return null;
|
|
}
|
|
|
|
// Find a run of digits, spaces, dashes, parens, dots and a leading '+',
|
|
// with at least 10 digits in total. Walk candidates left-to-right and
|
|
// pick the first that yields 10 or 11 digits.
|
|
// NOTE: the space class is a literal ' ' — not '\s'. If '\s' is used
|
|
// a newline or tab between two adjacent form fields (phone value,
|
|
// then checkbox value) would bleed into the phone and append extra
|
|
// digits. MessageFormatter/Classifier flatten fields with "\n" before
|
|
// calling us, so this matters in practice.
|
|
if (!preg_match_all('/\+?[\d][\d ().\-]{8,}/u', $text, $m)) {
|
|
return null;
|
|
}
|
|
|
|
foreach ($m[0] as $candidate) {
|
|
$digits = preg_replace('/\D+/', '', $candidate) ?? '';
|
|
$plus = strpos($candidate, '+') === 0;
|
|
|
|
// Russian detection & normalization
|
|
if (!$plus && strlen($digits) === 11 && $digits[0] === '8') {
|
|
$digits = '7' . substr($digits, 1);
|
|
}
|
|
if (!$plus && strlen($digits) === 10 && preg_match('/^[3-9]/', $digits)) {
|
|
// Bare 10-digit RU mobile/landline area code
|
|
$digits = '7' . $digits;
|
|
}
|
|
|
|
if (strlen($digits) < 10 || strlen($digits) > 15) {
|
|
continue;
|
|
}
|
|
|
|
$e164 = '+' . $digits;
|
|
$is_ru = (strlen($digits) === 11 && $digits[0] === '7');
|
|
|
|
return [
|
|
'e164' => $e164,
|
|
'pretty' => self::format_pretty($e164, $is_ru),
|
|
'is_russian' => $is_ru,
|
|
];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private static function format_pretty(string $e164, bool $is_ru): string
|
|
{
|
|
if ($is_ru) {
|
|
// +7 XXX XXX-XX-XX → "+7 (XXX) XXX-XX-XX"
|
|
$d = substr($e164, 2);
|
|
return sprintf(
|
|
'+7 (%s) %s-%s-%s',
|
|
substr($d, 0, 3),
|
|
substr($d, 3, 3),
|
|
substr($d, 6, 2),
|
|
substr($d, 8, 2)
|
|
);
|
|
}
|
|
return $e164;
|
|
}
|
|
}
|