xref: /php-src/ext/intl/collator/collator_create.c (revision 11accb5c)
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 	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 	ZEND_PARSE_PARAMETERS_START(1, 1)
35 		Z_PARAM_STRING(locale, locale_len)
36 	ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
37 
38 	if (error_handling != NULL) {
39 		zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, error_handling);
40 		*error_handling_replaced = 1;
41 	}
42 
43 	INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len);
44 	COLLATOR_METHOD_FETCH_OBJECT;
45 
46 	if(locale_len == 0) {
47 		locale = (char *)intl_locale_get_default();
48 	}
49 
50 	/* Open ICU collator. */
51 	co->ucoll = ucol_open( locale, COLLATOR_ERROR_CODE_P( co ) );
52 	INTL_CTOR_CHECK_STATUS(co, "collator_create: unable to open ICU collator");
53 	return SUCCESS;
54 }
55 /* }}} */
56 
57 /* {{{ Create collator. */
PHP_FUNCTION(collator_create)58 PHP_FUNCTION( collator_create )
59 {
60 	object_init_ex( return_value, Collator_ce_ptr );
61 	if (collator_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL, NULL) == FAILURE) {
62 		zval_ptr_dtor(return_value);
63 		RETURN_NULL();
64 	}
65 }
66 /* }}} */
67 
68 /* {{{ Collator object constructor. */
PHP_METHOD(Collator,__construct)69 PHP_METHOD( Collator, __construct )
70 {
71 	zend_error_handling error_handling;
72 	bool error_handling_replaced = 0;
73 
74 	return_value = ZEND_THIS;
75 	if (collator_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, &error_handling, &error_handling_replaced) == FAILURE) {
76 		if (!EG(exception)) {
77 			zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);
78 		}
79 	}
80 	if (error_handling_replaced) {
81 		zend_restore_error_handling(&error_handling);
82 	}
83 }
84 /* }}} */
85