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: Gustavo Lopes <cataphract@php.net> |
14 +----------------------------------------------------------------------+
15 */
16
17 #include "../intl_cppshims.h"
18
19 #include <unicode/calendar.h>
20 #include <unicode/gregocal.h>
21
22 #include "dateformat_helpers.h"
23
24 extern "C" {
25 #include "../php_intl.h"
26 #include <Zend/zend_operators.h>
27 #define USE_CALENDAR_POINTER 1
28 #include "../calendar/calendar_class.h"
29 }
30
31 using icu::GregorianCalendar;
32
datefmt_process_calendar_arg(zval * calendar_zv,Locale const & locale,const char * func_name,intl_error * err,Calendar * & cal,zend_long & cal_int_type,bool & calendar_owned)33 int datefmt_process_calendar_arg(zval* calendar_zv,
34 Locale const& locale,
35 const char *func_name,
36 intl_error *err,
37 Calendar*& cal,
38 zend_long& cal_int_type,
39 bool& calendar_owned)
40 {
41 char *msg;
42 UErrorCode status = UErrorCode();
43
44 if (calendar_zv == NULL || Z_TYPE_P(calendar_zv) == IS_NULL) {
45
46 // default requested
47 cal = new GregorianCalendar(locale, status);
48 calendar_owned = true;
49
50 cal_int_type = UCAL_GREGORIAN;
51
52 } else if (Z_TYPE_P(calendar_zv) == IS_LONG) {
53
54 zend_long v = Z_LVAL_P(calendar_zv);
55 if (v != (zend_long)UCAL_TRADITIONAL && v != (zend_long)UCAL_GREGORIAN) {
56 spprintf(&msg, 0, "%s: invalid value for calendar type; it must be "
57 "one of IntlDateFormatter::TRADITIONAL (locale's default "
58 "calendar) or IntlDateFormatter::GREGORIAN. "
59 "Alternatively, it can be an IntlCalendar object",
60 func_name);
61 intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR, msg, 1);
62 efree(msg);
63 return FAILURE;
64 } else if (v == (zend_long)UCAL_TRADITIONAL) {
65 cal = Calendar::createInstance(locale, status);
66 } else { //UCAL_GREGORIAN
67 cal = new GregorianCalendar(locale, status);
68 }
69 calendar_owned = true;
70
71 cal_int_type = Z_LVAL_P(calendar_zv);
72
73 } else if (Z_TYPE_P(calendar_zv) == IS_OBJECT &&
74 instanceof_function_ex(Z_OBJCE_P(calendar_zv),
75 Calendar_ce_ptr, 0)) {
76
77 cal = calendar_fetch_native_calendar(calendar_zv);
78 if (cal == NULL) {
79 spprintf(&msg, 0, "%s: Found unconstructed IntlCalendar object",
80 func_name);
81 intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR, msg, 1);
82 efree(msg);
83 return FAILURE;
84 }
85 calendar_owned = false;
86
87 cal_int_type = -1;
88
89 } else {
90 spprintf(&msg, 0, "%s: Invalid calendar argument; should be an integer "
91 "or an IntlCalendar instance", func_name);
92 intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR, msg, 1);
93 efree(msg);
94 return FAILURE;
95 }
96
97 if (cal == NULL && !U_FAILURE(status)) {
98 status = U_MEMORY_ALLOCATION_ERROR;
99 }
100 if (U_FAILURE(status)) {
101 spprintf(&msg, 0, "%s: Failure instantiating calendar", func_name);
102 intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR, msg, 1);
103 efree(msg);
104 return FAILURE;
105 }
106
107 return SUCCESS;
108 }
109