20
20
21
21
extern " C" {
22
22
#include " php.h"
23
+ #include " zend_API.h"
24
+ #include " ../intl_common.h"
25
+ #include " ../intl_error.h"
23
26
#include " ../php_intl.h"
24
27
#include " ../intl_data.h"
25
28
#include " rangeformatter_arginfo.h"
@@ -45,16 +48,28 @@ zend_object *IntlNumberRangeFormatter_object_create(zend_class_entry *ce)
45
48
zend_object_std_init (&intern->zo , ce);
46
49
object_properties_init (&intern->zo , ce);
47
50
51
+ // Initialize rangeformatter_data structure
52
+ intl_error_init (&intern->nrf_data .error );
53
+ intern->nrf_data .unumrf = nullptr ;
54
+
55
+ intern->zo .handlers = &rangeformatter_handlers;
56
+
48
57
return &intern->zo ;
49
58
}
50
59
51
60
U_CFUNC PHP_METHOD (IntlNumberRangeFormatter, __construct)
52
61
{
53
-
62
+ ZEND_PARSE_PARAMETERS_NONE ();
63
+ zend_throw_error (NULL , " Cannot directly construct %s, use createFromSkeleton method instead" , ZSTR_VAL (Z_OBJCE_P (ZEND_THIS)->name ));
54
64
}
55
65
56
66
U_CFUNC PHP_METHOD (IntlNumberRangeFormatter, createFromSkeleton)
57
67
{
68
+ #if U_ICU_VERSION_MAJOR_NUM < 63
69
+ zend_throw_error (NULL , " IntlNumberRangeFormatter is not available in ICU 62 and earlier" );
70
+ RETURN_THROWS ();
71
+ #endif
72
+
58
73
char * skeleton;
59
74
char * locale;
60
75
size_t locale_len;
@@ -74,12 +89,12 @@ U_CFUNC PHP_METHOD(IntlNumberRangeFormatter, createFromSkeleton)
74
89
}
75
90
76
91
if (skeleton_len == 0 ) {
77
- zend_argument_value_error ( 1 , " Skeleton string cannot be empty " );
92
+ zend_argument_must_not_be_empty_error ( 1 );
78
93
RETURN_THROWS ();
79
94
}
80
95
81
96
if (locale_len > INTL_MAX_LOCALE_LEN) {
82
- zend_argument_value_error (2 , " Locale string too long, should be no longer than %d characters" , INTL_MAX_LOCALE_LEN);
97
+ zend_argument_value_error (2 , " must be no longer than %d characters" , INTL_MAX_LOCALE_LEN);
83
98
RETURN_THROWS ();
84
99
}
85
100
@@ -89,20 +104,26 @@ U_CFUNC PHP_METHOD(IntlNumberRangeFormatter, createFromSkeleton)
89
104
}
90
105
91
106
if (collapse != UNUM_RANGE_COLLAPSE_AUTO && collapse != UNUM_RANGE_COLLAPSE_NONE && collapse != UNUM_RANGE_COLLAPSE_UNIT && collapse != UNUM_RANGE_COLLAPSE_ALL) {
92
- zend_argument_value_error (3 , " Invalid collapse value " );
107
+ zend_argument_value_error (3 , " must be one of IntlNumberRangeFormatter::COLLAPSE_AUTO, IntlNumberRangeFormatter::COLLAPSE_NONE, IntlNumberRangeFormatter::COLLAPSE_UNIT, or IntlNumberRangeFormatter::COLLAPSE_ALL " );
93
108
RETURN_THROWS ();
94
109
}
95
110
96
111
if (identityFallback != UNUM_IDENTITY_FALLBACK_SINGLE_VALUE && identityFallback != UNUM_IDENTITY_FALLBACK_APPROXIMATELY_OR_SINGLE_VALUE && identityFallback != UNUM_IDENTITY_FALLBACK_APPROXIMATELY && identityFallback != UNUM_IDENTITY_FALLBACK_RANGE) {
97
- zend_argument_value_error (4 , " Invalid identity fallback value " );
112
+ zend_argument_value_error (4 , " must be one of IntlNumberRangeFormatter::IDENTITY_FALLBACK_SINGLE_VALUE, IntlNumberRangeFormatter::IDENTITY_FALLBACK_APPROXIMATELY_OR_SINGLE_VALUE, IntlNumberRangeFormatter::IDENTITY_FALLBACK_APPROXIMATELY, or IntlNumberRangeFormatter::IDENTITY_FALLBACK_RANGE " );
98
113
RETURN_THROWS ();
99
114
}
100
115
101
- UErrorCode error = U_ZERO_ERROR;
116
+ UErrorCode status = U_ZERO_ERROR;
102
117
103
118
UnicodeString skeleton_ustr (skeleton, skeleton_len);
104
119
105
- UnlocalizedNumberFormatter nf = NumberFormatter::forSkeleton (skeleton_ustr, error);
120
+ UnlocalizedNumberFormatter nf = NumberFormatter::forSkeleton (skeleton_ustr, status);
121
+
122
+ if (U_FAILURE (status)) {
123
+ intl_error_set (NULL , status, " Failed to create the number skeleton" , 0 );
124
+ zend_throw_exception (IntlException_ce_ptr, " Failed to create the number skeleton" , 0 );
125
+ RETURN_THROWS ();
126
+ }
106
127
107
128
LocalizedNumberRangeFormatter* nrf = new LocalizedNumberRangeFormatter (
108
129
NumberRangeFormatter::with ()
@@ -113,7 +134,6 @@ U_CFUNC PHP_METHOD(IntlNumberRangeFormatter, createFromSkeleton)
113
134
);
114
135
115
136
zend_object* obj = IntlNumberRangeFormatter_object_create (class_entry_IntlNumberRangeFormatter);
116
- obj->handlers = &rangeformatter_handlers;
117
137
118
138
RANGEFORMATTER_OBJECT (php_intl_numberrangeformatter_fetch_object (obj)) = nrf;
119
139
@@ -123,37 +143,58 @@ U_CFUNC PHP_METHOD(IntlNumberRangeFormatter, createFromSkeleton)
123
143
124
144
U_CFUNC PHP_METHOD (IntlNumberRangeFormatter, format)
125
145
{
126
- double start;
127
- double end;
146
+ zval * start;
147
+ zval * end;
128
148
129
149
IntlNumberRangeFormatter_object* obj = Z_INTL_RANGEFORMATTER_P (ZEND_THIS);
130
150
131
151
ZEND_PARSE_PARAMETERS_START (2 , 2 )
132
- Z_PARAM_DOUBLE (start)
133
- Z_PARAM_DOUBLE (end)
152
+ Z_PARAM_NUMBER (start)
153
+ Z_PARAM_NUMBER (end)
134
154
ZEND_PARSE_PARAMETERS_END ();
135
155
136
156
UErrorCode error = U_ZERO_ERROR;
137
157
138
- UnicodeString result = RANGEFORMATTER_OBJECT (obj)->formatFormattableRange (start, end, error).toString (error);
158
+ icu::Formattable start_formattable (Z_TYPE_P (start) == IS_DOUBLE ? Z_DVAL_P (start) : Z_LVAL_P (start));
159
+ icu::Formattable end_formattable (Z_TYPE_P (end) == IS_DOUBLE ? Z_DVAL_P (end) : Z_LVAL_P (end));
160
+
161
+ UnicodeString result = RANGEFORMATTER_OBJECT (obj)->formatFormattableRange (start_formattable, end_formattable, error).toString (error);
139
162
140
163
if (U_FAILURE (error)) {
141
164
intl_error_set (NULL , error, " Failed to format number range" , 0 );
142
-
143
- return ;
165
+ zend_throw_exception (IntlException_ce_ptr, " Failed to format number range " , 0 );
166
+ RETURN_THROWS () ;
144
167
}
145
168
146
169
zend_string *ret = intl_charFromString (result, &error);
147
170
148
- if (!ret ) {
171
+ if (U_FAILURE (error) ) {
149
172
intl_error_set (NULL , error, " Failed to convert result to UTF-8" , 0 );
150
- // RETVAL_FALSE ;
151
- return ;
173
+ zend_throw_exception (IntlException_ce_ptr, " Failed to convert result to UTF-8 " , 0 ) ;
174
+ RETURN_THROWS () ;
152
175
}
153
176
154
177
RETVAL_NEW_STR (ret);
155
178
}
156
179
180
+ U_CFUNC PHP_METHOD (IntlNumberRangeFormatter, getErrorCode)
181
+ {
182
+ ZEND_PARSE_PARAMETERS_NONE ();
183
+
184
+ IntlNumberRangeFormatter_object* obj = Z_INTL_RANGEFORMATTER_P (ZEND_THIS);
185
+
186
+ RETURN_LONG (intl_error_get_code (&obj->nrf_data .error ));
187
+ }
188
+
189
+ U_CFUNC PHP_METHOD (IntlNumberRangeFormatter, getErrorMessage)
190
+ {
191
+ ZEND_PARSE_PARAMETERS_NONE ();
192
+
193
+ IntlNumberRangeFormatter_object* obj = Z_INTL_RANGEFORMATTER_P (ZEND_THIS);
194
+
195
+ RETURN_STR (intl_error_get_message (&obj->nrf_data .error ));
196
+ }
197
+
157
198
void IntlNumberRangeFormatter_object_free (zend_object *object)
158
199
{
159
200
IntlNumberRangeFormatter_object* nfo = php_intl_numberrangeformatter_fetch_object (object);
0 commit comments