dateformat_class.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  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: Kirti Velankar <kirtig@yahoo-inc.com> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include <unicode/unum.h>
  17. #include "dateformat_class.h"
  18. #include "php_intl.h"
  19. #include "dateformat_data.h"
  20. #include "dateformat_format.h"
  21. #include "dateformat_format_object.h"
  22. #include "dateformat_parse.h"
  23. #include "dateformat.h"
  24. #include "dateformat_attr.h"
  25. #include "dateformat_attrcpp.h"
  26. #include <zend_exceptions.h>
  27. zend_class_entry *IntlDateFormatter_ce_ptr = NULL;
  28. static zend_object_handlers IntlDateFormatter_handlers;
  29. /*
  30. * Auxiliary functions needed by objects of 'IntlDateFormatter' class
  31. */
  32. /* {{{ IntlDateFormatter_objects_dtor */
  33. static void IntlDateFormatter_object_dtor(void *object, zend_object_handle handle TSRMLS_DC )
  34. {
  35. zend_objects_destroy_object( object, handle TSRMLS_CC );
  36. }
  37. /* }}} */
  38. /* {{{ IntlDateFormatter_objects_free */
  39. void IntlDateFormatter_object_free( zend_object *object TSRMLS_DC )
  40. {
  41. IntlDateFormatter_object* dfo = (IntlDateFormatter_object*)object;
  42. zend_object_std_dtor( &dfo->zo TSRMLS_CC );
  43. if (dfo->requested_locale) {
  44. efree( dfo->requested_locale );
  45. }
  46. dateformat_data_free( &dfo->datef_data TSRMLS_CC );
  47. efree( dfo );
  48. }
  49. /* }}} */
  50. /* {{{ IntlDateFormatter_object_create */
  51. zend_object_value IntlDateFormatter_object_create(zend_class_entry *ce TSRMLS_DC)
  52. {
  53. zend_object_value retval;
  54. IntlDateFormatter_object* intern;
  55. intern = ecalloc( 1, sizeof(IntlDateFormatter_object) );
  56. dateformat_data_init( &intern->datef_data TSRMLS_CC );
  57. zend_object_std_init( &intern->zo, ce TSRMLS_CC );
  58. object_properties_init(&intern->zo, ce);
  59. intern->date_type = 0;
  60. intern->time_type = 0;
  61. intern->calendar = -1;
  62. intern->requested_locale = NULL;
  63. retval.handle = zend_objects_store_put(
  64. intern,
  65. IntlDateFormatter_object_dtor,
  66. (zend_objects_free_object_storage_t)IntlDateFormatter_object_free,
  67. NULL TSRMLS_CC );
  68. retval.handlers = &IntlDateFormatter_handlers;
  69. return retval;
  70. }
  71. /* }}} */
  72. /* {{{ IntlDateFormatter_object_clone */
  73. zend_object_value IntlDateFormatter_object_clone(zval *object TSRMLS_DC)
  74. {
  75. zend_object_value new_obj_val;
  76. zend_object_handle handle = Z_OBJ_HANDLE_P(object);
  77. IntlDateFormatter_object *dfo, *new_dfo;
  78. DATE_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK;
  79. new_obj_val = IntlDateFormatter_ce_ptr->create_object(Z_OBJCE_P(object) TSRMLS_CC);
  80. new_dfo = (IntlDateFormatter_object *)zend_object_store_get_object_by_handle(new_obj_val.handle TSRMLS_CC);
  81. /* clone standard parts */
  82. zend_objects_clone_members(&new_dfo->zo, new_obj_val, &dfo->zo, handle TSRMLS_CC);
  83. /* clone formatter object */
  84. if (dfo->datef_data.udatf != NULL) {
  85. DATE_FORMAT_OBJECT(new_dfo) = udat_clone(DATE_FORMAT_OBJECT(dfo), &INTL_DATA_ERROR_CODE(dfo));
  86. if (U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
  87. /* set up error in case error handler is interested */
  88. intl_errors_set(INTL_DATA_ERROR_P(dfo), INTL_DATA_ERROR_CODE(dfo),
  89. "Failed to clone IntlDateFormatter object", 0 TSRMLS_CC );
  90. zend_throw_exception(NULL, "Failed to clone IntlDateFormatter object", 0 TSRMLS_CC);
  91. }
  92. } else {
  93. zend_throw_exception(NULL, "Cannot clone unconstructed IntlDateFormatter", 0 TSRMLS_CC);
  94. }
  95. return new_obj_val;
  96. }
  97. /* }}} */
  98. /*
  99. * 'IntlDateFormatter' class registration structures & functions
  100. */
  101. /* {{{ arginfo */
  102. ZEND_BEGIN_ARG_INFO_EX(datefmt_parse_args, 0, 0, 1)
  103. ZEND_ARG_INFO(0, string)
  104. ZEND_ARG_INFO(1, position)
  105. ZEND_END_ARG_INFO()
  106. ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter_format, 0, 0, 0)
  107. ZEND_ARG_INFO(0, args)
  108. ZEND_ARG_INFO(0, array)
  109. ZEND_END_ARG_INFO()
  110. ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter_format_object, 0, 0, 1)
  111. ZEND_ARG_INFO(0, object)
  112. ZEND_ARG_INFO(0, format)
  113. ZEND_ARG_INFO(0, locale)
  114. ZEND_END_ARG_INFO()
  115. ZEND_BEGIN_ARG_INFO(arginfo_intldateformatter_getdatetype, 0)
  116. ZEND_END_ARG_INFO()
  117. ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter_settimezoneid, 0, 0, 1)
  118. ZEND_ARG_INFO(0, zone)
  119. ZEND_END_ARG_INFO()
  120. ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter_setpattern, 0, 0, 1)
  121. ZEND_ARG_INFO(0, pattern)
  122. ZEND_END_ARG_INFO()
  123. ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter_setlenient, 0, 0, 1)
  124. ZEND_ARG_INFO(0, lenient)
  125. ZEND_END_ARG_INFO()
  126. ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter_setcalendar, 0, 0, 1)
  127. ZEND_ARG_INFO(0, which)
  128. ZEND_END_ARG_INFO()
  129. ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter___construct, 0, 0, 3)
  130. ZEND_ARG_INFO(0, locale)
  131. ZEND_ARG_INFO(0, datetype)
  132. ZEND_ARG_INFO(0, timetype)
  133. ZEND_ARG_INFO(0, timezone)
  134. ZEND_ARG_INFO(0, calendar)
  135. ZEND_ARG_INFO(0, pattern)
  136. ZEND_END_ARG_INFO()
  137. /* }}} */
  138. /* {{{ IntlDateFormatter_class_functions
  139. * Every 'IntlDateFormatter' class method has an entry in this table
  140. */
  141. static zend_function_entry IntlDateFormatter_class_functions[] = {
  142. PHP_ME( IntlDateFormatter, __construct, arginfo_intldateformatter___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR )
  143. ZEND_FENTRY( create, ZEND_FN( datefmt_create ), arginfo_intldateformatter___construct, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
  144. PHP_NAMED_FE( getDateType, ZEND_FN( datefmt_get_datetype ), arginfo_intldateformatter_getdatetype )
  145. PHP_NAMED_FE( getTimeType, ZEND_FN( datefmt_get_timetype ), arginfo_intldateformatter_getdatetype )
  146. PHP_NAMED_FE( getCalendar, ZEND_FN( datefmt_get_calendar ), arginfo_intldateformatter_getdatetype )
  147. PHP_NAMED_FE( getCalendarObject, ZEND_FN( datefmt_get_calendar_object ), arginfo_intldateformatter_getdatetype )
  148. PHP_NAMED_FE( setCalendar, ZEND_FN( datefmt_set_calendar ), arginfo_intldateformatter_setcalendar )
  149. PHP_NAMED_FE( getTimeZoneId, ZEND_FN( datefmt_get_timezone_id ), arginfo_intldateformatter_getdatetype )
  150. PHP_NAMED_FE( setTimeZoneId, ZEND_FN( datefmt_set_timezone_id ), arginfo_intldateformatter_settimezoneid )
  151. PHP_NAMED_FE( getTimeZone, ZEND_FN( datefmt_get_timezone ), arginfo_intldateformatter_getdatetype )
  152. PHP_NAMED_FE( setTimeZone, ZEND_FN( datefmt_set_timezone ), arginfo_intldateformatter_settimezoneid )
  153. PHP_NAMED_FE( setPattern, ZEND_FN( datefmt_set_pattern ), arginfo_intldateformatter_setpattern )
  154. PHP_NAMED_FE( getPattern, ZEND_FN( datefmt_get_pattern ), arginfo_intldateformatter_getdatetype )
  155. PHP_NAMED_FE( getLocale, ZEND_FN( datefmt_get_locale ), arginfo_intldateformatter_getdatetype )
  156. PHP_NAMED_FE( setLenient, ZEND_FN( datefmt_set_lenient ), arginfo_intldateformatter_setlenient )
  157. PHP_NAMED_FE( isLenient, ZEND_FN( datefmt_is_lenient ), arginfo_intldateformatter_getdatetype )
  158. PHP_NAMED_FE( format, ZEND_FN( datefmt_format ), arginfo_intldateformatter_format )
  159. PHP_ME_MAPPING( formatObject, datefmt_format_object, arginfo_intldateformatter_format_object, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  160. PHP_NAMED_FE( parse, ZEND_FN( datefmt_parse), datefmt_parse_args )
  161. PHP_NAMED_FE( localtime, ZEND_FN( datefmt_localtime ), datefmt_parse_args )
  162. PHP_NAMED_FE( getErrorCode, ZEND_FN( datefmt_get_error_code ), arginfo_intldateformatter_getdatetype )
  163. PHP_NAMED_FE( getErrorMessage, ZEND_FN( datefmt_get_error_message ), arginfo_intldateformatter_getdatetype )
  164. PHP_FE_END
  165. };
  166. /* }}} */
  167. /* {{{ dateformat_register_class
  168. * Initialize 'IntlDateFormatter' class
  169. */
  170. void dateformat_register_IntlDateFormatter_class( TSRMLS_D )
  171. {
  172. zend_class_entry ce;
  173. /* Create and register 'IntlDateFormatter' class. */
  174. INIT_CLASS_ENTRY( ce, "IntlDateFormatter", IntlDateFormatter_class_functions );
  175. ce.create_object = IntlDateFormatter_object_create;
  176. IntlDateFormatter_ce_ptr = zend_register_internal_class( &ce TSRMLS_CC );
  177. memcpy(&IntlDateFormatter_handlers, zend_get_std_object_handlers(),
  178. sizeof IntlDateFormatter_handlers);
  179. IntlDateFormatter_handlers.clone_obj = IntlDateFormatter_object_clone;
  180. /* Declare 'IntlDateFormatter' class properties. */
  181. if( !IntlDateFormatter_ce_ptr )
  182. {
  183. zend_error(E_ERROR, "Failed to register IntlDateFormatter class");
  184. return;
  185. }
  186. }
  187. /* }}} */