xref: /PHP-7.4/ext/intl/collator/collator_class.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 #include "collator_class.h"
19 #include "php_intl.h"
20 #include "collator_attr.h"
21 #include "collator_compare.h"
22 #include "collator_sort.h"
23 #include "collator_convert.h"
24 #include "collator_locale.h"
25 #include "collator_create.h"
26 #include "collator_error.h"
27 #include "intl_error.h"
28 
29 #include <unicode/ucol.h>
30 
31 zend_class_entry *Collator_ce_ptr = NULL;
32 static zend_object_handlers Collator_handlers;
33 
34 /*
35  * Auxiliary functions needed by objects of 'Collator' class
36  */
37 
38 /* {{{ Collator_objects_free */
Collator_objects_free(zend_object * object)39 void Collator_objects_free(zend_object *object )
40 {
41 	Collator_object* co = php_intl_collator_fetch_object(object);
42 
43 	zend_object_std_dtor(&co->zo );
44 
45 	collator_object_destroy(co );
46 }
47 /* }}} */
48 
49 /* {{{ Collator_object_create */
Collator_object_create(zend_class_entry * ce)50 zend_object *Collator_object_create(zend_class_entry *ce )
51 {
52 	Collator_object *intern = zend_object_alloc(sizeof(Collator_object), ce);
53 	intl_error_init(COLLATOR_ERROR_P(intern));
54 	zend_object_std_init(&intern->zo, ce );
55 	object_properties_init(&intern->zo, ce);
56 
57 	intern->zo.handlers = &Collator_handlers;
58 
59 	return &intern->zo;
60 }
61 /* }}} */
62 
63 /*
64  * 'Collator' class registration structures & functions
65  */
66 
67 /* {{{ Collator methods arguments info */
68 /* NOTE: modifying 'collator_XX_args' do not forget to
69        modify approptiate 'collator_XX_args' for
70        the procedural API.
71 */
72 ZEND_BEGIN_ARG_INFO_EX( collator_0_args, 0, 0, 0 )
73 ZEND_END_ARG_INFO()
74 
75 ZEND_BEGIN_ARG_INFO_EX( collator_1_arg, 0, 0, 1 )
76 	ZEND_ARG_INFO( 0, arg1 )
77 ZEND_END_ARG_INFO()
78 
79 ZEND_BEGIN_ARG_INFO_EX( collator_2_args, 0, 0, 2 )
80 	ZEND_ARG_INFO( 0, arg1 )
81 	ZEND_ARG_INFO( 0, arg2 )
82 ZEND_END_ARG_INFO()
83 
84 ZEND_BEGIN_ARG_INFO_EX( collator_sort_args, 0, 0, 1 )
85 	ZEND_ARG_ARRAY_INFO( 1, arr, 0 )
86 	ZEND_ARG_INFO( 0, flags )
87 ZEND_END_ARG_INFO()
88 
89 ZEND_BEGIN_ARG_INFO_EX( collator_sort_with_sort_keys_args, 0, 0, 1 )
90 	ZEND_ARG_ARRAY_INFO( 1, arr, 0 )
91 ZEND_END_ARG_INFO()
92 
93 /* }}} */
94 
95 /* {{{ Collator_class_functions
96  * Every 'Collator' class method has an entry in this table
97  */
98 
99 static const zend_function_entry Collator_class_functions[] = {
100 	PHP_ME( Collator, __construct, collator_1_arg, ZEND_ACC_PUBLIC )
101 	ZEND_FENTRY( create, ZEND_FN( collator_create ), collator_1_arg, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
102 	PHP_NAMED_FE( compare, ZEND_FN( collator_compare ), collator_2_args )
103 	PHP_NAMED_FE( sort, ZEND_FN( collator_sort ), collator_sort_args )
104 	PHP_NAMED_FE( sortWithSortKeys, ZEND_FN( collator_sort_with_sort_keys ), collator_sort_with_sort_keys_args )
105 	PHP_NAMED_FE( asort, ZEND_FN( collator_asort ), collator_sort_args )
106 	PHP_NAMED_FE( getAttribute, ZEND_FN( collator_get_attribute ), collator_1_arg )
107 	PHP_NAMED_FE( setAttribute, ZEND_FN( collator_set_attribute ), collator_2_args )
108 	PHP_NAMED_FE( getStrength, ZEND_FN( collator_get_strength ), collator_0_args )
109 	PHP_NAMED_FE( setStrength, ZEND_FN( collator_set_strength ), collator_1_arg )
110 	PHP_NAMED_FE( getLocale, ZEND_FN( collator_get_locale ), collator_1_arg )
111 	PHP_NAMED_FE( getErrorCode, ZEND_FN( collator_get_error_code ), collator_0_args )
112 	PHP_NAMED_FE( getErrorMessage, ZEND_FN( collator_get_error_message ), collator_0_args )
113 	PHP_NAMED_FE( getSortKey, ZEND_FN( collator_get_sort_key ), collator_1_arg )
114 	PHP_FE_END
115 };
116 /* }}} */
117 
118 /* {{{ collator_register_Collator_class
119  * Initialize 'Collator' class
120  */
collator_register_Collator_class(void)121 void collator_register_Collator_class( void )
122 {
123 	zend_class_entry ce;
124 
125 	/* Create and register 'Collator' class. */
126 	INIT_CLASS_ENTRY( ce, "Collator", Collator_class_functions );
127 	ce.create_object = Collator_object_create;
128 	Collator_ce_ptr = zend_register_internal_class( &ce );
129 
130 	memcpy(&Collator_handlers, &std_object_handlers,
131 		sizeof Collator_handlers);
132 	/* Collator has no usable clone semantics - ucol_cloneBinary/ucol_openBinary require binary buffer
133 	   for which we don't have the place to keep */
134 	Collator_handlers.offset = XtOffsetOf(Collator_object, zo);
135 	Collator_handlers.clone_obj = NULL;
136 	Collator_handlers.free_obj = Collator_objects_free;
137 
138 	/* Declare 'Collator' class properties. */
139 	if( !Collator_ce_ptr )
140 	{
141 		zend_error( E_ERROR,
142 			"Collator: attempt to create properties "
143 			"on a non-registered class." );
144 		return;
145 	}
146 }
147 /* }}} */
148 
149 /* {{{ void collator_object_init( Collator_object* co )
150  * Initialize internals of Collator_object.
151  * Must be called before any other call to 'collator_object_...' functions.
152  */
collator_object_init(Collator_object * co)153 void collator_object_init( Collator_object* co )
154 {
155 	if( !co )
156 		return;
157 
158 	intl_error_init( COLLATOR_ERROR_P( co ) );
159 }
160 /* }}} */
161 
162 /* {{{ void collator_object_destroy( Collator_object* co )
163  * Clean up mem allocted by internals of Collator_object
164  */
collator_object_destroy(Collator_object * co)165 void collator_object_destroy( Collator_object* co )
166 {
167 	if( !co )
168 		return;
169 
170 	if( co->ucoll )
171 	{
172 		ucol_close( co->ucoll );
173 		co->ucoll = NULL;
174 	}
175 
176 	intl_error_reset( COLLATOR_ERROR_P( co ) );
177 }
178 /* }}} */
179