php-serialize
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -11,3 +11,3 @@ { | ||
"comma-dangle": [2, "never"], | ||
"max-len": [1, 120], | ||
"max-len": [1, 150], | ||
"no-console": 0, | ||
@@ -17,3 +17,4 @@ "prefer-template": 0, | ||
"no-multi-spaces": 0, | ||
"no-use-before-define": 0 | ||
"no-use-before-define": 0, | ||
"camelcase": 0 | ||
}, | ||
@@ -20,0 +21,0 @@ "globals": { |
@@ -0,1 +1,5 @@ | ||
## 1.2.0 | ||
- Add support for `__PHP_Incomplete_Class` | ||
## 1.1.1 | ||
@@ -2,0 +6,0 @@ |
@@ -9,2 +9,4 @@ 'use strict'; | ||
var _helpers = require('./helpers'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -19,7 +21,2 @@ | ||
function getClass(prototype) { | ||
function Container() {} | ||
Container.prototype = prototype; | ||
return Container; | ||
} | ||
@@ -70,2 +67,3 @@ function serialize(item) { | ||
var items = []; | ||
var constructorName = item.__PHP_Incomplete_Class_Name || item.constructor.name.length; | ||
for (var _key in item) { | ||
@@ -78,6 +76,6 @@ if (item.hasOwnProperty(_key) && typeof item[_key] !== 'function') { | ||
} | ||
return 'O:' + item.constructor.name.length + ':"' + item.constructor.name + '":' + items.length / 2 + ':{' + items.join('') + '}'; | ||
return 'O:' + constructorName + ':"' + item.constructor.name + '":' + items.length / 2 + ':{' + items.join('') + '}'; | ||
} | ||
function unserializeItem(item, scope) { | ||
function unserializeItem(item, scope, options) { | ||
var type = item.substr(0, 1); | ||
@@ -108,6 +106,14 @@ if (type === 'i' || type === 'd') { | ||
var classContent = item.slice(contentOffset, contentOffset + contentLength); | ||
(0, _assert2.default)(typeof scope[className] !== 'undefined', 'Class ' + className + ' not found in given scope'); | ||
(0, _assert2.default)(typeof scope[className].prototype.unserialize === 'function', className + '.prototype.unserialize is not a function'); | ||
var container = new (getClass(scope[className].prototype))(); | ||
container.unserialize(classContent); | ||
var classReference = scope[className]; | ||
var container = void 0; | ||
if (!classReference) { | ||
if (options.strict) { | ||
(0, _assert2.default)(false, 'Class ' + className + ' not found in given scope'); | ||
} | ||
container = (0, _helpers.getIncompleteClass)(className); | ||
} else { | ||
(0, _assert2.default)(typeof scope[className].prototype.unserialize === 'function', className + '.prototype.unserialize is not a function'); | ||
container = new ((0, _helpers.getClass)(scope[className].prototype))(); | ||
container.unserialize(classContent); | ||
} | ||
return { index: contentOffset + contentLength + 1, value: container }; | ||
@@ -129,3 +135,3 @@ } | ||
} else container[key] = value; | ||
}); | ||
}, options); | ||
return { | ||
@@ -145,7 +151,15 @@ v: { index: 4 + (lengthEnd - 2) + index + 1, value: container } | ||
var contentOffset = info.index + info[0].length + 1; | ||
(0, _assert2.default)(typeof scope[className] !== 'undefined', 'Class ' + className + ' not found in given scope'); | ||
var container = new (getClass(scope[className].prototype))(); | ||
var classReference = scope[className]; | ||
var container = void 0; | ||
if (!classReference) { | ||
if (options.strict) { | ||
(0, _assert2.default)(false, 'Class ' + className + ' not found in given scope'); | ||
} | ||
container = (0, _helpers.getIncompleteClass)(className); | ||
} else { | ||
container = new ((0, _helpers.getClass)(scope[className].prototype))(); | ||
} | ||
var index = unserializeObject(contentLength, item.slice(contentOffset), scope, function (key, value) { | ||
container[key] = value; | ||
}); | ||
}, options); | ||
return { | ||
@@ -161,3 +175,3 @@ v: { index: contentOffset + index + 1, value: container } | ||
function unserializeObject(count, content, scope, valueCallback) { | ||
function unserializeObject(count, content, scope, valueCallback, options) { | ||
var realCount = count * 2; | ||
@@ -167,3 +181,3 @@ var index = 0; | ||
for (var i = 0; i < realCount; ++i) { | ||
var item = unserializeItem(content.slice(index), scope); | ||
var item = unserializeItem(content.slice(index), scope, options); | ||
if (key !== null) { | ||
@@ -182,6 +196,10 @@ valueCallback(key, item.value); | ||
var scope = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
return unserializeItem(item, scope).value; | ||
if (typeof options.strict === 'undefined') { | ||
options.strict = true; | ||
} | ||
return unserializeItem(item, scope, options).value; | ||
} | ||
module.exports = { serialize: serialize, unserialize: unserialize }; |
{ | ||
"name": "php-serialize", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "PHP serialize/unserialize in Javascript", | ||
@@ -9,3 +9,4 @@ "main": "lib/index.js", | ||
"lint": "eslint .", | ||
"compile": "ucompiler go" | ||
"compile": "ucompiler go", | ||
"watch": "ucompiler watch" | ||
}, | ||
@@ -25,3 +26,3 @@ "repository": { | ||
"babel-preset-steelbrain": "^2.0.1", | ||
"eslint": "~2.2.0", | ||
"eslint": "~2.7.0", | ||
"eslint-config-airbnb": "^6.1.0", | ||
@@ -28,0 +29,0 @@ "eslint-plugin-react": "^4.2.3", |
@@ -33,3 +33,3 @@ PHP-Serialize | ||
serialize(item: any): string | ||
unserialize(item: string, scope: Object = {}): any | ||
unserialize(item: string, scope: Object = {}, options: { strict: boolean } = { strict: false }): any | ||
} | ||
@@ -36,0 +36,0 @@ ``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13137
13
218