Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持 php serialize #200

Merged
merged 1 commit into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ const transform = debounce(async () => {
return current.serialization = Serialize.formYaml(current.value)
case "php_array":
return current.serialization = Serialize.formPhpArray(current.value)
case "php_serialize":
return current.serialization = Serialize.formPhpSerialize(current.value)
case "toml":
return current.serialization = Serialize.formToml(current.value)
case "properties":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ watch(() => {
case "php_array":
r = data.toPhpArray()
break
case "php_serialize":
r = data.toPhpSerialize()
break
case "toml":
r = data.toToml()
break
Expand All @@ -145,6 +148,7 @@ watch(() => {
result = await formatter.simple(current.type, 'beautify', r)
}
} catch (e) {
console.error(e)
result = $error(e)
}
}, {immediate: true, deep: true})
Expand Down
1 change: 1 addition & 0 deletions packages/ctool-core/src/helper/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const customLanguages = [
defineConfig('Angular', getCodemirrorLanguage('html')),
defineConfig('HTML Table', getCodemirrorLanguage('html'), ['html_table']),
defineConfig('PHP Array', getCodemirrorLanguage('php'), ['php_array']),
defineConfig('Php Serialize', getCodemirrorLanguage('php'), ['php_serialize']),
defineConfig('Http Query String', null, ['http_query_string']),
defineConfig('JSON Schema', getCodemirrorLanguage('json')),
defineConfig('JavaScript PropTypes', getCodemirrorLanguage('TypeScript')),
Expand Down
11 changes: 11 additions & 0 deletions packages/ctool-core/src/helper/serialize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import yaml from "./yaml"
import phpArray from "./phpArray"
import toml from "@ltd/j-toml"
import xml from "./xml"
import phpSerialize from "./phpSerialize"
import {isEmpty, isObject} from "lodash";
import Json from "@/helper/json"

Expand Down Expand Up @@ -99,6 +100,12 @@ class Serialize<T extends ContentType = ContentType> {
})
}

static formPhpSerialize<T extends ContentType = ContentType>(str) {
return Serialize.formCallback<T>(() => {
return phpSerialize.parse<T>(str)
})
}

static formToml<T extends ContentType = ContentType>(str: string) {
return Serialize.formCallback<T>(() => {
return toml.parse(str) as T
Expand Down Expand Up @@ -135,6 +142,10 @@ class Serialize<T extends ContentType = ContentType> {
return yaml.dump(this.content())
}

toPhpSerialize() {
return phpSerialize.stringify(this.content());
}

toPhpArray() {
return phpArray.stringify(this.content());
}
Expand Down
11 changes: 11 additions & 0 deletions packages/ctool-core/src/helper/serialize/phpSerialize/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import unserialize from "./unserialize"
import serialize from "./serialize"

export default {
parse<T = any>(value = ""): T {
return unserialize(value)
},
stringify(json: any): string {
return serialize(json)
}
}
132 changes: 132 additions & 0 deletions packages/ctool-core/src/helper/serialize/phpSerialize/serialize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// @ts-nocheck
/**
* @see https://github.com/locutusjs/locutus/blob/master/src/php/var/serialize.js
* support bigint
*/
const serialize = (mixedValue: any) => {
// discuss at: https://locutus.io/php/serialize/
// original by: Arpad Ray (mailto:arpad@php.net)
// improved by: Dino
// improved by: Le Torbi (https://www.letorbi.de/)
// improved by: Kevin van Zonneveld (https://kvz.io/)
// bugfixed by: Andrej Pavlovic
// bugfixed by: Garagoth
// bugfixed by: Russell Walker (https://www.nbill.co.uk/)
// bugfixed by: Jamie Beck (https://www.terabit.ca/)
// bugfixed by: Kevin van Zonneveld (https://kvz.io/)
// bugfixed by: Ben (https://benblume.co.uk/)
// bugfixed by: Codestar (https://codestarlive.com/)
// bugfixed by: idjem (https://github.com/idjem)
// input by: DtTvB (https://dt.in.th/2008-09-16.string-length-in-bytes.html)
// input by: Martin (https://www.erlenwiese.de/)
// note 1: We feel the main purpose of this function should be to ease
// note 1: the transport of data between php & js
// note 1: Aiming for PHP-compatibility, we have to translate objects to arrays
// example 1: serialize(['Kevin', 'van', 'Zonneveld'])
// returns 1: 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'
// example 2: serialize({firstName: 'Kevin', midName: 'van'})
// returns 2: 'a:2:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";}'
// example 3: serialize( {'ü': 'ü', '四': '四', '𠜎': '𠜎'})
// returns 3: 'a:3:{s:2:"ü";s:2:"ü";s:3:"四";s:3:"四";s:4:"𠜎";s:4:"𠜎";}'

let val, key, okey
let ktype = ''
let vals = ''
let count = 0

const _utf8Size = function (str) {
return ~-encodeURI(str).split(/%..|./).length
}

const _getType = function (inp) {
let match
let key
let cons
let types
let type = typeof inp

if (type === 'object' && !inp) {
return 'null'
}

if (type === 'object') {
if (!inp.constructor) {
return 'object'
}
cons = inp.constructor.toString()
match = cons.match(/(\w+)\(/)
if (match) {
cons = match[1].toLowerCase()
}
types = ['boolean', 'number', 'string', 'array']
for (key in types) {
if (cons === types[key]) {
type = types[key]
break
}
}
}
return type
}

const type = _getType(mixedValue)

switch (type) {
case 'function':
val = ''
break
case 'boolean':
val = 'b:' + (mixedValue ? '1' : '0')
break
case 'number':
val = (Math.round(mixedValue) === mixedValue ? 'i' : 'd') + ':' + mixedValue
break
case 'bigint':
val = `i:${(mixedValue as BigInt).toString()}`
break
case 'string':
val = 's:' + _utf8Size(mixedValue) + ':"' + mixedValue + '"'
break
case 'array':
case 'object':
val = 'a'
/*
if (type === 'object') {
var objname = mixedValue.constructor.toString().match(/(\w+)\(\)/);
if (objname === undefined) {
return;
}
objname[1] = serialize(objname[1]);
val = 'O' + objname[1].substring(1, objname[1].length - 1);
}
*/

for (key in mixedValue) {
if (mixedValue.hasOwnProperty(key)) {
ktype = _getType(mixedValue[key])
if (ktype === 'function') {
continue
}

okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key)
vals += serialize(okey) + serialize(mixedValue[key])
count++
}
}
val += ':' + count + ':{' + vals + '}'
break
case 'undefined':
default:
// Fall-through
// if the JS object has a property which contains a null value,
// the string cannot be unserialized by PHP
val = 'N'
break
}
if (type !== 'object' && type !== 'array') {
val += ';'
}

return val
}
export default serialize
Loading