-
Notifications
You must be signed in to change notification settings - Fork 29
feat: Semantic Versioning suppport in Audience Evaluation #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
88dffb7
ea29804
9f1bf1c
6a0d5d3
41e373b
e5f1c45
5880e51
cc7d95b
ece7e62
f7bfdf6
ee3f9f2
cda1a90
7c61ebe
5c70a89
0cdb46b
761d679
137b967
fac2ec2
7ac685a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
|
|
||
| use Monolog\Logger; | ||
| use Optimizely\Enums\CommonAudienceEvaluationLogs as logs; | ||
| use Optimizely\Utils\SemVersionConditionEvaluator; | ||
| use Optimizely\Utils\Validator; | ||
|
|
||
| class CustomAttributeConditionEvaluator | ||
|
|
@@ -28,12 +29,19 @@ class CustomAttributeConditionEvaluator | |
| const EXACT_MATCH_TYPE = 'exact'; | ||
| const EXISTS_MATCH_TYPE = 'exists'; | ||
| const GREATER_THAN_MATCH_TYPE = 'gt'; | ||
| const GREATER_THAN_EQUAL_TO_MATCH_TYPE = 'ge'; | ||
| const LESS_THAN_MATCH_TYPE = 'lt'; | ||
| const LESS_THAN_EQUAL_TO_MATCH_TYPE = 'le'; | ||
| const SUBSTRING_MATCH_TYPE = 'substring'; | ||
| const SEMVER_EQ = 'semver_eq'; | ||
| const SEMVER_GT = 'semver_gt'; | ||
| const SEMVER_GE = 'semver_ge'; | ||
| const SEMVER_LT = 'semver_lt'; | ||
| const SEMVER_LE = 'semver_le'; | ||
|
|
||
| /** | ||
| * @var UserAttributes | ||
| */ | ||
| */ | ||
| protected $userAttributes; | ||
|
|
||
| /** | ||
|
|
@@ -57,7 +65,7 @@ protected function setNullForMissingKeys(array $leafCondition) | |
| { | ||
| $keys = ['type', 'match', 'value']; | ||
| foreach ($keys as $key) { | ||
| $leafCondition[$key] = isset($leafCondition[$key]) ? $leafCondition[$key]: null; | ||
| $leafCondition[$key] = isset($leafCondition[$key]) ? $leafCondition[$key] : null; | ||
| } | ||
|
|
||
| return $leafCondition; | ||
|
|
@@ -70,8 +78,9 @@ protected function setNullForMissingKeys(array $leafCondition) | |
| */ | ||
| protected function getMatchTypes() | ||
| { | ||
| return array(self::EXACT_MATCH_TYPE, self::EXISTS_MATCH_TYPE, self::GREATER_THAN_MATCH_TYPE, | ||
| self::LESS_THAN_MATCH_TYPE, self::SUBSTRING_MATCH_TYPE); | ||
| return array(self::EXACT_MATCH_TYPE, self::EXISTS_MATCH_TYPE, self::GREATER_THAN_MATCH_TYPE, self::GREATER_THAN_EQUAL_TO_MATCH_TYPE, | ||
| self::LESS_THAN_MATCH_TYPE, self::LESS_THAN_EQUAL_TO_MATCH_TYPE, self::SUBSTRING_MATCH_TYPE, self::SEMVER_EQ, | ||
| self::SEMVER_GT, self::SEMVER_GE, self::SEMVER_LT, self::SEMVER_LE); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -87,8 +96,15 @@ protected function getEvaluatorByMatchType($matchType) | |
| $evaluatorsByMatchType[self::EXACT_MATCH_TYPE] = 'exactEvaluator'; | ||
| $evaluatorsByMatchType[self::EXISTS_MATCH_TYPE] = 'existsEvaluator'; | ||
| $evaluatorsByMatchType[self::GREATER_THAN_MATCH_TYPE] = 'greaterThanEvaluator'; | ||
| $evaluatorsByMatchType[self::GREATER_THAN_EQUAL_TO_MATCH_TYPE] = 'greaterThanEqualToEvaluator'; | ||
| $evaluatorsByMatchType[self::LESS_THAN_MATCH_TYPE] = 'lessThanEvaluator'; | ||
| $evaluatorsByMatchType[self::LESS_THAN_EQUAL_TO_MATCH_TYPE] = 'lessThanEqualToEvaluator'; | ||
| $evaluatorsByMatchType[self::SUBSTRING_MATCH_TYPE] = 'substringEvaluator'; | ||
| $evaluatorsByMatchType[self::SEMVER_EQ] = 'semverEqualEvaluator'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit. Alphabetize. |
||
| $evaluatorsByMatchType[self::SEMVER_GT] = 'semverGreaterThanEvaluator'; | ||
| $evaluatorsByMatchType[self::SEMVER_GE] = 'semverGreaterThanEqualToEvaluator'; | ||
| $evaluatorsByMatchType[self::SEMVER_LT] = 'semverLessThanEvaluator'; | ||
| $evaluatorsByMatchType[self::SEMVER_LE] = 'semverLessThanEqualToEvaluator'; | ||
|
|
||
| return $evaluatorsByMatchType[$matchType]; | ||
| } | ||
|
|
@@ -109,6 +125,35 @@ protected function isValueTypeValidForExactConditions($value) | |
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * compares two semantic versions. | ||
| * | ||
| * @param object $condition | ||
| * | ||
| * @return null|int 0 if user's semver attribute is equal to the semver condition value, | ||
| * 1 if user's semver attribute is greater than the semver condition value, | ||
| * -1 if user's semver attribute is less than the semver condition value, | ||
|
||
| * null if the condition value or user attribute value has an invalid type, or | ||
| * if there is a mismatch between the user attribute type and the condition | ||
| * value type. | ||
| */ | ||
| protected function semverEvaluator($condition) | ||
| { | ||
| $conditionName = $condition['name']; | ||
| $conditionValue = $condition['value']; | ||
| $userValue = isset($this->userAttributes[$conditionName]) ? $this->userAttributes[$conditionName] : null; | ||
|
|
||
| if (!Validator::validateNonEmptyString($conditionValue)) { | ||
|
||
| $this->logger->log(Logger::WARNING, sprintf( | ||
| logs::ATTRIBUTE_FORMAT_INVALID | ||
| )); | ||
| return null; | ||
| } | ||
|
|
||
| $semVerCondEval = new SemVersionConditionEvaluator($userValue, $this->logger); | ||
| return $semVerCondEval->compareVersion($conditionValue); | ||
| } | ||
|
|
||
| /** | ||
| * Evaluate the given exact match condition for the given user attributes. | ||
| * | ||
|
|
@@ -124,7 +169,7 @@ protected function exactEvaluator($condition) | |
| { | ||
| $conditionName = $condition['name']; | ||
| $conditionValue = $condition['value']; | ||
| $userValue = isset($this->userAttributes[$conditionName]) ? $this->userAttributes[$conditionName]: null; | ||
| $userValue = isset($this->userAttributes[$conditionName]) ? $this->userAttributes[$conditionName] : null; | ||
|
|
||
| if (!$this->isValueTypeValidForExactConditions($conditionValue) || | ||
| ((is_int($conditionValue) || is_float($conditionValue)) && !Validator::isFiniteNumber($conditionValue))) { | ||
|
|
@@ -189,7 +234,7 @@ protected function greaterThanEvaluator($condition) | |
| { | ||
| $conditionName = $condition['name']; | ||
| $conditionValue = $condition['value']; | ||
| $userValue = isset($this->userAttributes[$conditionName]) ? $this->userAttributes[$conditionName]: null; | ||
| $userValue = isset($this->userAttributes[$conditionName]) ? $this->userAttributes[$conditionName] : null; | ||
|
|
||
| if (!Validator::isFiniteNumber($conditionValue)) { | ||
| $this->logger->log(Logger::WARNING, sprintf( | ||
|
|
@@ -221,6 +266,52 @@ protected function greaterThanEvaluator($condition) | |
| return $userValue > $conditionValue; | ||
| } | ||
|
|
||
| /** | ||
| * Evaluate the given greater than equal to match condition for the given user attributes. | ||
| * | ||
| * @param object $condition | ||
| * | ||
| * @return boolean true if the user attribute value is greater than or equal to the condition value, | ||
| * false if the user attribute value is less than the condition value, | ||
| * null if the condition value isn't a number or the user attribute value | ||
| * isn't a number. | ||
| */ | ||
| protected function greaterThanEqualToEvaluator($condition) | ||
| { | ||
| $conditionName = $condition['name']; | ||
| $conditionValue = $condition['value']; | ||
| $userValue = isset($this->userAttributes[$conditionName]) ? $this->userAttributes[$conditionName] : null; | ||
|
|
||
| if (!Validator::isFiniteNumber($conditionValue)) { | ||
| $this->logger->log(Logger::WARNING, sprintf( | ||
| logs::UNKNOWN_CONDITION_VALUE, | ||
| json_encode($condition) | ||
| )); | ||
| return null; | ||
| } | ||
|
|
||
| if (!(is_int($userValue) || is_float($userValue))) { | ||
| $this->logger->log(Logger::WARNING, sprintf( | ||
| logs::UNEXPECTED_TYPE, | ||
| json_encode($condition), | ||
| gettype($userValue), | ||
| $conditionName | ||
| )); | ||
| return null; | ||
| } | ||
|
|
||
| if (!Validator::isFiniteNumber($userValue)) { | ||
| $this->logger->log(Logger::WARNING, sprintf( | ||
| logs::INFINITE_ATTRIBUTE_VALUE, | ||
| json_encode($condition), | ||
| $conditionName | ||
| )); | ||
| return null; | ||
| } | ||
|
|
||
| return $userValue >= $conditionValue; | ||
| } | ||
|
|
||
| /** | ||
| * Evaluate the given less than match condition for the given user attributes. | ||
| * | ||
|
|
@@ -235,7 +326,7 @@ protected function lessThanEvaluator($condition) | |
| { | ||
| $conditionName = $condition['name']; | ||
| $conditionValue = $condition['value']; | ||
| $userValue = isset($this->userAttributes[$conditionName]) ? $this->userAttributes[$conditionName]: null; | ||
| $userValue = isset($this->userAttributes[$conditionName]) ? $this->userAttributes[$conditionName] : null; | ||
|
|
||
| if (!Validator::isFiniteNumber($conditionValue)) { | ||
| $this->logger->log(Logger::WARNING, sprintf( | ||
|
|
@@ -267,7 +358,53 @@ protected function lessThanEvaluator($condition) | |
| return $userValue < $conditionValue; | ||
| } | ||
|
|
||
| /** | ||
| /** | ||
| * Evaluate the given less than equal to match condition for the given user attributes. | ||
| * | ||
| * @param object $condition | ||
| * | ||
| * @return boolean true if the user attribute value is less than or equal to the condition value, | ||
| * false if the user attribute value is greater than the condition value, | ||
| * null if the condition value isn't a number or the user attribute value | ||
| * isn't a number. | ||
| */ | ||
| protected function lessThanEqualToEvaluator($condition) | ||
| { | ||
| $conditionName = $condition['name']; | ||
| $conditionValue = $condition['value']; | ||
| $userValue = isset($this->userAttributes[$conditionName]) ? $this->userAttributes[$conditionName] : null; | ||
|
|
||
| if (!Validator::isFiniteNumber($conditionValue)) { | ||
| $this->logger->log(Logger::WARNING, sprintf( | ||
| logs::UNKNOWN_CONDITION_VALUE, | ||
| json_encode($condition) | ||
| )); | ||
| return null; | ||
| } | ||
|
|
||
| if (!(is_int($userValue) || is_float($userValue))) { | ||
| $this->logger->log(Logger::WARNING, sprintf( | ||
| logs::UNEXPECTED_TYPE, | ||
| json_encode($condition), | ||
| gettype($userValue), | ||
| $conditionName | ||
| )); | ||
| return null; | ||
| } | ||
|
|
||
| if (!Validator::isFiniteNumber($userValue)) { | ||
| $this->logger->log(Logger::WARNING, sprintf( | ||
| logs::INFINITE_ATTRIBUTE_VALUE, | ||
| json_encode($condition), | ||
| $conditionName | ||
| )); | ||
| return null; | ||
| } | ||
|
|
||
| return $userValue <= $conditionValue; | ||
| } | ||
|
|
||
| /** | ||
| * Evaluate the given substring than match condition for the given user attributes. | ||
| * | ||
| * @param object $condition | ||
|
|
@@ -281,7 +418,7 @@ protected function substringEvaluator($condition) | |
| { | ||
| $conditionName = $condition['name']; | ||
| $conditionValue = $condition['value']; | ||
| $userValue = isset($this->userAttributes[$conditionName]) ? $this->userAttributes[$conditionName]: null; | ||
| $userValue = isset($this->userAttributes[$conditionName]) ? $this->userAttributes[$conditionName] : null; | ||
|
|
||
| if (!is_string($conditionValue)) { | ||
| $this->logger->log(Logger::WARNING, sprintf( | ||
|
|
@@ -304,6 +441,96 @@ protected function substringEvaluator($condition) | |
| return strpos($userValue, $conditionValue) !== false; | ||
| } | ||
|
|
||
| /** | ||
| * Evaluate the given semantic version equal match condition for the given user attributes. | ||
| * | ||
| * @param object $condition | ||
| * | ||
| * @return boolean true if user's semver attribute is equal to the semver condition value, | ||
| * false if the user's semver is greater or less than the semver condition value, | ||
| * null if the semver condition value or user's semver attribute is invalid. | ||
| */ | ||
| protected function semverEqualEvaluator($condition) | ||
| { | ||
| $comparison = $this->semverEvaluator($condition); | ||
| if ($comparison !== null) { | ||
|
||
| return $comparison === 0; | ||
| } | ||
| return $comparison; | ||
| } | ||
|
|
||
| /** | ||
| * Evaluate the given semantic version greater than match condition for the given user attributes. | ||
| * | ||
| * @param object $condition | ||
| * | ||
| * @return boolean true if user's semver attribute is greater than the semver condition value, | ||
|
||
| * false if the user's semver is less than or equal to the semver condition value, | ||
| * null if the semver condition value or user's semver attribute is invalid. | ||
| */ | ||
| protected function semverGreaterThanEvaluator($condition) | ||
| { | ||
| $comparison = $this->semverEvaluator($condition); | ||
| if ($comparison !== null) { | ||
| return $comparison > 0; | ||
| } | ||
| return $comparison; | ||
| } | ||
|
|
||
| /** | ||
| * Evaluate the given semantic version greater than equal to match condition for the given user attributes. | ||
| * | ||
| * @param object $condition | ||
| * | ||
| * @return boolean true if user's semver attribute is greater than or equal to the semver condition value, | ||
| * false if the user's semver is less than the semver condition value, | ||
| * null if the semver condition value or user's semver attribute is invalid. | ||
| */ | ||
| protected function semverGreaterThanEqualToEvaluator($condition) | ||
| { | ||
| $comparison = $this->semverEvaluator($condition); | ||
| if ($comparison !== null) { | ||
| return $comparison >= 0; | ||
| } | ||
| return $comparison; | ||
| } | ||
|
|
||
| /** | ||
| * Evaluate the given semantic version less than match condition for the given user attributes. | ||
| * | ||
| * @param object $condition | ||
| * | ||
| * @return boolean true if user's semver attribute is less than the semver condition value, | ||
| * false if the user's semver is greater than or equal to the semver condition value, | ||
| * null if the semver condition value or user's semver attribute is invalid. | ||
| */ | ||
| protected function semverLessThanEvaluator($condition) | ||
| { | ||
| $comparison = $this->semverEvaluator($condition); | ||
| if ($comparison !== null) { | ||
| return $comparison < 0; | ||
| } | ||
| return $comparison; | ||
| } | ||
|
|
||
| /** | ||
| * Evaluate the given semantic version less than equal to match condition for the given user attributes. | ||
| * | ||
| * @param object $condition | ||
| * | ||
| * @return boolean true if user's semver attribute is less than or equal to the semver condition value, | ||
| * false if the user's semver is greater than the semver condition value, | ||
| * null if the semver condition value or user's semver attribute is invalid. | ||
| */ | ||
| protected function semverLessThanEqualToEvaluator($condition) | ||
| { | ||
| $comparison = $this->semverEvaluator($condition); | ||
| if ($comparison !== null) { | ||
| return $comparison <= 0; | ||
| } | ||
| return $comparison; | ||
| } | ||
|
|
||
| /** | ||
| * Function to evaluate audience conditions against user's attributes. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be made nicer by having these on separate lines and alphabetized.