forked from yvasiyarov/php_session_decoder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencoder.go
47 lines (39 loc) · 924 Bytes
/
encoder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package php_session_decoder
import (
"bytes"
"fmt"
"github.com/yvasiyarov/php_session_decoder/php_serialize"
)
type PhpEncoder struct {
data PhpSession
encoder *php_serialize.Serializer
}
func NewPhpEncoder(data PhpSession) *PhpEncoder {
return &PhpEncoder{
data: data,
encoder: php_serialize.NewSerializer(),
}
}
func (self *PhpEncoder) SetSerializedEncodeFunc(f php_serialize.SerializedEncodeFunc) {
self.encoder.SetSerializedEncodeFunc(f)
}
func (self *PhpEncoder) Encode() (string, error) {
if self.data == nil {
return "", nil
}
var (
err error
val string
)
buf := bytes.NewBuffer([]byte{})
for k, v := range self.data {
buf.WriteString(k)
buf.WriteRune(SEPARATOR_VALUE_NAME)
if val, err = self.encoder.Encode(v); err != nil {
err = fmt.Errorf("php_session: error during encode value for %q: %v", k, err)
break
}
buf.WriteString(val)
}
return buf.String(), err
}