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: Mel Dafert (mel@dafert.at)                                  |
12    +----------------------------------------------------------------------+
13 */
14 
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
18 
19 #include "../intl_cppshims.h"
20 
21 #include <unicode/dtptngen.h>
22 
23 extern "C" {
24 #define USE_DATETIMEPATTERNGENERATOR_POINTER 1
25 #include "datepatterngenerator_class.h"
26 #include "datepatterngenerator_arginfo.h"
27 #include <zend_exceptions.h>
28 #include <assert.h>
29 }
30 
31 using icu::DateTimePatternGenerator;
32 using icu::Locale;
33 
34 zend_class_entry *IntlDatePatternGenerator_ce_ptr;
35 zend_object_handlers IntlDatePatternGenerator_handlers;
36 
IntlDatePatternGenerator_object_clone(zend_object * object)37 static zend_object *IntlDatePatternGenerator_object_clone(zend_object *object)
38 {
39 	IntlDatePatternGenerator_object *dtpgo_orig = php_intl_datepatterngenerator_fetch_object(object);
40 	zend_object                        *ret_val = IntlDatePatternGenerator_ce_ptr->create_object(object->ce);
41 	IntlDatePatternGenerator_object  *dtpgo_new = php_intl_datepatterngenerator_fetch_object(ret_val);
42 
43 	zend_objects_clone_members(&dtpgo_new->zo, &dtpgo_orig->zo);
44 
45 	if (dtpgo_orig->dtpg != NULL) {
46 		DateTimePatternGenerator *newDtpg = dtpgo_orig->dtpg->clone();
47 		if (!newDtpg) {
48 			zend_throw_error(NULL, "Failed to clone IntlDatePatternGenerator");
49 		} else {
50 			dtpgo_new->dtpg = newDtpg;
51 		}
52 	} else {
53 		zend_throw_error(NULL, "Cannot clone uninitialized IntlDatePatternGenerator");
54 	}
55 
56 	return ret_val;
57 }
58 
59 /*
60  * Initialize internals of IntlDatePatternGenerator_object not specific to zend standard objects.
61  */
IntlDatePatternGenerator_object_init(IntlDatePatternGenerator_object * co)62 static void IntlDatePatternGenerator_object_init(IntlDatePatternGenerator_object *co)
63 {
64 	intl_error_init(DTPATTERNGEN_ERROR_P(co));
65 	co->dtpg = NULL;
66 }
67 
IntlDatePatternGenerator_object_free(zend_object * object)68 static void IntlDatePatternGenerator_object_free(zend_object *object)
69 {
70 	IntlDatePatternGenerator_object* co = php_intl_datepatterngenerator_fetch_object(object);
71 
72 	if (co->dtpg) {
73 		delete co->dtpg;
74 		co->dtpg = NULL;
75 	}
76 	intl_error_reset(DTPATTERNGEN_ERROR_P(co));
77 
78 	zend_object_std_dtor(&co->zo);
79 }
80 
IntlDatePatternGenerator_object_create(zend_class_entry * ce)81 static zend_object *IntlDatePatternGenerator_object_create(zend_class_entry *ce)
82 {
83 	IntlDatePatternGenerator_object *intern =
84 		(IntlDatePatternGenerator_object*) zend_object_alloc(sizeof(IntlDatePatternGenerator_object), ce);
85 
86 	zend_object_std_init(&intern->zo, ce);
87 	object_properties_init(&intern->zo, ce);
88 	IntlDatePatternGenerator_object_init(intern);
89 
90 	return &intern->zo;
91 }
92 
93 /*
94  * 'IntlDatePatternGenerator' class registration structures & functions
95  */
96 
97 /*
98  * Initialize 'IntlDatePatternGenerator' class
99  */
dateformat_register_IntlDatePatternGenerator_class(void)100 void dateformat_register_IntlDatePatternGenerator_class( void )
101 {
102 	/* Create and register 'IntlDatePatternGenerator' class. */
103 	IntlDatePatternGenerator_ce_ptr = register_class_IntlDatePatternGenerator();
104 	IntlDatePatternGenerator_ce_ptr->create_object = IntlDatePatternGenerator_object_create;
105 	IntlDatePatternGenerator_ce_ptr->default_object_handlers = &IntlDatePatternGenerator_handlers;
106 
107 	memcpy(&IntlDatePatternGenerator_handlers, &std_object_handlers,
108 		sizeof IntlDatePatternGenerator_handlers);
109 	IntlDatePatternGenerator_handlers.offset = XtOffsetOf(IntlDatePatternGenerator_object, zo);
110 	IntlDatePatternGenerator_handlers.clone_obj = IntlDatePatternGenerator_object_clone;
111 	IntlDatePatternGenerator_handlers.free_obj = IntlDatePatternGenerator_object_free;
112 }
113