diff options
author | Marius David Wieschollek <passwords.public@mdns.eu> | 2020-02-10 01:38:24 +0300 |
---|---|---|
committer | Marius David Wieschollek <passwords.public@mdns.eu> | 2020-02-10 01:38:24 +0300 |
commit | 06330f1bf282ba2a96ec2b9d7f114e6b933a9617 (patch) | |
tree | 6aeea07afcd9c026a2f4b2ad24b00670d3d8a476 | |
parent | 4775ed818e3198aecf6ccde6a7245002bf9265ff (diff) |
Add settings converter and collection
Signed-off-by: Marius David Wieschollek <passwords.public@mdns.eu>
-rw-r--r-- | src/Collection/AbstractCollection.js | 6 | ||||
-rw-r--r-- | src/Collection/SettingCollection.js | 5 | ||||
-rw-r--r-- | src/Converter/SettingConverter.js | 97 | ||||
-rw-r--r-- | src/Model/Setting/Setting.js | 41 |
4 files changed, 134 insertions, 15 deletions
diff --git a/src/Collection/AbstractCollection.js b/src/Collection/AbstractCollection.js index e187af4..5947cf4 100644 --- a/src/Collection/AbstractCollection.js +++ b/src/Collection/AbstractCollection.js @@ -107,16 +107,16 @@ export default class AbstractCollection { } /** - * @return {Object[]} + * @return {String[]} */ toJSON() { let json = []; for(let element of this._elements) { - json.push(element.toJSON()); + json.push(this._converter.toObject(element)); } - return json; + return JSON.stringify(json); } [Symbol.iterator]() { diff --git a/src/Collection/SettingCollection.js b/src/Collection/SettingCollection.js new file mode 100644 index 0000000..5f5e9ac --- /dev/null +++ b/src/Collection/SettingCollection.js @@ -0,0 +1,5 @@ +import AbstractCollection from './AbstractCollection'; + +export default class SettingCollection extends AbstractCollection { + +}
\ No newline at end of file diff --git a/src/Converter/SettingConverter.js b/src/Converter/SettingConverter.js new file mode 100644 index 0000000..463aef7 --- /dev/null +++ b/src/Converter/SettingConverter.js @@ -0,0 +1,97 @@ +export default class SettingConverter { + + /** + * @param {Api} api + */ + constructor(api) { + this._api = api; + /** @type {Cache} **/ + this._cache = api.getInstance('cache.cache'); + } + + /** + * + * @param {String} json + * @return {(SettingCollection|Setting)} + */ + fromJSON(json) { + let object = JSON.parse(json); + + if(Array.isArray(object)) { + return this.fromArray(object); + } + + return this.fromObject(object); + } + + /** + * + * @param {Object} object + * @return {Setting} + */ + fromObject(object) { + let key = `setting.${object.scope}.${object.name}`; + + if(this._cache.has(key)) { + /** @type {Setting} **/ + let setting = this._cache.get(key); + if(setting.getValue() !== object.value) setting.setValue(object.value); + + return setting; + } + + let setting = this._api.getClass('model.setting', object.name, object.value, object.scope); + this._cache.set(key, setting, 'setting.model'); + + return setting; + } + + /** + * + * @param {Object[]} array + * @return {SettingCollection} + */ + fromArray(array) { + let settings = []; + + for(let setting of array) { + settings.push(this.fromObject(setting)); + } + + return this._api.getInstance('collection.setting', settings); + } + + /** + * @param {SettingCollection} collection + * @return {Object[]} + */ + toArray(collection) { + let array = []; + + for(let field of collection) { + array.push(this.toObject(field)); + } + + return array; + } + + /** + * @param {Setting} setting + * @return {Object} + */ + toObject(setting) { + return { + scope: setting.scope, + name : setting.name, + value: setting.value + }; + } + + /** + * @param {Setting} setting + * @return {String} + */ + toJSON(setting) { + return JSON.stringify(setting); + } +}
\ No newline at end of file diff --git a/src/Model/Setting/Setting.js b/src/Model/Setting/Setting.js index 3a73bdb..451f44d 100644 --- a/src/Model/Setting/Setting.js +++ b/src/Model/Setting/Setting.js @@ -5,28 +5,28 @@ export default class Setting { /** * @return {String} */ - get SCOPE_USER() { - return 'user'; + static get SCOPE_SERVER() { + return 'server'; } /** * @return {String} */ - get SCOPE_SERVER() { - return 'server'; + static get SCOPE_USER() { + return 'user'; } /** * @return {String} */ - get SCOPE_CLIENT() { + static get SCOPE_CLIENT() { return 'client'; } /** * @return {String[]} */ - get SCOPES() { + static get SCOPES() { return [ this.SCOPE_USER, this.SCOPE_SERVER, @@ -73,9 +73,7 @@ export default class Setting { * @param {String} value */ set scope(value) { - if(this.SCOPES.indexOf(value) === -1) { - throw new InvalidScopeError(value); - } + this._checkScope(value); this._scope = value; } @@ -86,9 +84,7 @@ export default class Setting { * @param {String} scope */ constructor(name, value, scope = 'client') { - if(this.SCOPES.indexOf(scope) === -1) { - throw new InvalidScopeError(scope); - } + this._checkScope(scope); this._name = name; this._value = value; @@ -148,4 +144,25 @@ export default class Setting { return this; } + + /** + * @param {String} scope + * @private + */ + _checkScope(scope) { + if(Setting.SCOPES.indexOf(scope) === -1) { + throw new InvalidScopeError(scope); + } + } + + /** + * @return {{scope: String, name: String, value: String}} + */ + toJSON() { + return { + scope: this._scope, + name : this._name, + value: this._value + }; + } }
\ No newline at end of file |