xref: /PHP-8.0/ext/intl/collator/collator_create.c (revision 2b5de6f8)
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    | http://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)25 static int collator_ctor(INTERNAL_FUNCTION_PARAMETERS)
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 	INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len);
42 	COLLATOR_METHOD_FETCH_OBJECT;
43 
44 	if(locale_len == 0) {
45 		locale = intl_locale_get_default();
46 	}
47 
48 	/* Open ICU collator. */
49 	co->ucoll = ucol_open( locale, COLLATOR_ERROR_CODE_P( co ) );
50 	INTL_CTOR_CHECK_STATUS(co, "collator_create: unable to open ICU collator");
51 	return SUCCESS;
52 }
53 /* }}} */
54 
55 /* {{{ Create collator. */
PHP_FUNCTION(collator_create)56 PHP_FUNCTION( collator_create )
57 {
58 	object_init_ex( return_value, Collator_ce_ptr );
59 	if (collator_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU) == FAILURE) {
60 		zval_ptr_dtor(return_value);
61 		RETURN_NULL();
62 	}
63 }
64 /* }}} */
65 
66 /* {{{ Collator object constructor. */
PHP_METHOD(Collator,__construct)67 PHP_METHOD( Collator, __construct )
68 {
69 	zend_error_handling error_handling;
70 
71 	zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, &error_handling);
72 	return_value = ZEND_THIS;
73 	if (collator_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU) == FAILURE) {
74 		if (!EG(exception)) {
75 			zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);
76 		}
77 	}
78 	zend_restore_error_handling(&error_handling);
79 }
80 /* }}} */
81