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