@@ -168,84 +168,134 @@ PHP_FUNCTION( resourcebundle_create )
168
168
/* }}} */
169
169
170
170
/* {{{ resourcebundle_array_fetch */
171
- static void resourcebundle_array_fetch (zend_object * object , zval * offset , zval * return_value , int fallback )
171
+ static zval * resource_bundle_array_fetch (
172
+ zend_object * object , zend_string * offset_str , zend_long offset_int ,
173
+ zval * return_value , bool fallback , uint32_t offset_arg_num )
172
174
{
173
- int32_t meindex = 0 ;
174
- char * mekey = NULL ;
175
- bool is_numeric = 0 ;
176
- char * pbuf ;
175
+ int32_t index = 0 ;
176
+ char * key = NULL ;
177
+ bool is_numeric = offset_str == NULL ;
178
+ char * pbuf ;
177
179
ResourceBundle_object * rb ;
178
180
179
181
rb = php_intl_resourcebundle_fetch_object (object );
180
182
intl_error_reset (NULL );
181
183
intl_error_reset (INTL_DATA_ERROR_P (rb ));
182
184
183
- if (Z_TYPE_P (offset ) == IS_LONG ) {
184
- is_numeric = 1 ;
185
- meindex = (int32_t )Z_LVAL_P (offset );
186
- rb -> child = ures_getByIndex ( rb -> me , meindex , rb -> child , & INTL_DATA_ERROR_CODE (rb ) );
187
- } else if (Z_TYPE_P (offset ) == IS_STRING ) {
188
- mekey = Z_STRVAL_P (offset );
189
- rb -> child = ures_getByKey (rb -> me , mekey , rb -> child , & INTL_DATA_ERROR_CODE (rb ) );
185
+ if (offset_str ) {
186
+ if (UNEXPECTED (ZSTR_LEN (offset_str ) == 0 )) {
187
+ if (offset_arg_num ) {
188
+ zend_argument_value_error (offset_arg_num , "cannot be empty" );
189
+ } else {
190
+ zend_value_error ("Offset cannot be empty" );
191
+ }
192
+ return NULL ;
193
+ }
194
+ key = ZSTR_VAL (offset_str );
195
+ rb -> child = ures_getByKey (rb -> me , key , rb -> child , & INTL_DATA_ERROR_CODE (rb ) );
190
196
} else {
191
- intl_errors_set (INTL_DATA_ERROR_P (rb ), U_ILLEGAL_ARGUMENT_ERROR ,
192
- "resourcebundle_get: index should be integer or string" , 0 );
193
- RETURN_NULL ();
197
+ if (UNEXPECTED (offset_int < (zend_long )INT32_MIN || offset_int > (zend_long )INT32_MAX )) {
198
+ if (offset_arg_num ) {
199
+ zend_argument_value_error (offset_arg_num , "index must be between %d and %d" , INT32_MIN , INT32_MAX );
200
+ } else {
201
+ zend_value_error ("Index must be between %d and %d" , INT32_MIN , INT32_MAX );
202
+ }
203
+ return NULL ;
204
+ }
205
+ index = (int32_t )offset_int ;
206
+ rb -> child = ures_getByIndex (rb -> me , index , rb -> child , & INTL_DATA_ERROR_CODE (rb ));
194
207
}
195
208
196
209
intl_error_set_code ( NULL , INTL_DATA_ERROR_CODE (rb ) );
197
210
if (U_FAILURE (INTL_DATA_ERROR_CODE (rb ))) {
198
211
if (is_numeric ) {
199
- spprintf ( & pbuf , 0 , "Cannot load resource element %d" , meindex );
212
+ spprintf ( & pbuf , 0 , "Cannot load resource element %d" , index );
200
213
} else {
201
- spprintf ( & pbuf , 0 , "Cannot load resource element '%s'" , mekey );
214
+ spprintf ( & pbuf , 0 , "Cannot load resource element '%s'" , key );
202
215
}
203
216
intl_errors_set_custom_msg ( INTL_DATA_ERROR_P (rb ), pbuf , 1 );
204
217
efree (pbuf );
205
- RETURN_NULL ();
218
+ RETVAL_NULL ();
219
+ return return_value ;
206
220
}
207
221
208
222
if (!fallback && (INTL_DATA_ERROR_CODE (rb ) == U_USING_FALLBACK_WARNING || INTL_DATA_ERROR_CODE (rb ) == U_USING_DEFAULT_WARNING )) {
209
223
UErrorCode icuerror ;
210
224
const char * locale = ures_getLocaleByType ( rb -> me , ULOC_ACTUAL_LOCALE , & icuerror );
211
225
if (is_numeric ) {
212
- spprintf ( & pbuf , 0 , "Cannot load element %d without fallback from to %s" , meindex , locale );
226
+ spprintf (& pbuf , 0 , "Cannot load element %d without fallback from to %s" , index , locale );
213
227
} else {
214
- spprintf ( & pbuf , 0 , "Cannot load element '%s' without fallback from to %s" , mekey , locale );
228
+ spprintf (& pbuf , 0 , "Cannot load element '%s' without fallback from to %s" , key , locale );
215
229
}
216
- intl_errors_set_custom_msg ( INTL_DATA_ERROR_P (rb ), pbuf , 1 );
230
+ intl_errors_set_custom_msg ( INTL_DATA_ERROR_P (rb ), pbuf , 1 );
217
231
efree (pbuf );
218
- RETURN_NULL ();
232
+ RETVAL_NULL ();
233
+ return return_value ;
219
234
}
220
235
221
236
resourcebundle_extract_value ( return_value , rb );
237
+ return return_value ;
222
238
}
223
239
/* }}} */
224
240
225
241
/* {{{ resourcebundle_array_get */
226
242
zval * resourcebundle_array_get (zend_object * object , zval * offset , int type , zval * rv )
227
243
{
228
- if (offset == NULL ) {
229
- php_error ( E_ERROR , "Cannot apply [] to ResourceBundle object" );
244
+ if (offset == NULL ) {
245
+ zend_throw_error (NULL , "Cannot apply [] to ResourceBundle object" );
246
+ return NULL ;
247
+ }
248
+
249
+ ZVAL_DEREF (offset );
250
+ if (Z_TYPE_P (offset ) == IS_LONG ) {
251
+ return resource_bundle_array_fetch (object , /* offset_str */ NULL , Z_LVAL_P (offset ), rv , /* fallback */ true, /* arg_num */ 0 );
252
+ } else if (Z_TYPE_P (offset ) == IS_STRING ) {
253
+ return resource_bundle_array_fetch (object , Z_STR_P (offset ), /* offset_int */ 0 , rv , /* fallback */ true, /* arg_num */ 0 );
254
+ } else {
255
+ zend_illegal_container_offset (object -> ce -> name , offset , type );
256
+ return NULL ;
230
257
}
231
- ZVAL_NULL (rv );
232
- resourcebundle_array_fetch (object , offset , rv , 1 );
233
- return rv ;
234
258
}
235
259
/* }}} */
236
260
237
261
/* {{{ Get resource identified by numerical index or key name. */
238
262
PHP_FUNCTION ( resourcebundle_get )
239
263
{
240
- bool fallback = 1 ;
241
- zval * offset ;
242
- zval * object ;
243
-
244
- if (zend_parse_method_parameters (ZEND_NUM_ARGS (), getThis (), "Oz|b" , & object , ResourceBundle_ce_ptr , & offset , & fallback ) == FAILURE ) {
264
+ bool fallback = true;
265
+ zend_object * resource_bundle = NULL ;
266
+ zend_string * offset_str = NULL ;
267
+ zend_long offset_long = 0 ;
268
+
269
+ ZEND_PARSE_PARAMETERS_START (2 , 3 )
270
+ Z_PARAM_OBJ_OF_CLASS (resource_bundle , ResourceBundle_ce_ptr )
271
+ Z_PARAM_STR_OR_LONG (offset_str , offset_long )
272
+ Z_PARAM_OPTIONAL
273
+ Z_PARAM_BOOL (fallback )
274
+ ZEND_PARSE_PARAMETERS_END ();
275
+
276
+ zval * retval = resource_bundle_array_fetch (resource_bundle , offset_str , offset_long , return_value , fallback , /* arg_num */ 2 );
277
+ if (!retval ) {
245
278
RETURN_THROWS ();
246
279
}
280
+ }
281
+ /* }}} */
247
282
248
- resourcebundle_array_fetch (Z_OBJ_P (object ), offset , return_value , fallback );
283
+ PHP_METHOD (ResourceBundle , get )
284
+ {
285
+ bool fallback = true;
286
+ zend_string * offset_str = NULL ;
287
+ zend_long offset_long = 0 ;
288
+
289
+ ZEND_PARSE_PARAMETERS_START (1 , 2 )
290
+ Z_PARAM_STR_OR_LONG (offset_str , offset_long )
291
+ Z_PARAM_OPTIONAL
292
+ Z_PARAM_BOOL (fallback )
293
+ ZEND_PARSE_PARAMETERS_END ();
294
+
295
+ zval * retval = resource_bundle_array_fetch (Z_OBJ_P (ZEND_THIS ), offset_str , offset_long , return_value , fallback , /* arg_num */ 1 );
296
+ if (!retval ) {
297
+ RETURN_THROWS ();
298
+ }
249
299
}
250
300
/* }}} */
251
301
0 commit comments