44 lines
1.0 KiB
TypeScript
44 lines
1.0 KiB
TypeScript
import type { ValueObject } from '@core/shared/domain/ValueObject';
|
|
import { IdentityDomainValidationError } from '../errors/IdentityDomainError';
|
|
|
|
export interface RatingValueProps {
|
|
value: number;
|
|
}
|
|
|
|
export class RatingValue implements ValueObject<RatingValueProps> {
|
|
readonly value: number;
|
|
|
|
private constructor(value: number) {
|
|
this.value = value;
|
|
}
|
|
|
|
static create(value: number): RatingValue {
|
|
if (typeof value !== 'number' || isNaN(value)) {
|
|
throw new IdentityDomainValidationError('Rating value must be a valid number');
|
|
}
|
|
|
|
if (value < 0 || value > 100) {
|
|
throw new IdentityDomainValidationError(
|
|
`Rating value must be between 0 and 100, got: ${value}`
|
|
);
|
|
}
|
|
|
|
return new RatingValue(value);
|
|
}
|
|
|
|
get props(): RatingValueProps {
|
|
return { value: this.value };
|
|
}
|
|
|
|
equals(other: ValueObject<RatingValueProps>): boolean {
|
|
return this.value === other.props.value;
|
|
}
|
|
|
|
toNumber(): number {
|
|
return this.value;
|
|
}
|
|
|
|
toString(): string {
|
|
return this.value.toString();
|
|
}
|
|
} |