php: Move server code into new phpx package

This commit is contained in:
Tom Wiesing 2022-10-19 13:52:24 +02:00
parent 4df5f6387c
commit 2e47626900
No known key found for this signature in database
13 changed files with 134 additions and 86 deletions

View file

@ -1,33 +0,0 @@
package phpserver
import (
"encoding/json"
"strings"
)
// Marshal marshals data as a PHP expression, meaning it can safely be used inside code.
//
// Typically data is marshaled using [json.Marshal] and decoded in PHP using 'json_decode'.
// Special cases may exist for specific datatypes.
func Marshal(data any) (string, error) {
switch d := data.(type) {
case string:
return MarshalString(d), nil
}
bytes, err := json.Marshal(data)
if err != nil {
return "", err
}
return "json_decode(" + MarshalString(string(bytes)) + ")", nil
}
var replacer = strings.NewReplacer("'", "\\'", "\\", "\\\\")
// MarshalString marshals s as a php string that can be used safely as a PHP expression.
//
// See [https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single].
func MarshalString(s string) string {
return "'" + replacer.Replace(s) + "'"
}