Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Optimizely/Enums/CommonAudienceEvaluationLogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
class CommonAudienceEvaluationLogs
{
const AUDIENCE_EVALUATION_RESULT = "Audience \"%s\" evaluated to %s.";
const ATTRIBUTE_FORMAT_INVALID = "Provided attributes are in an invalid format.";
const EVALUATING_AUDIENCE = "Starting to evaluate audience \"%s\" with conditions: %s.";
const INFINITE_ATTRIBUTE_VALUE = "Audience condition %s evaluated to UNKNOWN because the number value for user attribute \"%s\" is not in the range [-2^53, +2^53].";
const MISSING_ATTRIBUTE_VALUE = "Audience condition %s evaluated to UNKNOWN because no value was passed for user attribute \"%s\".";
Expand Down
245 changes: 236 additions & 9 deletions src/Optimizely/Utils/CustomAttributeConditionEvaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use Monolog\Logger;
use Optimizely\Enums\CommonAudienceEvaluationLogs as logs;
use Optimizely\Utils\SemVersionConditionEvaluator;
use Optimizely\Utils\Validator;

class CustomAttributeConditionEvaluator
Expand All @@ -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;

/**
Expand All @@ -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;
Expand All @@ -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);
Copy link
Contributor

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.

}

/**
Expand All @@ -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';
Copy link
Contributor

Choose a reason for hiding this comment

The 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];
}
Expand All @@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User has a version attribute. Semver is a specification.

* 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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$userValue should be checked here instead of $conditionValue. And a unit test written if not already.

$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.
*
Expand All @@ -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))) {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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.
*
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit. Will be more readable if you do:
if ($comparison === null) { return null } return $comparison === 0
For all others too

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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit. version attribute. Please update at all places.

* 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.
*
Expand Down
Loading