Files
gridpilot.gg/core/identity/domain/value-objects/ExternalRating.ts
2026-01-16 16:46:57 +01:00

54 lines
1.5 KiB
TypeScript

import type { ValueObject } from '@core/shared/domain/ValueObject';
import { IdentityDomainValidationError } from '../errors/IdentityDomainError';
import { GameKey } from './GameKey';
export interface ExternalRatingProps {
gameKey: GameKey;
type: string;
value: number;
}
export class ExternalRating implements ValueObject<ExternalRatingProps> {
readonly gameKey: GameKey;
readonly type: string;
readonly value: number;
private constructor(gameKey: GameKey, type: string, value: number) {
this.gameKey = gameKey;
this.type = type;
this.value = value;
}
static create(gameKey: GameKey, type: string, value: number): ExternalRating {
if (!type || type.trim().length === 0) {
throw new IdentityDomainValidationError('External rating type cannot be empty');
}
if (typeof value !== 'number' || isNaN(value)) {
throw new IdentityDomainValidationError('External rating value must be a valid number');
}
const trimmedType = type.trim();
return new ExternalRating(gameKey, trimmedType, value);
}
get props(): ExternalRatingProps {
return {
gameKey: this.gameKey,
type: this.type,
value: this.value,
};
}
equals(other: ValueObject<ExternalRatingProps>): boolean {
return (
this.gameKey.equals(other.props.gameKey) &&
this.type === other.props.type &&
this.value === other.props.value
);
}
toString(): string {
return `${this.gameKey.toString()}:${this.type}=${this.value}`;
}
}