35 lines
808 B
TypeScript
35 lines
808 B
TypeScript
import { DomainException } from '@shared/exceptions/domain.exception';
|
||
|
||
export class PhoneNumber {
|
||
private readonly _value: string;
|
||
|
||
private constructor(value: string) {
|
||
const normalized = value.replace(/\s+/g, '');
|
||
if (!/^1[3-9]\d{9}$/.test(normalized)) {
|
||
throw new DomainException('手机号格式错误,必须是11位中国大陆手机号');
|
||
}
|
||
this._value = normalized;
|
||
}
|
||
|
||
static create(value: string): PhoneNumber {
|
||
return new PhoneNumber(value);
|
||
}
|
||
|
||
get value(): string {
|
||
return this._value;
|
||
}
|
||
|
||
masked(): string {
|
||
return this._value.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
|
||
}
|
||
|
||
equals(other: PhoneNumber): boolean {
|
||
if (!other) return false;
|
||
return this._value === other.value;
|
||
}
|
||
|
||
toString(): string {
|
||
return this.masked();
|
||
}
|
||
}
|