forked from HydraCG/Heracles.ts
/
MappingsBuilder.ts
69 lines (60 loc) · 2.07 KB
/
MappingsBuilder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { IDictionary } from "../IDictionary";
import MappingsCollection from "./Collections/MappingsCollection";
import PropertyMapping from "./PropertyMapping";
/**
* Provides a builder for {@link IIriTemplate} variable mapping values.
*/
export default class MappingsBuilder {
private readonly mappings: MappingsCollection;
private readonly result: IDictionary<string>;
/**
* Gets variable mappings in form of variable name - property IRI pairs.
* @returns {IDictionary<string>}
*/
public get variableMappings(): IDictionary<string> {
const result = {};
for (const mapping of this.mappings) {
result[mapping.variable] = mapping.property.iri;
}
return result;
}
/**
* Initializes a new instance of the {@link MappingBuilder} class.
* @param {MappingsCollection} mappings IRI template variable mappings collection.
*/
public constructor(mappings: MappingsCollection) {
this.mappings = mappings;
this.result = {};
}
/**
* Allows to add an IRI property value.
* @param {string} property IRI of the property to be filled with value.
* @returns {PropertyMapping}
*/
public withProperty(property: string): PropertyMapping {
const mapping = this.mappings.ofProperty(property).first();
if (!mapping) {
throw new Error(`Invalid mapping of property '${property}.`);
}
return new PropertyMapping(this, this.result, mapping);
}
/**
* Allows to add a direct variable value.
* @param {string} property IRI of the property to be filled with value.
* @returns {PropertyMapping}
*/
public withVariable(variableName: string): PropertyMapping {
const mapping = this.mappings.ofVariableName(variableName).first();
if (!mapping) {
throw new Error(`Invalid mapping of variable name mapping '${variableName}.`);
}
return new PropertyMapping(this, this.result, mapping);
}
/**
* Completes the variable values mappings in form of variable name - serialized value pairs.
* @returns {IDictionary<string>}
*/
public complete(): IDictionary<string> {
return this.result;
}
}