/** * Class model for Entity Interface */ import type { EntityInterface } from "@/types/entity"; import type { IndividualInterface } from "@/types/individual"; import type { OrganizationInterface } from "@/types/organization"; import type { GroupInterface } from "@/types/group"; import { IndividualObject } from "./individual"; import { OrganizationObject } from "./organization"; import { GroupObject } from "./group"; export class EntityObject implements EntityInterface { _data!: EntityInterface; constructor() { this._data = { provider: '', service: '', collection: '', identifier: '', signature: null, created: null, modified: null, properties: new IndividualObject(), }; } fromJson(data: EntityInterface) : EntityObject { this._data = data; if (data.properties) { const type = data.properties.type; if (type === 'organization') { this._data.properties = new OrganizationObject().fromJson(data.properties as OrganizationInterface); } else if (type === 'group') { this._data.properties = new GroupObject().fromJson(data.properties as GroupInterface); } else { this._data.properties = new IndividualObject().fromJson(data.properties as IndividualInterface); } } return this; } toJson(): EntityInterface { const json = { ...this._data }; if (this._data.properties instanceof IndividualObject || this._data.properties instanceof OrganizationObject || this._data.properties instanceof GroupObject) { json.properties = this._data.properties.toJson(); } return json; } clone(): EntityObject { const cloned = new EntityObject(); cloned._data = { ...this._data }; return cloned; } /** Immutable Properties */ get provider(): string { return this._data.provider; } get service(): string { return this._data.service; } get collection(): string | number { return this._data.collection; } get identifier(): string | number { return this._data.identifier; } get signature(): string | null { return this._data.signature; } get created(): string | null { return this._data.created; } get modified(): string | null { return this._data.modified; } get properties(): IndividualObject | OrganizationObject | GroupObject { if (this._data.properties instanceof IndividualObject || this._data.properties instanceof OrganizationObject || this._data.properties instanceof GroupObject) { return this._data.properties; } const defaultProperties = new IndividualObject(); this._data.properties = defaultProperties; return defaultProperties; } set properties(value: IndividualObject | OrganizationObject | GroupObject) { this._data.properties = value; } }