HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/csb.komma.pro/resources/js/global/models/language.js
/**
 * Language.
 *
 * Represents a Language
 */
class Language {
    constructor()
    {
        this._name = '';
        this._displayName = '';
        this._nativeName = '';
        this._twoLetterIsoLanguageName = '';
        this._threeLetterIsoLanguageName = '';
    }

    /**
     * @param {string} json
     * @return {Language|null}
     */
    static fromJsonString(json) {
        if(!this.is(json)) {
            return null;
        }
        let jsonObject = JSON.parse(json);

        let instance = new this;
        instance._name = jsonObject.name;
        instance._displayName = jsonObject.displayName;
        instance._nativeName = jsonObject.nativeName;
        instance._twoLetterIsoLanguageName = jsonObject.twoLetterIsoLanguageName;
        instance._threeLetterIsoLanguageName = jsonObject.threeLetterIsoLanguageName;

        return instance;
    }

    /**
     * Checks that the given json string represents a ErrorResponse
     *
     * @param {string|object} json
     * @param {boolean} logErrors
     * @return {boolean}
     */
    static is(json, logErrors = true)
    {
        let jsonObject = null;
        
        if(typeof json === 'string') {
            try {
                jsonObject = JSON.parse(json);
                if (!jsonObject) return false;
            } catch (e) {
                console.error('Language: The given json does not represent a Language since the json string was not a valid json. Object: ', jsonObject, json);
                return false;
            }
        } else {
            jsonObject = json
        }

        if(!jsonObject.hasOwnProperty('name') || typeof jsonObject.displayName !== 'string') {
            if(logErrors) console.error('Language: The language object must have a property called name that is a string. Object: ', jsonObject);
            return false;
        }
        if(!jsonObject.hasOwnProperty('displayName') || typeof jsonObject.displayName !== 'string') {
            if(logErrors) console.error('Language: The language object must have a property called displayName that is a string. Object: ', jsonObject);
            return false;
        }
        if(!jsonObject.hasOwnProperty('nativeName') || typeof jsonObject.nativeName !== 'string') {
            if(logErrors) console.error('Language: The language object must have a property called nativeName that is a string. Object: ', jsonObject);
            return false;
        }
        if(!jsonObject.hasOwnProperty('twoLetterIsoLanguageName') || typeof jsonObject.twoLetterIsoLanguageName !== 'string') {
            if(logErrors) console.error('Language: The language object must have a property called twoLetterIsoLanguageName that is a string. Object: ', jsonObject.twoLetterIsoLanguageName);
            return false;
        }
        if(!jsonObject.hasOwnProperty('threeLetterIsoLanguageName') || typeof jsonObject.threeLetterIsoLanguageName !== 'string') {
            if(logErrors) console.error('Language: The language object must have a property called threeLetterIsoLanguageName that is a string. Object: ', jsonObject);
            return false;
        }

        return true;
    }


    /**
     * @return {{valid: boolean, errors: Array}}
     * @private
     */
    _toJson() {
        return {
            'name': this._name,
            'displayName': this._displayName,
            'nativeName': this._nativeName,
            'twoLetterIsoLanguageName': this._twoLetterIsoLanguageName,
            'threeLetterIsoLanguageName': this._threeLetterIsoLanguageName
        }
    }

    get name() {
        return this._name;
    }

    set name(value) {
        this._name = value;
    }

    get displayName() {
        return this._displayName;
    }

    set displayName(value) {
        this._displayName = value;
    }

    get nativeName() {
        return this._nativeName;
    }

    set nativeName(value) {
        this._nativeName = value;
    }

    get twoLetterIsoLanguageName() {
        return this._twoLetterIsoLanguageName;
    }

    set twoLetterIsoLanguageName(value) {
        this._twoLetterIsoLanguageName = value;
    }

    get threeLetterIsoLanguageName() {
        return this._threeLetterIsoLanguageName;
    }

    set threeLetterIsoLanguageName(value) {
        this._threeLetterIsoLanguageName = value;
    }
}

export { Language }