Skip to content

Commit

Permalink
feat: add StructName on MarshalOptions to custom Class Name
Browse files Browse the repository at this point in the history
  • Loading branch information
lyan-on-jakpat committed Mar 29, 2023
1 parent c610362 commit 155230f
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type MarshalOptions struct {
// If this is true, then all struct names will be stripped from objects
// and "stdClass" will be used instead. The default value is false.
OnlyStdClass bool
StructName string
}

// DefaultMarshalOptions will create a new instance of MarshalOptions with
Expand All @@ -29,12 +30,12 @@ func DefaultMarshalOptions() *MarshalOptions {
// MarshalBool returns the bytes to represent a PHP serialized bool value. This
// would be the equivalent to running:
//
// echo serialize(false);
// // b:0;
// echo serialize(false);
// // b:0;
//
// The same result would be returned by marshalling a boolean value:
//
// Marshal(true)
// Marshal(true)
func MarshalBool(value bool) []byte {
if value {
return []byte("b:1;")
Expand All @@ -46,12 +47,12 @@ func MarshalBool(value bool) []byte {
// MarshalInt returns the bytes to represent a PHP serialized integer value.
// This would be the equivalent to running:
//
// echo serialize(123);
// // i:123;
// echo serialize(123);
// // i:123;
//
// The same result would be returned by marshalling an integer value:
//
// Marshal(123)
// Marshal(123)
func MarshalInt(value int64) []byte {
return []byte("i:" + strconv.FormatInt(value, 10) + ";")
}
Expand All @@ -65,35 +66,35 @@ func MarshalUint(value uint64) []byte {
// MarshalFloat returns the bytes to represent a PHP serialized floating-point
// value. This would be the equivalent to running:
//
// echo serialize(1.23);
// // d:1.23;
// echo serialize(1.23);
// // d:1.23;
//
// The bitSize should represent the size of the float. This makes conversion to
// a string value more accurate, for example:
//
// // float64 is implicit for literals
// MarshalFloat(1.23, 64)
// // float64 is implicit for literals
// MarshalFloat(1.23, 64)
//
// // If the original value was cast from a float32
// f := float32(1.23)
// MarshalFloat(float64(f), 32)
// // If the original value was cast from a float32
// f := float32(1.23)
// MarshalFloat(float64(f), 32)
//
// The same result would be returned by marshalling a floating-point value:
//
// Marshal(1.23)
// Marshal(1.23)
func MarshalFloat(value float64, bitSize int) []byte {
return []byte("d:" + strconv.FormatFloat(value, 'f', -1, bitSize) + ";")
}

// MarshalString returns the bytes to represent a PHP serialized string value.
// This would be the equivalent to running:
//
// echo serialize('Hello world');
// // s:11:"Hello world";
// echo serialize('Hello world');
// // s:11:"Hello world";
//
// The same result would be returned by marshalling a string value:
//
// Marshal('Hello world')
// Marshal('Hello world')
//
// One important distinction is that PHP stores binary data in strings. See
// MarshalBytes for more information.
Expand Down Expand Up @@ -125,8 +126,8 @@ func MarshalBytes(value []byte) []byte {
// MarshalNil returns the bytes to represent a PHP serialized null value.
// This would be the equivalent to running:
//
// echo serialize(null);
// // N;
// echo serialize(null);
// // N;
//
// Unlike the other specific Marshal functions it does not take an argument
// because the output is a constant value.
Expand Down Expand Up @@ -191,6 +192,10 @@ func MarshalStruct(input interface{}, options *MarshalOptions) ([]byte, error) {
className = "stdClass"
}

if options.StructName != "" {
className = options.StructName
}

return []byte(fmt.Sprintf("O:%d:\"%s\":%d:{%s}", len(className),
className, visibleFieldCount, buffer.String())), nil
}
Expand Down

0 comments on commit 155230f

Please sign in to comment.