|
36 | 36 | interface IVWOClient { |
37 | 37 | public function getFlag(string $featureKey, $context); |
38 | 38 | public function trackEvent(string $eventName, $context, array $eventProperties); |
39 | | - public function setAttribute(string $attributeKey, string $attributeValue, $context); |
| 39 | + public function setAttribute($attributesOrAttributeValue, $attributeValueOrContext, $context); |
40 | 40 | } |
41 | 41 |
|
42 | 42 | class VWOClient implements IVWOClient { |
@@ -141,31 +141,81 @@ public function trackEvent($eventName = null, $context = null, $eventProperties |
141 | 141 | } |
142 | 142 | } |
143 | 143 |
|
144 | | - public function setAttribute($attributeKey = null, $attributeValue = null, $context = null) { |
| 144 | + public function setAttribute($attributesOrAttributeValue = null , $attributeValueOrContext = null, $context = null) { |
145 | 145 | $apiName = 'setAttribute'; |
146 | 146 |
|
147 | 147 | try { |
148 | 148 | LogManager::instance()->debug("API Called: $apiName"); |
149 | 149 |
|
150 | | - if (!DataTypeUtil::isString($attributeKey)) { |
151 | | - LogManager::instance()->error("Attribute key passed to $apiName API is not valid."); |
152 | | - throw new \TypeError('TypeError: attributeKey should be a valid string'); |
153 | | - } |
154 | | - |
155 | | - if (!DataTypeUtil::isString($attributeValue) && !DataTypeUtil::isNumber($attributeValue) && !DataTypeUtil::isBoolean($attributeValue)) { |
156 | | - LogManager::instance()->error("Attribute value passed to $apiName API is not valid."); |
| 150 | + if (DataTypeUtil::isString($attributesOrAttributeValue)) { |
| 151 | + // Validate attributeKey is a string |
| 152 | + if (!DataTypeUtil::isString($attributesOrAttributeValue)) { |
| 153 | + LogManager::instance()->error("Attribute key passed to $apiName API is not valid."); |
| 154 | + throw new \TypeError('TypeError: attributeKey should be a valid string'); |
| 155 | + } |
| 156 | + |
| 157 | + // Validate attributeValue (the second argument) is valid |
| 158 | + if (!DataTypeUtil::isString($attributeValueOrContext) && |
| 159 | + !DataTypeUtil::isNumber($attributeValueOrContext) && |
| 160 | + !DataTypeUtil::isBoolean($attributeValueOrContext)) { |
| 161 | + LogManager::instance()->error("Attribute value passed to $apiName API is not valid."); |
157 | 162 | throw new \TypeError('TypeError: attributeValue should be a valid string, number, or boolean'); |
| 163 | + } |
| 164 | + |
| 165 | + // Ensure context is valid |
| 166 | + if (!isset($context['id']) || empty($context['id'])) { |
| 167 | + LogManager::instance()->error('Context must contain a valid user ID.'); |
| 168 | + throw new \Error('TypeError: Invalid context'); |
| 169 | + } |
| 170 | + |
| 171 | + $contextModel = new ContextModel(); |
| 172 | + $contextModel->modelFromDictionary($context); |
| 173 | + |
| 174 | + // Create the attributes map from key-value |
| 175 | + $attributes = [$attributesOrAttributeValue => $attributeValueOrContext]; |
| 176 | + (new SetAttribute())->setAttribute($this->settings, $attributes, $contextModel); |
| 177 | + |
| 178 | + } else { |
| 179 | + // Case where attributeKey is an array (multiple attributes) |
| 180 | + $attributes = $attributesOrAttributeValue; |
| 181 | + |
| 182 | + // Validate attributes is an array |
| 183 | + if (!DataTypeUtil::isArray($attributes)) { |
| 184 | + LogManager::instance()->error("Attributes passed to $apiName API is not valid."); |
| 185 | + throw new \TypeError('TypeError: attributes should be an array'); |
| 186 | + } |
| 187 | + |
| 188 | + // Validate attributes is not empty |
| 189 | + if (empty($attributes)) { |
| 190 | + LogManager::instance()->error("Key 'attributesMap' passed to setAttribute API is not of valid type. Got type: null or empty array, should be: a non-empty array."); |
| 191 | + throw new \TypeError('TypeError: attributes should be a non-empty array'); |
| 192 | + } |
| 193 | + |
| 194 | + // Validate that each attribute value is of a supported type (string, number, or boolean) |
| 195 | + foreach ($attributes as $key => $value) { |
| 196 | + if (!is_string($key)) { |
| 197 | + LogManager::instance()->error("Attribute key in attributesMap is not valid. Got type: '" . gettype($key) . "', should be: string."); |
| 198 | + throw new \TypeError("TypeError: attribute key '$key' should only be a string"); |
| 199 | + } |
| 200 | + |
| 201 | + if (!DataTypeUtil::isString($value) && !DataTypeUtil::isNumber($value) && !DataTypeUtil::isBoolean($value)) { |
| 202 | + LogManager::instance()->error("Attribute value for key '$key' is not valid."); |
| 203 | + throw new \TypeError("TypeError: attributeValue for key '$key' should be a valid string, number, or boolean"); |
| 204 | + } |
| 205 | + } |
| 206 | + $context = $attributeValueOrContext; |
| 207 | + // Ensure context is valid |
| 208 | + if (!isset($context['id']) || empty($context['id'])) { |
| 209 | + LogManager::instance()->error('Context must contain a valid user ID.'); |
| 210 | + throw new \Error('TypeError: Invalid context'); |
| 211 | + } |
| 212 | + |
| 213 | + $contextModel = new ContextModel(); |
| 214 | + $contextModel->modelFromDictionary($context); |
| 215 | + |
| 216 | + // Proceed with setting the attributes if validation is successful |
| 217 | + (new SetAttribute())->setAttribute($this->settings, $attributes, $contextModel); |
158 | 218 | } |
159 | | - |
160 | | - if (!isset($context['id']) || empty($context['id'])) { |
161 | | - LogManager::instance()->error('Context must contain a valid user ID.'); |
162 | | - throw new \Error('TypeError: Invalid context'); |
163 | | - } |
164 | | - |
165 | | - $contextModel = new ContextModel(); |
166 | | - $contextModel->modelFromDictionary($context); |
167 | | - |
168 | | - (new SetAttribute())->setAttribute($this->settings, $attributeKey, $attributeValue, $contextModel); |
169 | 219 | } catch (\Throwable $error) { |
170 | 220 | LogManager::instance()->error("API - $apiName failed to execute. Error: " . $error->getMessage()); |
171 | 221 | } |
|
0 commit comments