xref: /PHP-8.2/ext/intl/msgformat/msgformat_attr.c (revision 01b3fc03)
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 	MSG_FORMAT_METHOD_INIT_VARS;
56 
57 	/* Parse parameters. */
58 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os",
59 		&object, MessageFormatter_ce_ptr, &value, &value_len ) == FAILURE )
60 	{
61 		RETURN_THROWS();
62 	}
63 
64 	MSG_FORMAT_METHOD_FETCH_OBJECT;
65 
66 	/* Convert given pattern to UTF-16. */
67 	intl_convert_utf8_to_utf16(&spattern, &spattern_len, value, value_len, &INTL_DATA_ERROR_CODE(mfo));
68 	INTL_METHOD_CHECK_STATUS(mfo, "Error converting pattern to UTF-16" );
69 
70 #ifdef MSG_FORMAT_QUOTE_APOS
71 	if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
72 		intl_error_set( NULL, U_INVALID_FORMAT_ERROR,
73 			"msgfmt_set_pattern: error converting pattern to quote-friendly format", 0 );
74 		RETURN_FALSE;
75 	}
76 #endif
77 
78 	/* TODO: add parse error information */
79 	umsg_applyPattern(MSG_FORMAT_OBJECT(mfo), spattern, spattern_len, NULL, &INTL_DATA_ERROR_CODE(mfo));
80 	if (spattern) {
81 		efree(spattern);
82 	}
83 	INTL_METHOD_CHECK_STATUS(mfo, "Error setting symbol value");
84 
85 	if(mfo->mf_data.orig_format) {
86 		efree(mfo->mf_data.orig_format);
87 	}
88 	mfo->mf_data.orig_format = estrndup(value, value_len);
89 	mfo->mf_data.orig_format_len = value_len;
90 	/* invalidate cached format types */
91 	if (mfo->mf_data.arg_types) {
92 		zend_hash_destroy(mfo->mf_data.arg_types);
93 		efree(mfo->mf_data.arg_types);
94 		mfo->mf_data.arg_types = NULL;
95 	}
96 
97 	RETURN_TRUE;
98 }
99 /* }}} */
100 
101 /* {{{ Get formatter locale. */
PHP_FUNCTION(msgfmt_get_locale)102 PHP_FUNCTION( msgfmt_get_locale )
103 {
104 	char *loc;
105 	MSG_FORMAT_METHOD_INIT_VARS;
106 
107 	/* Parse parameters. */
108 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
109 		&object, MessageFormatter_ce_ptr ) == FAILURE )
110 	{
111 		RETURN_THROWS();
112 	}
113 
114 	/* Fetch the object. */
115 	MSG_FORMAT_METHOD_FETCH_OBJECT;
116 
117 	loc = (char *)umsg_getLocale(MSG_FORMAT_OBJECT(mfo));
118 	RETURN_STRING(loc);
119 }
120 /* }}} */
121