self::isPlausibleDdd($digits) && $digits[2] === '9' ? sprintf('(%s) %s-%s', substr($digits, 0, 2), substr($digits, 2, 5), substr($digits, 7)) : $collapsed, 10 => self::isPlausibleDdd($digits) ? sprintf('(%s) %s-%s', substr($digits, 0, 2), substr($digits, 2, 4), substr($digits, 6)) : $collapsed, default => $collapsed, }; } /** * A Brazilian DDD (area code) runs 11-99: the first digit is never * '0' (not a valid leading digit) and the second is never '0' either * (no DDD like "10", "20", "30" exists). This does not check that the * DDD is one of the officially assigned codes — only that its shape * is plausible enough to distinguish it from a foreign number. */ private static function isPlausibleDdd(string $digits): bool { return preg_match('/^[1-9][1-9]/', $digits) === 1; } }