/** * Class model for Collection Interface */ import type { CollectionContentTypes, CollectionInterface, CollectionModelInterface, CollectionPropertiesInterface, CollectionPropertiesModelInterface } from "@/types/collection"; import type { CollectionIdentifier, ServiceIdentifier } from "@/types/common"; import { clonePlain } from './clone-plain'; export class CollectionObject implements CollectionModelInterface { _data!: CollectionInterface; _properties: CollectionPropertiesObject | undefined = undefined; constructor() { this._data = { '@type': 'people:collection', version: 1, provider: '', service: '' as ServiceIdentifier, collection: null as CollectionIdentifier | null, identifier: '' as CollectionIdentifier, properties: new CollectionPropertiesObject().toJson(), }; } fromJson(data: CollectionInterface): CollectionObject { this._data = clonePlain(data); this._properties = undefined; return this; } toJson(): CollectionInterface { const json = this._properties ? { ...this._data, properties: this._properties.toJson() } : this._data; return clonePlain(json); } clone(): CollectionObject { return new CollectionObject().fromJson(this.toJson()); } /** Immutable Properties */ get provider(): string { return this._data.provider; } get service(): ServiceIdentifier { return this._data.service as ServiceIdentifier; } get collection(): CollectionIdentifier | null { return this._data.collection as CollectionIdentifier | null; } get identifier(): CollectionIdentifier { return this._data.identifier as CollectionIdentifier; } get signature(): string | null | undefined { return this._data.signature; } get created(): string | null | undefined { return this._data.created; } get modified(): string | null | undefined { return this._data.modified; } /** Mutable Properties */ get properties(): CollectionPropertiesObject { if (this._properties) { return this._properties; } else if (this._data.properties) { const properties = new CollectionPropertiesObject().fromJson(this._data.properties as CollectionPropertiesInterface); this._properties = properties; return properties; } return new CollectionPropertiesObject(); } set properties(value: CollectionPropertiesObject) { this._properties = value; } } export class CollectionPropertiesObject implements CollectionPropertiesModelInterface { private _data!: CollectionPropertiesInterface; constructor() { this._data = { '@type': 'people:addressbook', content: [], label: '', description: null, rank: null, visibility: null, color: null, }; } fromJson(data: CollectionPropertiesInterface): CollectionPropertiesObject { this._data = clonePlain(data); return this; } toJson(): CollectionPropertiesInterface { return clonePlain(this._data); } clone(): CollectionPropertiesObject { return new CollectionPropertiesObject().fromJson(this.toJson()); } /** Immutable Properties */ get content(): CollectionContentTypes[] { return this._data.content ?? []; } /** Mutable Properties */ get label(): string { return this._data.label || ''; } set label(value: string) { this._data.label = value; } get description(): string | null { return this._data.description; } set description(value: string | null) { this._data.description = value; } get rank(): number | null { return this._data.rank; } set rank(value: number | null) { this._data.rank = value; } get visibility(): boolean | null { return this._data.visibility; } set visibility(value: boolean | null) { this._data.visibility = value; } get color(): string | null { return this._data.color; } set color(value: string | null) { this._data.color = value; } }