Passed
Pull Request — develop (#7)
by Tito
01:57
created

Entity.getIdString   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
1
import { FieldNotFoundError } from "Errors/Entities/FieldNotFoundError";
2
import { UniqueEntityId } from "Utilities/Ids/UniqueEntityId";
3
4
/**
5
 * Abstract Entity class
6
 */
7
export abstract class Entity<T> {
8
  /**
9
   * Id of the entity
10
   */
11
  protected readonly _id: UniqueEntityId;
12
13
  /**
14
   * Collection of properties
15
   */
16
  protected readonly props: T;
17
18
  /**
19
   * @param T props
20
   * @param IEntityDefinition definition Needed for each specific entity overriding this entity.  The child entity should not expose this parameter in its own constructor
21
   * @param UniqueEntityId id If not sent, it generates a UUID
22
   */
23
  constructor(props: T, id?: UniqueEntityId) {
24
    this.props = props;
25
    this._id = id ? id : new UniqueEntityId();
26
  }
27
28
  /**
29
   * Gets the id in its natural type
30
   *
31
   * @returns string|number
32
   */
33
  public getId(): string | number {
34
    return this._id.toValue();
35
  }
36
37
  /**
38
   * Gets the id stringified
39
   *
40
   * @returns string
41
   */
42
  public getIdString(): string {
43
    return this._id.toString();
44
  }
45
46
  /**
47
   * Field getter
48
   *
49
   * @param field
50
   *
51
   * @throws FieldNotFoundError
52
   */
53
  public get(field: string): any {
54
    if (typeof this.props[field] === "undefined") {
55
      throw new FieldNotFoundError(field);
56
    }
57
58
    return this.props[field];
59
  }
60
}
61