collator_create.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include "php_intl.h"
  21. #include "collator_class.h"
  22. #include "collator_create.h"
  23. #include "intl_data.h"
  24. /* {{{ */
  25. static int collator_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_constructor)
  26. {
  27. const char* locale;
  28. size_t locale_len = 0;
  29. zval* object;
  30. Collator_object* co;
  31. int zpp_flags = is_constructor ? ZEND_PARSE_PARAMS_THROW : 0;
  32. intl_error_reset( NULL );
  33. object = return_value;
  34. /* Parse parameters. */
  35. if( zend_parse_parameters_ex( zpp_flags, ZEND_NUM_ARGS(), "s",
  36. &locale, &locale_len ) == FAILURE )
  37. {
  38. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  39. "collator_create: unable to parse input params", 0 );
  40. return FAILURE;
  41. }
  42. INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len);
  43. COLLATOR_METHOD_FETCH_OBJECT;
  44. if(locale_len == 0) {
  45. locale = intl_locale_get_default();
  46. }
  47. /* Open ICU collator. */
  48. co->ucoll = ucol_open( locale, COLLATOR_ERROR_CODE_P( co ) );
  49. INTL_CTOR_CHECK_STATUS(co, "collator_create: unable to open ICU collator");
  50. return SUCCESS;
  51. }
  52. /* }}} */
  53. /* {{{ proto Collator collator_create( string $locale )
  54. * Create collator.
  55. */
  56. PHP_FUNCTION( collator_create )
  57. {
  58. object_init_ex( return_value, Collator_ce_ptr );
  59. if (collator_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0) == FAILURE) {
  60. zval_ptr_dtor(return_value);
  61. RETURN_NULL();
  62. }
  63. }
  64. /* }}} */
  65. /* {{{ proto Collator::__construct( string $locale )
  66. * Collator object constructor.
  67. */
  68. PHP_METHOD( Collator, __construct )
  69. {
  70. zend_error_handling error_handling;
  71. zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, &error_handling);
  72. return_value = getThis();
  73. if (collator_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1) == FAILURE) {
  74. if (!EG(exception)) {
  75. zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);
  76. }
  77. }
  78. zend_restore_error_handling(&error_handling);
  79. }
  80. /* }}} */
  81. /*
  82. * Local variables:
  83. * tab-width: 4
  84. * c-basic-offset: 4
  85. * End:
  86. * vim600: noet sw=4 ts=4 fdm=marker
  87. * vim<600: noet sw=4 ts=4
  88. */