add ion\LOB
authorMichael Wallner <mike@php.net>
Fri, 10 Dec 2021 22:02:36 +0000 (23:02 +0100)
committerMichael Wallner <mike@php.net>
Tue, 14 Dec 2021 11:54:08 +0000 (12:54 +0100)
ion.c
ion.stub.php
ion_arginfo.h
ion_private.h

diff --git a/ion.c b/ion.c
index 638ce603d74b07173a2354a4eaa72bc1eed5fd49..5ec89a1627309c368df47814c4ba71c8465ae397 100644 (file)
--- a/ion.c
+++ b/ion.c
@@ -225,6 +225,22 @@ ZEND_METHOD(ion_Decimal, isInt)
 
        RETURN_BOOL(ion_decimal_is_integer(&obj->dec));
 }
+ZEND_METHOD(ion_LOB, __construct)
+{
+       zend_string *value;
+       zend_object *type = NULL;
+       ZEND_PARSE_PARAMETERS_START(1, 2)
+               Z_PARAM_STR(value)
+               Z_PARAM_OPTIONAL
+               Z_PARAM_OBJ_OF_CLASS(type, ce_Type)
+       ZEND_PARSE_PARAMETERS_END();
+
+       if (!type) {
+               type = zend_enum_get_case_cstr(ce_Type, "CLob");
+       }
+       update_property_obj(Z_OBJ_P(ZEND_THIS), ZEND_STRL("type"), type);
+       zend_update_property_str(Z_OBJCE_P(ZEND_THIS), Z_OBJ_P(ZEND_THIS), ZEND_STRL("value"), value);
+}
 ZEND_METHOD(ion_Reader_Options, __construct)
 {
        php_ion_reader_options *opt = php_ion_obj(reader_options, Z_OBJ_P(ZEND_THIS));
@@ -1442,6 +1458,7 @@ PHP_MINIT_FUNCTION(ion)
        ce_Symbol_System_SID = register_class_ion_Symbol_System_SID();
 
        ce_Collection = register_class_ion_Collection();
+       ce_LOB = register_class_ion_LOB();
 
        php_ion_register(decimal, Decimal);
        php_ion_register(decimal_ctx, Decimal_Context);
index 7e27334ce1e9a7ea39fc0b857eaa61605d95175c..dcf0cfa16e20c6ac631b7de5ddc4e84458a8db8f 100644 (file)
@@ -101,6 +101,14 @@ class Collection {
 
 }
 
+namespace ion;
+class LOB {
+    public function __construct(
+        public readonly string $value,
+        public readonly Type $type = Type::CLob,
+    ) {
+    }
+}
 
 namespace ion\Decimal;
 class Context {
index 7c1369d7ce24538b5f777fa939f3e00760fc6467..da22a4b45d7c6f9e0686752930669fa0fa7fc9d2 100644 (file)
@@ -1,5 +1,5 @@
 /* This is a generated file, edit the .stub.php file instead.
- * Stub hash: 13de1401ca779d900a35c7be3ac484c8d4dde44d */
+ * Stub hash: f2b34e7e90a3fcb65ad470c6acce7cc31b804716 */
 
 ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ion_serialize, 0, 1, IS_STRING, 0)
        ZEND_ARG_TYPE_INFO(0, data, IS_MIXED, 0)
@@ -31,6 +31,11 @@ ZEND_END_ARG_INFO()
 
 #define arginfo_class_ion_Symbol_toString arginfo_class_ion_Symbol___toString
 
+ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ion_LOB___construct, 0, 0, 1)
+       ZEND_ARG_TYPE_INFO(0, value, IS_STRING, 0)
+       ZEND_ARG_OBJ_INFO_WITH_DEFAULT_VALUE(0, type, ion\\Type, 0, "ion\\Type::CLob")
+ZEND_END_ARG_INFO()
+
 ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ion_Decimal_Context___construct, 0, 0, 0)
        ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, bits, IS_LONG, 0, "128")
 ZEND_END_ARG_INFO()
@@ -448,6 +453,7 @@ ZEND_METHOD(ion_Symbol_ImportLocation, __construct);
 ZEND_METHOD(ion_Symbol, __construct);
 ZEND_METHOD(ion_Symbol, equals);
 ZEND_METHOD(ion_Symbol, __toString);
+ZEND_METHOD(ion_LOB, __construct);
 ZEND_METHOD(ion_Decimal_Context, __construct);
 ZEND_METHOD(ion_Decimal, __construct);
 ZEND_METHOD(ion_Decimal, equals);
@@ -591,6 +597,12 @@ static const zend_function_entry class_ion_Collection_methods[] = {
 };
 
 
+static const zend_function_entry class_ion_LOB_methods[] = {
+       ZEND_ME(ion_LOB, __construct, arginfo_class_ion_LOB___construct, ZEND_ACC_PUBLIC)
+       ZEND_FE_END
+};
+
+
 static const zend_function_entry class_ion_Decimal_Context_methods[] = {
        ZEND_ME(ion_Decimal_Context, __construct, arginfo_class_ion_Decimal_Context___construct, ZEND_ACC_PUBLIC)
        ZEND_FE_END
@@ -1148,6 +1160,29 @@ static zend_class_entry *register_class_ion_Collection(void)
        return class_entry;
 }
 
+static zend_class_entry *register_class_ion_LOB(void)
+{
+       zend_class_entry ce, *class_entry;
+
+       INIT_NS_CLASS_ENTRY(ce, "ion", "LOB", class_ion_LOB_methods);
+       class_entry = zend_register_internal_class_ex(&ce, NULL);
+
+       zval property_value_default_value;
+       ZVAL_UNDEF(&property_value_default_value);
+       zend_string *property_value_name = zend_string_init("value", sizeof("value") - 1, 1);
+       zend_declare_typed_property(class_entry, property_value_name, &property_value_default_value, ZEND_ACC_PUBLIC|ZEND_ACC_READONLY, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING));
+       zend_string_release(property_value_name);
+
+       zend_string *property_type_class_ion_Type = zend_string_init("ion\\Type", sizeof("ion\\Type")-1, 1);
+       zval property_type_default_value;
+       ZVAL_UNDEF(&property_type_default_value);
+       zend_string *property_type_name = zend_string_init("type", sizeof("type") - 1, 1);
+       zend_declare_typed_property(class_entry, property_type_name, &property_type_default_value, ZEND_ACC_PUBLIC|ZEND_ACC_READONLY, NULL, (zend_type) ZEND_TYPE_INIT_CLASS(property_type_class_ion_Type, 0, 0));
+       zend_string_release(property_type_name);
+
+       return class_entry;
+}
+
 static zend_class_entry *register_class_ion_Decimal_Context(void)
 {
        zend_class_entry ce, *class_entry;
index 787ba18d969c19361e031179225dd6eaac6d4224..56da560b39b19b374c40364c90ce22c4853bb369 100644 (file)
@@ -170,6 +170,7 @@ static zend_class_entry
        *ce_Collection,
        *ce_Decimal,
        *ce_Decimal_Context,
+       *ce_LOB,
        *ce_Reader,
        *ce_Reader_Options,
        *ce_Reader_Reader,
@@ -1050,6 +1051,24 @@ static inline void php_ion_serialize_object_std(php_ion_serializer *ser, zend_ob
        }
 }
 
+static inline void php_ion_serialize_object_lob(php_ion_serializer *ser, zend_object *zobject)
+{
+       zval tmp_type, *type = zend_read_property(NULL, zobject, ZEND_STRL("type"), 0, &tmp_type);
+       zval tmp_value, *value = zend_read_property(NULL, zobject, ZEND_STRL("value"), 0, &tmp_value);
+       switch (Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(type)))) {
+       case tid_BLOB_INT:
+               ION_CHECK(ion_writer_write_blob(ser->writer, (BYTE *) Z_STRVAL_P(value), Z_STRLEN_P(value)));
+               break;
+       case tid_CLOB_INT:
+               ION_CHECK(ion_writer_write_clob(ser->writer, (BYTE *) Z_STRVAL_P(value), Z_STRLEN_P(value)));
+               break;
+       default:
+               zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG,
+                               "Unsupported LOB type: ion\\Type::%s", Z_STRVAL_P(zend_enum_fetch_case_name(Z_OBJ_P(type))));
+               break;
+       }
+}
+
 static inline bool can_call_magic_serialize(php_ion_serializer *ser, zend_class_entry *ce)
 {
        return ce->__serialize && ser->call_magic;
@@ -1097,6 +1116,8 @@ static inline void php_ion_serialize_object(php_ion_serializer *ser, zend_object
                php_ion_timestamp *pts = php_ion_obj(timestamp, zobject);
                decContext *ctx = ser->options ? ser->options->decimal_context : NULL;
                ION_CHECK(ion_writer_write_timestamp(ser->writer, ion_timestamp_from_php(&its, pts, ctx)));
+       } else if (ce == ce_LOB) {
+               php_ion_serialize_object_lob(ser, zobject);
        } else {
                php_ion_serialize_object_std(ser, zobject);
        }
@@ -1524,6 +1545,7 @@ again:
        }
        ION_CHECK(err, zend_string_release(zstr));
        if (zstr->len > read) {
+               zstr->val[read] = 0;
                zstr = zend_string_truncate(zstr, read, 0);
        }
        RETURN_STR(zstr);