Commit 7bb7172bad8 for php
commit 7bb7172bad841267566aa9a236783ec6cbc9b6f9
Author: Weilin Du <weilindu@php.net>
Date: Sun Dec 6 02:14:18 2026 +0800
ext/intl: Add remaining getter to Spoofchecker (#26076)
I forget this one when I was doing #25995. This is a follow-up :)
This add getAllowedChars to Spoofchecker which is the getter
method of setAllowedChars. And is the only remaining one in
Spoofchecker now.
diff --git a/NEWS b/NEWS
index 5cfb6b3daa5..f312ad10ac9 100644
--- a/NEWS
+++ b/NEWS
@@ -52,6 +52,7 @@ PHP NEWS
- Intl:
. Added IntlNumberFormatter::toSkeleton(). (Weilin Du)
+ . Added Spoofchecker::getAllowedChars(). (Weilin Du)
. Added Spoofchecker::getChecks(). (Weilin Du)
. Added Spoofchecker::getAllowedLocales(). (Weilin Du)
. Added Spoofchecker::getRestrictionLevel(). (Weilin Du)
diff --git a/UPGRADING b/UPGRADING
index ab5e4d1e83f..d33c56b2997 100644
--- a/UPGRADING
+++ b/UPGRADING
@@ -181,6 +181,8 @@ PHP 8.7 UPGRADE NOTES
the rules cannot be converted to UTF-8.
. Added IntlNumberFormatter::toSkeleton() to export the formatter settings
as a normalized, locale-independent ICU number skeleton.
+ . Added Spoofchecker::getAllowedChars() to export the allowed character set
+ as a UnicodeSet pattern that can be passed to setAllowedChars().
. Added Spoofchecker::getChecks() to retrieve the enabled checks as a bitmask.
. Added Spoofchecker::getAllowedLocales() to retrieve the configured locale
list.
diff --git a/ext/intl/spoofchecker/spoofchecker.stub.php b/ext/intl/spoofchecker/spoofchecker.stub.php
index f5e957513de..5823b049d12 100644
--- a/ext/intl/spoofchecker/spoofchecker.stub.php
+++ b/ext/intl/spoofchecker/spoofchecker.stub.php
@@ -87,6 +87,8 @@ public function getRestrictionLevel(): int {}
public function setAllowedChars(string $pattern, int $patternOptions = 0): void {}
+ public function getAllowedChars(): string {}
+
public function getSkeleton(string $string): string|false {}
#if U_ICU_VERSION_MAJOR_NUM >= 74
diff --git a/ext/intl/spoofchecker/spoofchecker_arginfo.h b/ext/intl/spoofchecker/spoofchecker_arginfo.h
index f76bf73d54b..fb589d9d77c 100644
Binary files a/ext/intl/spoofchecker/spoofchecker_arginfo.h and b/ext/intl/spoofchecker/spoofchecker_arginfo.h differ
diff --git a/ext/intl/spoofchecker/spoofchecker_main.cpp b/ext/intl/spoofchecker/spoofchecker_main.cpp
index f7a165462fd..8e861968520 100644
--- a/ext/intl/spoofchecker/spoofchecker_main.cpp
+++ b/ext/intl/spoofchecker/spoofchecker_main.cpp
@@ -18,9 +18,11 @@
#if __cplusplus >= 201703L
#include <string_view>
-#include <unicode/unistr.h>
#endif
+#include <unicode/uniset.h>
+#include <unicode/uspoof.h>
+
#include "php_intl.h"
#include "intl_convert.h"
#include "spoofchecker_class.h"
@@ -278,6 +280,39 @@ PHP_METHOD(Spoofchecker, setAllowedChars)
}
}
+/* {{{ Get the allowed character set as a UnicodeSet pattern. */
+PHP_METHOD(Spoofchecker, getAllowedChars)
+{
+ SPOOFCHECKER_METHOD_INIT_VARS;
+
+ ZEND_PARSE_PARAMETERS_NONE();
+
+ SPOOFCHECKER_METHOD_FETCH_OBJECT;
+
+ UErrorCode status = U_ZERO_ERROR;
+ const icu::UnicodeSet *allowed = uspoof_getAllowedUnicodeSet(co->uspoof, &status);
+ if (U_SUCCESS(status)) {
+ // Generate a pattern from the actual elements. ICU can cache a pattern
+ // whose meaning depends on the options originally passed to setAllowedChars().
+ icu::UnicodeSet chars;
+ icu::UnicodeString pattern;
+ chars.addAll(*allowed).toPattern(pattern, /* escapeUnprintable */ true);
+ if (chars.isBogus() || pattern.isBogus()) {
+ status = U_MEMORY_ALLOCATION_ERROR;
+ } else {
+ zend_string *result = intl_convert_utf16_to_utf8(pattern.getBuffer(), pattern.length(), &status);
+ if (result != nullptr) {
+ RETURN_STR(result);
+ }
+ }
+ }
+
+ intl_errors_set_code(SPOOFCHECKER_ERROR_P(co), status);
+ zend_throw_exception(IntlException_ce_ptr, "Failed to export allowed characters", 0);
+ RETURN_THROWS();
+}
+/* }}} */
+
/* {{{ Runs an ICU skeleton generator over a UTF-8 string, preflighting the result buffer */
template <typename F>
static zend_string *spoofchecker_skeleton(Spoofchecker_object *co, zend_string *string, F&& skeletonfn)
diff --git a/ext/intl/tests/spoofchecker_get_allowed_chars.phpt b/ext/intl/tests/spoofchecker_get_allowed_chars.phpt
new file mode 100644
index 00000000000..9221f7abc12
--- /dev/null
+++ b/ext/intl/tests/spoofchecker_get_allowed_chars.phpt
@@ -0,0 +1,50 @@
+--TEST--
+Spoofchecker::getAllowedChars() exports a reusable UnicodeSet pattern
+--EXTENSIONS--
+intl
+--FILE--
+<?php
+$checker = new Spoofchecker();
+var_dump($checker->getAllowedChars());
+
+$copy = new Spoofchecker();
+$copy->setChecks(Spoofchecker::CHAR_LIMIT);
+
+$checker->setAllowedChars('[]');
+var_dump($checker->getAllowedChars());
+
+$checker->setAllowedChars('[a-c{ch}]');
+var_dump($checker->getAllowedChars());
+
+$checker->setAllowedChars('[\uD800]');
+var_dump($checker->getAllowedChars());
+
+$checker->setAllowedChars('[[A-C]&[:upper:]]', Spoofchecker::IGNORE_SPACE | Spoofchecker::CASE_INSENSITIVE);
+$copy->setAllowedChars($checker->getAllowedChars());
+var_dump($copy->isSuspicious('aC'), $copy->isSuspicious('d'));
+
+$checker->setAllowedLocales('ru');
+$pattern = $checker->getAllowedChars();
+unset($checker);
+$copy->setAllowedChars($pattern);
+var_dump($copy->isSuspicious("\u{0416}"), $copy->isSuspicious("\u{03A9}"));
+
+$unconstructed = new class extends Spoofchecker {
+ public function __construct() {}
+};
+try {
+ $unconstructed->getAllowedChars();
+} catch (Error $e) {
+ echo $e->getMessage(), "\n";
+}
+?>
+--EXPECT--
+string(19) "[\u0000-\U0010FFFF]"
+string(2) "[]"
+string(9) "[a-c{ch}]"
+string(8) "[\uD800]"
+bool(false)
+bool(true)
+bool(false)
+bool(true)
+Found unconstructed Spoofchecker