xref: /php-src/ext/intl/msgformat/msgformat_attr.c (revision 11accb5c)
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: Stanislav Malyshev <stas@zend.com>                          |
12    +----------------------------------------------------------------------+
13  */
14 
15 #ifdef HAVE_CONFIG_H
16 #include <config.h>
17 #endif
18 
19 #include "php_intl.h"
20 #include "msgformat_class.h"
21 #include "msgformat_data.h"
22 #include "intl_convert.h"
23 
24 #include <unicode/ustring.h>
25 
26 /* {{{ Get formatter pattern. */
PHP_FUNCTION(msgfmt_get_pattern)27 PHP_FUNCTION( msgfmt_get_pattern )
28 {
29 	MSG_FORMAT_METHOD_INIT_VARS;
30 
31 	/* Parse parameters. */
32 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O", &object, MessageFormatter_ce_ptr ) == FAILURE )
33 	{
34 		RETURN_THROWS();
35 	}
36 
37 	/* Fetch the object. */
38 	MSG_FORMAT_METHOD_FETCH_OBJECT;
39 
40 	if(mfo->mf_data.orig_format) {
41 		RETURN_STRINGL(mfo->mf_data.orig_format, mfo->mf_data.orig_format_len);
42 	}
43 
44 	RETURN_FALSE;
45 }
46 /* }}} */
47 
48 /* {{{ Set formatter pattern. */
PHP_FUNCTION(msgfmt_set_pattern)49 PHP_FUNCTION( msgfmt_set_pattern )
50 {
51 	char*       value = NULL;
52 	size_t      value_len = 0;
53 	int32_t     spattern_len = 0;
54 	UChar*	    spattern  = NULL;
55 	UParseError spattern_error = {0};
56 	MSG_FORMAT_METHOD_INIT_VARS;
57 
58 	/* Parse parameters. */
59 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os",
60 		&object, MessageFormatter_ce_ptr, &value, &value_len ) == FAILURE )
61 	{
62 		RETURN_THROWS();
63 	}
64 
65 	MSG_FORMAT_METHOD_FETCH_OBJECT;
66 
67 	/* Convert given pattern to UTF-16. */
68 	intl_convert_utf8_to_utf16(&spattern, &spattern_len, value, value_len, &INTL_DATA_ERROR_CODE(mfo));
69 	INTL_METHOD_CHECK_STATUS(mfo, "Error converting pattern to UTF-16" );
70 
71 #ifdef MSG_FORMAT_QUOTE_APOS
72 	if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
73 		intl_error_set( NULL, U_INVALID_FORMAT_ERROR,
74 			"msgfmt_set_pattern: error converting pattern to quote-friendly format", 0 );
75 		RETURN_FALSE;
76 	}
77 #endif
78 
79 	umsg_applyPattern(MSG_FORMAT_OBJECT(mfo), spattern, spattern_len, &spattern_error, &INTL_DATA_ERROR_CODE(mfo));
80 	if (spattern) {
81 		efree(spattern);
82 	}
83 	if (U_FAILURE(INTL_DATA_ERROR_CODE(mfo))) {
84 		char *msg;
85 		spprintf(&msg, 0, "Error setting symbol value at line %d, offset %d", spattern_error.line, spattern_error.offset);
86 		intl_errors_set_custom_msg(INTL_DATA_ERROR_P(mfo), msg, 1);
87 		efree(msg);
88 		RETURN_FALSE;
89 	}
90 
91 	if(mfo->mf_data.orig_format) {
92 		efree(mfo->mf_data.orig_format);
93 	}
94 	mfo->mf_data.orig_format = estrndup(value, value_len);
95 	mfo->mf_data.orig_format_len = value_len;
96 	/* invalidate cached format types */
97 	if (mfo->mf_data.arg_types) {
98 		zend_hash_destroy(mfo->mf_data.arg_types);
99 		efree(mfo->mf_data.arg_types);
100 		mfo->mf_data.arg_types = NULL;
101 	}
102 
103 	RETURN_TRUE;
104 }
105 /* }}} */
106 
107 /* {{{ Get formatter locale. */
PHP_FUNCTION(msgfmt_get_locale)108 PHP_FUNCTION( msgfmt_get_locale )
109 {
110 	char *loc;
111 	MSG_FORMAT_METHOD_INIT_VARS;
112 
113 	/* Parse parameters. */
114 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
115 		&object, MessageFormatter_ce_ptr ) == FAILURE )
116 	{
117 		RETURN_THROWS();
118 	}
119 
120 	/* Fetch the object. */
121 	MSG_FORMAT_METHOD_FETCH_OBJECT;
122 
123 	loc = (char *)umsg_getLocale(MSG_FORMAT_OBJECT(mfo));
124 	RETURN_STRING(loc);
125 }
126 /* }}} */
127