xref: /PHP-8.1/ext/intl/collator/collator_create.c (revision 925a3097)
1 /*
2    +----------------------------------------------------------------------+
3    | This source file is subject to version 3.01 of the PHP license,      |
4    | that is bundled with this package in the file LICENSE, and is        |
5    | available through the world-wide-web at the following url:           |
6    | https://www.php.net/license/3_01.txt                                 |
7    | If you did not receive a copy of the PHP license and are unable to   |
8    | obtain it through the world-wide-web, please send a note to          |
9    | license@php.net so we can mail you a copy immediately.               |
10    +----------------------------------------------------------------------+
11    | Authors: Vadim Savchuk <vsavchuk@productengine.com>                  |
12    |          Dmitry Lakhtyuk <dlakhtyuk@productengine.com>               |
13    +----------------------------------------------------------------------+
14  */
15 
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
19 
20 #include "php_intl.h"
21 #include "collator_class.h"
22 #include "intl_data.h"
23 
24 /* {{{ */
collator_ctor(INTERNAL_FUNCTION_PARAMETERS,zend_error_handling * error_handling,bool * error_handling_replaced)25 static int collator_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_error_handling *error_handling, bool *error_handling_replaced)
26 {
27 	const char*      locale;
28 	size_t           locale_len = 0;
29 	zval*            object;
30 	Collator_object* co;
31 
32 	intl_error_reset( NULL );
33 	object = return_value;
34 	/* Parse parameters. */
35 	if( zend_parse_parameters( ZEND_NUM_ARGS(), "s",
36 		&locale, &locale_len ) == FAILURE )
37 	{
38 		return FAILURE;
39 	}
40 
41 	if (error_handling != NULL) {
42 		zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, error_handling);
43 		*error_handling_replaced = 1;
44 	}
45 
46 	INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len);
47 	COLLATOR_METHOD_FETCH_OBJECT;
48 
49 	if(locale_len == 0) {
50 		locale = intl_locale_get_default();
51 	}
52 
53 	/* Open ICU collator. */
54 	co->ucoll = ucol_open( locale, COLLATOR_ERROR_CODE_P( co ) );
55 	INTL_CTOR_CHECK_STATUS(co, "collator_create: unable to open ICU collator");
56 	return SUCCESS;
57 }
58 /* }}} */
59 
60 /* {{{ Create collator. */
PHP_FUNCTION(collator_create)61 PHP_FUNCTION( collator_create )
62 {
63 	object_init_ex( return_value, Collator_ce_ptr );
64 	if (collator_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL, NULL) == FAILURE) {
65 		zval_ptr_dtor(return_value);
66 		RETURN_NULL();
67 	}
68 }
69 /* }}} */
70 
71 /* {{{ Collator object constructor. */
PHP_METHOD(Collator,__construct)72 PHP_METHOD( Collator, __construct )
73 {
74 	zend_error_handling error_handling;
75 	bool error_handling_replaced = 0;
76 
77 	return_value = ZEND_THIS;
78 	if (collator_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, &error_handling, &error_handling_replaced) == FAILURE) {
79 		if (!EG(exception)) {
80 			zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);
81 		}
82 	}
83 	if (error_handling_replaced) {
84 		zend_restore_error_handling(&error_handling);
85 	}
86 }
87 /* }}} */
88