-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathCondition.php
More file actions
180 lines (157 loc) · 6.08 KB
/
Condition.php
File metadata and controls
180 lines (157 loc) · 6.08 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace SplitIO\Grammar;
use SplitIO\Exception\InvalidMatcherException;
use SplitIO\Grammar\Condition\Combiner\AndCombiner;
use SplitIO\Grammar\Condition\Combiner\CombinerEnum;
use SplitIO\Grammar\Condition\Combiner\Factor\NotFactor;
use SplitIO\Grammar\Condition\ConditionTypeEnum;
use SplitIO\Grammar\Condition\Matcher;
use SplitIO\Grammar\Condition\Matcher\AbstractMatcher;
use SplitIO\Grammar\Condition\Matcher\Dependency;
use SplitIO\Grammar\Condition\Partition;
use SplitIO\Grammar\Condition\Partition\TreatmentEnum;
use SplitIO\Sdk\Impressions\ImpressionLabel;
use SplitIO\Split as SplitApp;
class Condition
{
private $matcherGroup = null;
private $partitions = null;
private $label = null;
private $conditionType = null;
//On the next versions the condition will support Combiners: AND, OR, NOT
private $combiner = null;
/**
* @param array $condition
*/
public function __construct(array $condition)
{
SplitApp::logger()->debug(print_r($condition, true));
//So far the combiner is AND. On next versions the condition will support Combiners: OR
$this->combiner = new CombinerEnum(CombinerEnum::_AND);
if (isset($condition['partitions']) && is_array($condition['partitions'])) {
$this->partitions = array();
foreach ($condition['partitions'] as $partition) {
$this->partitions[] = new Partition($partition);
}
}
if (isset($condition['matcherGroup']['matchers']) && is_array($condition['matcherGroup']['matchers'])) {
$this->matcherGroup = array();
foreach ($condition['matcherGroup']['matchers'] as $matcher) {
$this->matcherGroup[] = Matcher::factory($matcher);
}
}
if (isset($condition['label']) && !empty($condition['label'])) {
$this->label = $condition['label'];
}
if (isset($condition['conditionType']) && ConditionTypeEnum::isValid($condition['conditionType'])) {
$this->conditionType = $condition['conditionType'];
} else {
$this->conditionType = ConditionTypeEnum::WHITELIST;
}
}
/**
* @param $key
* @param array|null $attributes
* @return bool
*/
public function match($key, ?array $attributes = null, $bucketingKey = null)
{
$eval = array();
foreach ($this->matcherGroup as $matcher) {
if ($matcher instanceof AbstractMatcher) {
$_evaluation = false;
//Check if Matcher has attributes
if (!$matcher->hasAttribute()) {
// scenario 1: no attr in matcher
// e.g. if user is in segment all then split 100:on
$_evaluation = $matcher->evaluate($key);
} else {
// scenario 2: attribute provided but no attribute value provided. Matcher does not match
// e.g. if user.age is >= 10 then split 100:on
if ($attributes === null || !isset($attributes[$matcher->getAttribute()])) {
$_evaluation = false;
} else {
// instead of using the user id, we use the attribute value for evaluation
$attrValue = $attributes[$matcher->getAttribute()];
$_evaluation = $matcher->evaluate($attrValue);
}
}
//If matcher is Negate or not
$eval[] = ($matcher->isNegate()) ? NotFactor::evaluate($_evaluation) : $_evaluation ;
} elseif ($matcher instanceof Dependency) {
$printable = is_array($key) ? implode($key) : $key;
$printableAttributes = is_array($attributes) ? implode($attributes) : $attributes;
SplitApp::logger()->info("Evaluating on IN_SPLIT_TREATMENT the KEY $printable");
SplitApp::logger()->info("with the following attributes: $printableAttributes");
$_evaluation = $matcher->evalKey($key, $attributes, $bucketingKey);
$eval[] = ($matcher->isNegate()) ? NotFactor::evaluate($_evaluation) : $_evaluation ;
} else {
//Throwing catchable exception the SDK client will return CONTROL
throw new InvalidMatcherException("Invalid Matcher");
}
}
if ($this->combiner instanceof CombinerEnum) {
switch ($this->combiner->getValue()) {
case CombinerEnum::_AND:
default:
return AndCombiner::evaluate($eval);
}
}
return false;
}
public static function getDefaultCondition()
{
return new Condition(array(
'conditionType' => ConditionTypeEnum::WHITELIST,
'matcherGroup' => array(
'combiner' => CombinerEnum::_AND,
'matchers' => array(
array(
'matcherType' => Matcher::ALL_KEYS,
'negate' => false,
'userDefinedSegmentMatcherData' => null,
'whitelistMatcherData' => null
)
)
),
'partitions' => array(
array(
'treatment' => TreatmentEnum::CONTROL,
'size' => 100
)
),
'label' => ImpressionLabel::UNSUPPORTED_MATCHER
));
}
/**
* @return array|null
*/
public function getPartitions()
{
return $this->partitions;
}
/**
* @return array
*/
public function getTreatments()
{
$treatments = array();
if ($this->partitions) {
foreach ($this->partitions as $partition) {
$treatments[] = $partition->getTreatment();
}
}
return $treatments;
}
/**
* @return null|string
*/
public function getLabel()
{
return $this->label;
}
public function getConditionType()
{
return $this->conditionType;
}
}