-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRulesDeserializer.php
More file actions
112 lines (87 loc) · 3.15 KB
/
RulesDeserializer.php
File metadata and controls
112 lines (87 loc) · 3.15 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
declare( strict_types = 1 );
namespace ProfessionalWiki\AutomatedValues\DataAccess;
use DataValues\StringValue;
use ProfessionalWiki\AutomatedValues\Domain\AliasesSpecList;
use ProfessionalWiki\AutomatedValues\Domain\EntityCriteria;
use ProfessionalWiki\AutomatedValues\Domain\LabelSpecList;
use ProfessionalWiki\AutomatedValues\Domain\Rule;
use ProfessionalWiki\AutomatedValues\Domain\Rules;
use ProfessionalWiki\AutomatedValues\Domain\StatementEqualityCriterion;
use ProfessionalWiki\AutomatedValues\Domain\Template;
use ProfessionalWiki\AutomatedValues\Domain\TemplatedAliasesSpec;
use ProfessionalWiki\AutomatedValues\Domain\TemplatedLabelSpec;
use ProfessionalWiki\AutomatedValues\Domain\TemplateSegment;
use ProfessionalWiki\AutomatedValues\Compat;
class RulesDeserializer {
private RulesJsonValidator $validator;
private array $defaultLanguageCodes;
/**
* @param string[] $defaultLanguageCodes
*/
public function __construct( RulesJsonValidator $validator, array $defaultLanguageCodes ) {
$this->validator = $validator;
$this->defaultLanguageCodes = $defaultLanguageCodes;
}
public function deserialize( string $rulesJson ): Rules {
if ( $this->validator->validate( $rulesJson ) ) {
$array = json_decode( $rulesJson, true );
if ( is_array( $array ) && array_key_exists( 'rules', $array ) ) {
return $this->newRules( $array['rules'] );
}
}
// TODO: log warning or throw and log higher up
return new Rules();
}
private function newRules( array $arrayRules ): Rules {
$rules = [];
foreach ( $arrayRules as $arrayRule ) {
$rules[] = new Rule(
$this->newEntityCriteria( $arrayRule ),
$this->newLabelSpecList( $arrayRule ),
$this->newAliasesSpecList( $arrayRule )
);
}
return new Rules( ...$rules );
}
private function newEntityCriteria( array $arrayRule ): EntityCriteria {
return new EntityCriteria(
...array_map(
fn ( array $criterion ) => new StatementEqualityCriterion( Compat::newPId( $criterion['statement'] ), new StringValue( $criterion['equalTo'] ) ),
$arrayRule['when'] ?? []
)
);
}
private function newLabelSpecList( array $arrayRule ): LabelSpecList {
$specs = [];
foreach ( $arrayRule['buildLabel'] ?? [] as $language => $arrayTemplate ) {
$specs[] = new TemplatedLabelSpec(
$language === '*' ? $this->defaultLanguageCodes : [ $language ],
$this->newTemplate( $arrayTemplate )
);
}
return new LabelSpecList( ...$specs );
}
private function newTemplate( array $arraySpec ): Template {
$segments = [];
foreach ( $arraySpec as $property => $template ) {
$ids = explode( '.', $property );
$segments[] = new TemplateSegment(
$template,
Compat::newPId( $ids[0] ),
array_key_exists( 1, $ids ) ? Compat::newPId( $ids[1] ) : null
);
}
return new Template( ...$segments );
}
private function newAliasesSpecList( array $arrayRule ): AliasesSpecList {
$specs = [];
foreach ( $arrayRule['buildAliases'] ?? [] as $language => $arrayTemplate ) {
$specs[] = new TemplatedAliasesSpec(
$language === '*' ? $this->defaultLanguageCodes : [ $language ],
$this->newTemplate( $arrayTemplate )
);
}
return new AliasesSpecList( ...$specs );
}
}