Skip to content

Commit ae9f6e7

Browse files
authored
Add IntlDatePatternGenerator (#6771)
Add IntlDatePatternGenerator class per RFC https://wiki.php.net/rfc/intldatetimepatterngenerator.
1 parent 81f6d36 commit ae9f6e7

12 files changed

+460
-0
lines changed

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,10 @@ PHP 8.1 UPGRADE NOTES
393393
7. New Classes and Interfaces
394394
========================================
395395

396+
- Intl:
397+
. Added IntlDatePatternGenerator to dynamically generate patterns to use with IntlDateFormatter.
398+
RFC: https://wiki.php.net/rfc/intldatetimepatterngenerator
399+
396400
========================================
397401
8. Removed Extensions and SAPIs
398402
========================================

ext/intl/config.m4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ if test "$PHP_INTL" != "no"; then
6969
dateformat/dateformat_create.cpp \
7070
dateformat/dateformat_attrcpp.cpp \
7171
dateformat/dateformat_helpers.cpp \
72+
dateformat/datepatterngenerator_class.cpp \
73+
dateformat/datepatterngenerator_methods.cpp \
7274
msgformat/msgformat_helpers.cpp \
7375
timezone/timezone_class.cpp \
7476
timezone/timezone_methods.cpp \

ext/intl/config.w32

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ if (PHP_INTL != "no") {
7070
dateformat_attrcpp.cpp \
7171
dateformat_helpers.cpp \
7272
dateformat_create.cpp \
73+
datepatterngenerator_class.cpp \
74+
datepatterngenerator_methods.cpp \
7375
", "intl");
7476
ADD_SOURCES(configure_module_dirname + "/uchar", "\
7577
uchar.c",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
/** @generate-class-entries */
4+
5+
class IntlDatePatternGenerator
6+
{
7+
public function __construct(?string $locale = null) {}
8+
9+
public static function create(?string $locale = null): ?IntlDatePatternGenerator {}
10+
11+
public function getBestPattern(string $skeleton): string|false {}
12+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* This is a generated file, edit the .stub.php file instead.
2+
* Stub hash: 74026c524046787844da9f8132029735243176c6 */
3+
4+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_IntlDatePatternGenerator___construct, 0, 0, 0)
5+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, locale, IS_STRING, 1, "null")
6+
ZEND_END_ARG_INFO()
7+
8+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_IntlDatePatternGenerator_create, 0, 0, IntlDatePatternGenerator, 1)
9+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, locale, IS_STRING, 1, "null")
10+
ZEND_END_ARG_INFO()
11+
12+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_IntlDatePatternGenerator_getBestPattern, 0, 1, MAY_BE_STRING|MAY_BE_FALSE)
13+
ZEND_ARG_TYPE_INFO(0, skeleton, IS_STRING, 0)
14+
ZEND_END_ARG_INFO()
15+
16+
17+
ZEND_METHOD(IntlDatePatternGenerator, __construct);
18+
ZEND_METHOD(IntlDatePatternGenerator, create);
19+
ZEND_METHOD(IntlDatePatternGenerator, getBestPattern);
20+
21+
22+
static const zend_function_entry class_IntlDatePatternGenerator_methods[] = {
23+
ZEND_ME(IntlDatePatternGenerator, __construct, arginfo_class_IntlDatePatternGenerator___construct, ZEND_ACC_PUBLIC)
24+
ZEND_ME(IntlDatePatternGenerator, create, arginfo_class_IntlDatePatternGenerator_create, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
25+
ZEND_ME(IntlDatePatternGenerator, getBestPattern, arginfo_class_IntlDatePatternGenerator_getBestPattern, ZEND_ACC_PUBLIC)
26+
ZEND_FE_END
27+
};
28+
29+
static zend_class_entry *register_class_IntlDatePatternGenerator(void)
30+
{
31+
zend_class_entry ce, *class_entry;
32+
33+
INIT_CLASS_ENTRY(ce, "IntlDatePatternGenerator", class_IntlDatePatternGenerator_methods);
34+
class_entry = zend_register_internal_class_ex(&ce, NULL);
35+
36+
return class_entry;
37+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
| http://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+
| [email protected] so we can mail you a copy immediately. |
10+
+----------------------------------------------------------------------+
11+
| Authors: Mel Dafert ([email protected]) |
12+
+----------------------------------------------------------------------+
13+
*/
14+
15+
#ifdef HAVE_CONFIG_H
16+
#include "config.h"
17+
#endif
18+
19+
#include "../intl_cppshims.h"
20+
21+
#include <unicode/dtptngen.h>
22+
23+
extern "C" {
24+
#define USE_DATETIMEPATTERNGENERATOR_POINTER 1
25+
#include "datepatterngenerator_class.h"
26+
#include "datepatterngenerator_arginfo.h"
27+
#include <zend_exceptions.h>
28+
#include <assert.h>
29+
}
30+
31+
using icu::DateTimePatternGenerator;
32+
using icu::Locale;
33+
34+
zend_class_entry *IntlDatePatternGenerator_ce_ptr;
35+
zend_object_handlers IntlDatePatternGenerator_handlers;
36+
37+
static zend_object *IntlDatePatternGenerator_object_clone(zend_object *object)
38+
{
39+
intl_error_reset(NULL);
40+
41+
IntlDatePatternGenerator_object *dtpgo_orig = php_intl_datepatterngenerator_fetch_object(object);
42+
intl_error_reset(DTPATTERNGEN_ERROR_P(dtpgo_orig));
43+
44+
zend_object *ret_val = IntlDatePatternGenerator_ce_ptr->create_object(object->ce);
45+
IntlDatePatternGenerator_object *dtpgo_new = php_intl_datepatterngenerator_fetch_object(ret_val);
46+
47+
zend_objects_clone_members(&dtpgo_new->zo, &dtpgo_orig->zo);
48+
49+
if (dtpgo_orig->dtpg != NULL) {
50+
DateTimePatternGenerator *newDtpg = dtpgo_orig->dtpg->clone();
51+
if (!newDtpg) {
52+
zend_string *err_msg;
53+
intl_errors_set_code(DTPATTERNGEN_ERROR_P(dtpgo_orig),
54+
U_MEMORY_ALLOCATION_ERROR);
55+
intl_errors_set_custom_msg(DTPATTERNGEN_ERROR_P(dtpgo_orig),
56+
"Could not clone IntlDatePatternGenerator", 0);
57+
err_msg = intl_error_get_message(DTPATTERNGEN_ERROR_P(dtpgo_orig));
58+
zend_throw_exception(NULL, ZSTR_VAL(err_msg), 0);
59+
zend_string_free(err_msg);
60+
} else {
61+
dtpgo_new->dtpg = newDtpg;
62+
}
63+
} else {
64+
zend_throw_exception(NULL, "Cannot clone unconstructed IntlDatePatternGenerator", 0);
65+
}
66+
67+
return ret_val;
68+
}
69+
70+
/*
71+
* Initialize internals of IntlDatePatternGenerator_object not specific to zend standard objects.
72+
*/
73+
static void IntlDatePatternGenerator_object_init(IntlDatePatternGenerator_object *co)
74+
{
75+
intl_error_init(DTPATTERNGEN_ERROR_P(co));
76+
co->dtpg = NULL;
77+
}
78+
79+
static void IntlDatePatternGenerator_object_free(zend_object *object)
80+
{
81+
IntlDatePatternGenerator_object* co = php_intl_datepatterngenerator_fetch_object(object);
82+
83+
if (co->dtpg) {
84+
delete co->dtpg;
85+
co->dtpg = NULL;
86+
}
87+
intl_error_reset(DTPATTERNGEN_ERROR_P(co));
88+
89+
zend_object_std_dtor(&co->zo);
90+
}
91+
92+
static zend_object *IntlDatePatternGenerator_object_create(zend_class_entry *ce)
93+
{
94+
IntlDatePatternGenerator_object *intern =
95+
(IntlDatePatternGenerator_object*) zend_object_alloc(sizeof(IntlDatePatternGenerator_object), ce);
96+
97+
zend_object_std_init(&intern->zo, ce);
98+
object_properties_init(&intern->zo, ce);
99+
IntlDatePatternGenerator_object_init(intern);
100+
101+
intern->zo.handlers = &IntlDatePatternGenerator_handlers;
102+
103+
return &intern->zo;
104+
}
105+
106+
/*
107+
* 'IntlDatePatternGenerator' class registration structures & functions
108+
*/
109+
110+
/*
111+
* Initialize 'IntlDatePatternGenerator' class
112+
*/
113+
void dateformat_register_IntlDatePatternGenerator_class( void )
114+
{
115+
/* Create and register 'IntlDatePatternGenerator' class. */
116+
IntlDatePatternGenerator_ce_ptr = register_class_IntlDatePatternGenerator();
117+
IntlDatePatternGenerator_ce_ptr->create_object = IntlDatePatternGenerator_object_create;
118+
119+
memcpy(&IntlDatePatternGenerator_handlers, &std_object_handlers,
120+
sizeof IntlDatePatternGenerator_handlers);
121+
IntlDatePatternGenerator_handlers.offset = XtOffsetOf(IntlDatePatternGenerator_object, zo);
122+
IntlDatePatternGenerator_handlers.clone_obj = IntlDatePatternGenerator_object_clone;
123+
IntlDatePatternGenerator_handlers.free_obj = IntlDatePatternGenerator_object_free;
124+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
| http://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+
| [email protected] so we can mail you a copy immediately. |
10+
+----------------------------------------------------------------------+
11+
| Authors: Mel Dafert ([email protected]) |
12+
+----------------------------------------------------------------------+
13+
*/
14+
15+
#ifndef DATEPATTERNGENERATOR_CLASS_H
16+
#define DATEPATTERNGENERATOR_CLASS_H
17+
18+
#include <php.h>
19+
#include "intl_common.h"
20+
#include "intl_error.h"
21+
#include "intl_data.h"
22+
23+
#ifndef USE_DATETIMEPATTERNGENERATOR_POINTER
24+
typedef void DateTimePatternGenerator;
25+
#else
26+
using icu::DateTimePatternGenerator;
27+
#endif
28+
29+
typedef struct {
30+
// error handling
31+
intl_error err;
32+
33+
// ICU break iterator
34+
DateTimePatternGenerator *dtpg;
35+
36+
zend_object zo;
37+
} IntlDatePatternGenerator_object;
38+
39+
static inline IntlDatePatternGenerator_object *php_intl_datepatterngenerator_fetch_object(zend_object *obj) {
40+
return (IntlDatePatternGenerator_object *)((char*)(obj) - XtOffsetOf(IntlDatePatternGenerator_object, zo));
41+
}
42+
#define Z_INTL_DATEPATTERNGENERATOR_P(zv) php_intl_datepatterngenerator_fetch_object(Z_OBJ_P(zv))
43+
44+
#define DTPATTERNGEN_ERROR(dtpgo) (dtpgo)->err
45+
#define DTPATTERNGEN_ERROR_P(dtpgo) &(DTPATTERNGEN_ERROR(dtpgo))
46+
47+
#define DTPATTERNGEN_ERROR_CODE(dtpgo) INTL_ERROR_CODE(DTPATTERNGEN_ERROR(dtpgo))
48+
#define DTPATTERNGEN_ERROR_CODE_P(dtpgo) &(INTL_ERROR_CODE(DTPATTERNGEN_ERROR(dtpgo)))
49+
50+
#define DTPATTERNGEN_METHOD_INIT_VARS INTL_METHOD_INIT_VARS(IntlDatePatternGenerator, dtpgo)
51+
#define DTPATTERNGEN_METHOD_FETCH_OBJECT_NO_CHECK INTL_METHOD_FETCH_OBJECT(INTL_DATEPATTERNGENERATOR, dtpgo)
52+
#define DTPATTERNGEN_METHOD_FETCH_OBJECT \
53+
DTPATTERNGEN_METHOD_FETCH_OBJECT_NO_CHECK; \
54+
if (dtpgo->dtpg == NULL) \
55+
{ \
56+
zend_throw_error(NULL, "Found unconstructed IntlDatePatternGenerator"); \
57+
RETURN_THROWS(); \
58+
}
59+
60+
void dateformat_register_IntlDatePatternGenerator_class(void);
61+
62+
extern zend_class_entry *IntlDatePatternGenerator_ce_ptr;
63+
64+
extern zend_object_handlers IntlDatePatternGenerator_handlers;
65+
66+
#endif /* #ifndef DATEPATTERNGENERATOR_CLASS_H */

0 commit comments

Comments
 (0)