|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2016, Optimizely |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace Optimizely\Utils; |
| 19 | + |
| 20 | +class ConditionEvaluator |
| 21 | +{ |
| 22 | + /** |
| 23 | + * const string Representing AND operator. |
| 24 | + */ |
| 25 | + const AND_OPERATOR = 'and'; |
| 26 | + |
| 27 | + /** |
| 28 | + * const string Representing OR operator. |
| 29 | + */ |
| 30 | + const OR_OPERATOR = 'or'; |
| 31 | + |
| 32 | + /** |
| 33 | + * const string Representing NOT operator. |
| 34 | + */ |
| 35 | + const NOT_OPERATOR = 'not'; |
| 36 | + |
| 37 | + /** |
| 38 | + * @param $conditions array Audience conditions list. |
| 39 | + * @param $userAttributes array Associative array of user attributes to values. |
| 40 | + * |
| 41 | + * @return boolean True if all conditions evaluate to True. |
| 42 | + */ |
| 43 | + private function andEvaluator($conditions, $userAttributes) |
| 44 | + { |
| 45 | + forEach ($conditions as $condition) { |
| 46 | + $result = $this->evaluate($condition, $userAttributes); |
| 47 | + if (!$result) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return true; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @param $conditions array Audience conditions list. |
| 57 | + * @param $userAttributes array Associative array of user attributes to values. |
| 58 | + * |
| 59 | + * @return boolean True if any one of the conditions evaluate to True. |
| 60 | + */ |
| 61 | + private function orEvaluator($conditions, $userAttributes) |
| 62 | + { |
| 63 | + forEach ($conditions as $condition) { |
| 64 | + $result = $this->evaluate($condition, $userAttributes); |
| 65 | + if ($result) { |
| 66 | + return true; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param $condition array Audience conditions list consisting of single condition. |
| 75 | + * @param $userAttributes array Associative array of user attributes to values. |
| 76 | + * |
| 77 | + * @return boolean True if the condition evaluates to False. |
| 78 | + */ |
| 79 | + private function notEvaluator($condition, $userAttributes) |
| 80 | + { |
| 81 | + if (count($condition) != 1) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + return !$this->evaluate($condition[0], $userAttributes); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Function to evaluate audience conditions against user's attributes. |
| 90 | + * |
| 91 | + * @param $conditions array Nested array of and/or/not conditions representing the audience conditions. |
| 92 | + * @param $userAttributes array Associative array of user attributes to values. |
| 93 | + * |
| 94 | + * @return boolean Representing if audience conditions are satisfied or not. |
| 95 | + */ |
| 96 | + public function evaluate($conditions, $userAttributes) |
| 97 | + { |
| 98 | + if (is_array($conditions)) { |
| 99 | + switch ($conditions[0]) { |
| 100 | + case self::AND_OPERATOR: |
| 101 | + array_shift($conditions); |
| 102 | + return $this->andEvaluator($conditions, $userAttributes); |
| 103 | + case self::OR_OPERATOR: |
| 104 | + array_shift($conditions); |
| 105 | + return $this->orEvaluator($conditions, $userAttributes); |
| 106 | + case self::NOT_OPERATOR: |
| 107 | + array_shift($conditions); |
| 108 | + return $this->notEvaluator($conditions, $userAttributes); |
| 109 | + default: |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + $conditionName = $conditions->{'name'}; |
| 116 | + if (!isset($userAttributes[$conditionName])) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + return $userAttributes[$conditionName] == $conditions->{'value'}; |
| 120 | + } |
| 121 | +} |
0 commit comments