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: Stanislav Malyshev <stas@zend.com> |
14 : +----------------------------------------------------------------------+
15 : */
16 :
17 : #ifdef HAVE_CONFIG_H
18 : #include "config.h"
19 : #endif
20 :
21 : #include <unicode/ustring.h>
22 :
23 : #include "php_intl.h"
24 : #include "formatter_class.h"
25 : #include "intl_convert.h"
26 :
27 : /* {{{ */
28 : static void numfmt_ctor(INTERNAL_FUNCTION_PARAMETERS)
29 201 : {
30 : char* locale;
31 201 : char* pattern = NULL;
32 201 : int locale_len = 0, pattern_len = 0;
33 : long style;
34 201 : UChar* spattern = NULL;
35 201 : int spattern_len = 0;
36 201 : FORMATTER_METHOD_INIT_VARS;
37 :
38 : /* Parse parameters. */
39 201 : if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "sl|s",
40 : &locale, &locale_len, &style, &pattern, &pattern_len ) == FAILURE )
41 : {
42 6 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
43 : "numfmt_create: unable to parse input parameters", 0 TSRMLS_CC );
44 6 : zval_dtor(return_value);
45 6 : RETURN_NULL();
46 : }
47 :
48 195 : INTL_CHECK_LOCALE_LEN_OBJ(locale_len, return_value);
49 195 : object = return_value;
50 195 : FORMATTER_METHOD_FETCH_OBJECT;
51 :
52 : /* Convert pattern (if specified) to UTF-16. */
53 195 : if(pattern && pattern_len) {
54 40 : intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(nfo));
55 40 : INTL_CTOR_CHECK_STATUS(nfo, "numfmt_create: error converting pattern to UTF-16");
56 : }
57 :
58 195 : if(locale_len == 0) {
59 3 : locale = INTL_G(default_locale);
60 : }
61 :
62 : /* Create an ICU number formatter. */
63 195 : FORMATTER_OBJECT(nfo) = unum_open(style, spattern, spattern_len, locale, NULL, &INTL_DATA_ERROR_CODE(nfo));
64 :
65 195 : if(spattern) {
66 40 : efree(spattern);
67 : }
68 :
69 195 : INTL_CTOR_CHECK_STATUS(nfo, "numfmt_create: number formatter creation failed");
70 : }
71 : /* }}} */
72 :
73 : /* {{{ proto NumberFormatter NumberFormatter::create( string $locale, int style[, string $pattern ] )
74 : * Create number formatter. }}} */
75 : /* {{{ proto NumberFormatter numfmt_create( string $locale, int style[, string $pattern ] )
76 : * Create number formatter.
77 : */
78 : PHP_FUNCTION( numfmt_create )
79 101 : {
80 101 : object_init_ex( return_value, NumberFormatter_ce_ptr );
81 101 : numfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
82 101 : }
83 : /* }}} */
84 :
85 : /* {{{ proto void NumberFormatter::__construct( string $locale, int style[, string $pattern ] )
86 : * NumberFormatter object constructor.
87 : */
88 : PHP_METHOD( NumberFormatter, __construct )
89 100 : {
90 100 : return_value = getThis();
91 100 : numfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
92 100 : }
93 : /* }}} */
94 :
95 : /* {{{ proto int NumberFormatter::getErrorCode()
96 : * Get formatter's last error code. }}} */
97 : /* {{{ proto int numfmt_get_error_code( NumberFormatter $nf )
98 : * Get formatter's last error code.
99 : */
100 : PHP_FUNCTION( numfmt_get_error_code )
101 2 : {
102 2 : FORMATTER_METHOD_INIT_VARS
103 :
104 : /* Parse parameters. */
105 2 : if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
106 : &object, NumberFormatter_ce_ptr ) == FAILURE )
107 : {
108 0 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
109 : "numfmt_get_error_code: unable to parse input params", 0 TSRMLS_CC );
110 :
111 0 : RETURN_FALSE;
112 : }
113 :
114 2 : nfo = (NumberFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
115 :
116 : /* Return formatter's last error code. */
117 2 : RETURN_LONG( INTL_DATA_ERROR_CODE(nfo) );
118 : }
119 : /* }}} */
120 :
121 : /* {{{ proto string NumberFormatter::getErrorMessage( )
122 : * Get text description for formatter's last error code. }}} */
123 : /* {{{ proto string numfmt_get_error_message( NumberFormatter $nf )
124 : * Get text description for formatter's last error code.
125 : */
126 : PHP_FUNCTION( numfmt_get_error_message )
127 2 : {
128 2 : char* message = NULL;
129 2 : FORMATTER_METHOD_INIT_VARS
130 :
131 : /* Parse parameters. */
132 2 : if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
133 : &object, NumberFormatter_ce_ptr ) == FAILURE )
134 : {
135 0 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
136 : "numfmt_get_error_message: unable to parse input params", 0 TSRMLS_CC );
137 :
138 0 : RETURN_FALSE;
139 : }
140 :
141 2 : nfo = (NumberFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
142 :
143 : /* Return last error message. */
144 2 : message = intl_error_get_message( &INTL_DATA_ERROR(nfo) TSRMLS_CC );
145 2 : RETURN_STRING( message, 0);
146 : }
147 : /* }}} */
148 :
149 : /*
150 : * Local variables:
151 : * tab-width: 4
152 : * c-basic-offset: 4
153 : * End:
154 : * vim600: noet sw=4 ts=4 fdm=marker
155 : * vim<600: noet sw=4 ts=4
156 : */
|