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: Kirti Velankar <kirtig@yahoo-inc.com>                       |
12    +----------------------------------------------------------------------+
13 */
14 #include <unicode/unum.h>
15 
16 #include "dateformat_class.h"
17 #include "php_intl.h"
18 #include "dateformat_data.h"
19 #include "dateformat.h"
20 #include "dateformat_arginfo.h"
21 
22 #include <zend_exceptions.h>
23 
24 zend_class_entry *IntlDateFormatter_ce_ptr = NULL;
25 static zend_object_handlers IntlDateFormatter_handlers;
26 
27 /*
28  * Auxiliary functions needed by objects of 'IntlDateFormatter' class
29  */
30 
31 /* {{{ IntlDateFormatter_objects_free */
IntlDateFormatter_object_free(zend_object * object)32 void IntlDateFormatter_object_free( zend_object *object )
33 {
34 	IntlDateFormatter_object* dfo = php_intl_dateformatter_fetch_object(object);
35 
36 	zend_object_std_dtor( &dfo->zo );
37 
38 	if (dfo->requested_locale) {
39 		efree( dfo->requested_locale );
40 	}
41 
42 	dateformat_data_free( &dfo->datef_data );
43 }
44 /* }}} */
45 
46 /* {{{ IntlDateFormatter_object_create */
IntlDateFormatter_object_create(zend_class_entry * ce)47 zend_object *IntlDateFormatter_object_create(zend_class_entry *ce)
48 {
49 	IntlDateFormatter_object*     intern;
50 
51 	intern = zend_object_alloc(sizeof(IntlDateFormatter_object), ce);
52 	dateformat_data_init( &intern->datef_data );
53 	zend_object_std_init( &intern->zo, ce );
54 	object_properties_init(&intern->zo, ce);
55 	intern->date_type			= 0;
56 	intern->time_type			= 0;
57 	intern->calendar			= -1;
58 	intern->requested_locale	= NULL;
59 
60 	return &intern->zo;
61 }
62 /* }}} */
63 
64 /* {{{ IntlDateFormatter_object_clone */
IntlDateFormatter_object_clone(zend_object * object)65 zend_object *IntlDateFormatter_object_clone(zend_object *object)
66 {
67 	IntlDateFormatter_object *dfo, *new_dfo;
68 	zend_object *new_obj;
69 
70 	dfo = php_intl_dateformatter_fetch_object(object);
71 	intl_error_reset(INTL_DATA_ERROR_P(dfo));
72 
73 	new_obj = IntlDateFormatter_ce_ptr->create_object(object->ce);
74 	new_dfo = php_intl_dateformatter_fetch_object(new_obj);
75 	/* clone standard parts */
76 	zend_objects_clone_members(&new_dfo->zo, &dfo->zo);
77 	/* clone formatter object */
78 	if (dfo->datef_data.udatf != NULL) {
79 		DATE_FORMAT_OBJECT(new_dfo) = udat_clone(DATE_FORMAT_OBJECT(dfo),  &INTL_DATA_ERROR_CODE(dfo));
80 		if (U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
81 			/* set up error in case error handler is interested */
82 			intl_errors_set(INTL_DATA_ERROR_P(dfo), INTL_DATA_ERROR_CODE(dfo),
83 					"Failed to clone IntlDateFormatter object", 0 );
84 			zend_throw_exception(NULL, "Failed to clone IntlDateFormatter object", 0);
85 		}
86 	} else {
87 		zend_throw_exception(NULL, "Cannot clone unconstructed IntlDateFormatter", 0);
88 	}
89 	return new_obj;
90 }
91 /* }}} */
92 
93 /*
94  * 'IntlDateFormatter' class registration structures & functions
95  */
96 
97 /* {{{ dateformat_register_class
98  * Initialize 'IntlDateFormatter' class
99  */
dateformat_register_IntlDateFormatter_class(void)100 void dateformat_register_IntlDateFormatter_class( void )
101 {
102 	/* Create and register 'IntlDateFormatter' class. */
103 	IntlDateFormatter_ce_ptr = register_class_IntlDateFormatter();
104 	IntlDateFormatter_ce_ptr->create_object = IntlDateFormatter_object_create;
105 	IntlDateFormatter_ce_ptr->default_object_handlers = &IntlDateFormatter_handlers;
106 
107 	memcpy(&IntlDateFormatter_handlers, &std_object_handlers,
108 		sizeof IntlDateFormatter_handlers);
109 	IntlDateFormatter_handlers.offset = XtOffsetOf(IntlDateFormatter_object, zo);
110 	IntlDateFormatter_handlers.clone_obj = IntlDateFormatter_object_clone;
111 	IntlDateFormatter_handlers.free_obj = IntlDateFormatter_object_free;
112 }
113 /* }}} */
114