Skip to content

Commit 6f1f465

Browse files
authored
ext/intl: Throw ValueError for invalid break iterator locale type (php#21997)
1 parent 10dad92 commit 6f1f465

4 files changed

Lines changed: 32 additions & 4 deletions

File tree

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ PHP NEWS
6666
. Added grapheme_strrev (Yuya Hamada)
6767
. Passing a non-stringable object as a time zone to Intl time zone
6868
argument handling now raises TypeError instead of Error. (Weilin Du)
69+
. IntlBreakIterator::getLocale() now raises ValueError for invalid locale
70+
types. (Weilin Du)
6971

7072
- JSON:
7173
. Enriched JSON last error / exception message with error location.

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ PHP 8.6 UPGRADE NOTES
3737
- Intl:
3838
. Passing a non-stringable object as a time zone to Intl APIs that accept
3939
time zone objects or strings now raises a TypeError instead of an Error.
40+
. IntlBreakIterator::getLocale() now raises a ValueError when the type is
41+
neither Locale::ACTUAL_LOCALE nor Locale::VALID_LOCALE instead of
42+
returning false.
4043

4144
- PCNTL:
4245
. pcntl_alarm() now raises a ValueError if the seconds argument is

ext/intl/breakiterator/breakiterator_methods.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,9 @@ U_CFUNC PHP_METHOD(IntlBreakIterator, getLocale)
297297
Z_PARAM_LONG(locale_type)
298298
ZEND_PARSE_PARAMETERS_END();
299299

300-
/* TODO: Change to ValueError? */
301300
if (locale_type != ULOC_ACTUAL_LOCALE && locale_type != ULOC_VALID_LOCALE) {
302-
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
303-
"invalid locale type");
304-
RETURN_FALSE;
301+
zend_argument_value_error(1, "must be either Locale::ACTUAL_LOCALE or Locale::VALID_LOCALE");
302+
RETURN_THROWS();
305303
}
306304

307305
BREAKITER_METHOD_FETCH_OBJECT;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
IntlBreakIterator::getLocale(): bad arguments
3+
--EXTENSIONS--
4+
intl
5+
--FILE--
6+
<?php
7+
8+
$bi = IntlBreakIterator::createSentenceInstance('pt');
9+
10+
try {
11+
$bi->getLocale(2);
12+
} catch (Throwable $e) {
13+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
14+
}
15+
16+
try {
17+
$bi->getLocale(-1);
18+
} catch (Throwable $e) {
19+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
20+
}
21+
22+
?>
23+
--EXPECT--
24+
ValueError: IntlBreakIterator::getLocale(): Argument #1 ($type) must be either Locale::ACTUAL_LOCALE or Locale::VALID_LOCALE
25+
ValueError: IntlBreakIterator::getLocale(): Argument #1 ($type) must be either Locale::ACTUAL_LOCALE or Locale::VALID_LOCALE

0 commit comments

Comments
 (0)