|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2024-2026 Wingify Software Pvt. Ltd. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +namespace wingify\Packages\SegmentationEvaluator\Utils; |
| 20 | + |
| 21 | +use wingify\Models\User\ContextModel; |
| 22 | +use wingify\Services\ServiceContainer; |
| 23 | +use wingify\Enums\ApiEnum; |
| 24 | +use wingify\Utils\DataTypeUtil; |
| 25 | + |
| 26 | +class WebTestingSegmentUtil { |
| 27 | + /** |
| 28 | + * Normalizes Web Testing campaign map keys and variation values to strings. |
| 29 | + * @param array|object $rawAssignments - The raw assignments map from the context. |
| 30 | + * @return array - The normalized assignments map with campaignId as key and variationId as value. |
| 31 | + */ |
| 32 | + public static function normalizeWebTestingCampaignsMap($rawAssignments): array { |
| 33 | + // Turn the raw assignments map into a simple string map for regex matching. |
| 34 | + $campaignIdToVariationId = []; |
| 35 | + $iterableAssignments = is_object($rawAssignments) ? get_object_vars($rawAssignments) : $rawAssignments; |
| 36 | + |
| 37 | + foreach ($iterableAssignments as $campaignId => $assignedVariationId) { |
| 38 | + if ( |
| 39 | + !is_null($assignedVariationId) && |
| 40 | + strlen((string)$campaignId) > 0 |
| 41 | + // Ignore empty keys; null/undefined variations mean nothing assigned for that id. |
| 42 | + ) { |
| 43 | + $campaignIdToVariationId[(string)$campaignId] = (string)$assignedVariationId; |
| 44 | + } |
| 45 | + } |
| 46 | + return $campaignIdToVariationId; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Parses `context.platformVariables.webTestingCampaigns` (JSON string or plain object). |
| 51 | + */ |
| 52 | + public static function parseWebTestingCampaignsFromContext( |
| 53 | + ContextModel $context, |
| 54 | + ServiceContainer $serviceContainer |
| 55 | + ): ?array { |
| 56 | + $platformVariables = $context->getPlatformVariables(); |
| 57 | + $webTestingCampaignsInput = null; |
| 58 | + if (is_array($platformVariables) && isset($platformVariables['webTestingCampaigns'])) { |
| 59 | + $webTestingCampaignsInput = $platformVariables['webTestingCampaigns']; |
| 60 | + } elseif (is_object($platformVariables) && isset($platformVariables->webTestingCampaigns)) { |
| 61 | + $webTestingCampaignsInput = $platformVariables->webTestingCampaigns; |
| 62 | + } |
| 63 | + |
| 64 | + // No payload from the integration means empty assignments map. |
| 65 | + if (is_null($webTestingCampaignsInput)) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + // SDK already forwarded a plain campaignId -> variationId object. |
| 70 | + if (is_array($webTestingCampaignsInput) || is_object($webTestingCampaignsInput)) { |
| 71 | + return self::normalizeWebTestingCampaignsMap($webTestingCampaignsInput); |
| 72 | + } |
| 73 | + |
| 74 | + // Some stacks pass JSON text (cookie, SSR prop, tag); parse it only if it's an object. |
| 75 | + if (DataTypeUtil::isString($webTestingCampaignsInput)) { |
| 76 | + $trimmedWebTestingCampaignsJson = trim($webTestingCampaignsInput); |
| 77 | + if ($trimmedWebTestingCampaignsJson === '') { |
| 78 | + // Empty JSON string is invalid. |
| 79 | + return null; |
| 80 | + } |
| 81 | + try { |
| 82 | + // extract all "key": tokens and check for duplicates before parsing swallows them |
| 83 | + preg_match_all('/"([^"\\\\]*)"\s*:/', $trimmedWebTestingCampaignsJson, $matches); |
| 84 | + if (!empty($matches[1])) { |
| 85 | + $campaignIds = $matches[1]; |
| 86 | + $hasDuplicateCampaignId = count($campaignIds) !== count(array_unique($campaignIds)); |
| 87 | + if ($hasDuplicateCampaignId) { |
| 88 | + $serviceContainer |
| 89 | + ->getLoggerService() |
| 90 | + ->error( |
| 91 | + 'INVALID_WEB_TESTING_CAMPAIGNS_DUPLICATE_KEY', |
| 92 | + ['an' => ApiEnum::GET_FLAG, 'uuid' => $context->getUUID(), 'sId' => $context->getSessionId()] |
| 93 | + ); |
| 94 | + } |
| 95 | + } |
| 96 | + // Parse the JSON string into an object. |
| 97 | + $parsedAssignments = json_decode($trimmedWebTestingCampaignsJson, true); |
| 98 | + if (json_last_error() === JSON_ERROR_NONE && (is_object(json_decode($trimmedWebTestingCampaignsJson)) || is_array($parsedAssignments))) { |
| 99 | + return self::normalizeWebTestingCampaignsMap($parsedAssignments); |
| 100 | + } |
| 101 | + // Parsed fine but it's an array/string/etc. Invalid shape. |
| 102 | + $serviceContainer |
| 103 | + ->getLoggerService() |
| 104 | + ->error( |
| 105 | + 'INVALID_WEB_TESTING_CAMPAIGNS_JSON', |
| 106 | + ['an' => ApiEnum::GET_FLAG, 'uuid' => $context->getUUID(), 'sId' => $context->getSessionId()] |
| 107 | + ); |
| 108 | + } catch (\Exception $e) { |
| 109 | + // Malformed JSON; treat like missing assignments. |
| 110 | + $serviceContainer |
| 111 | + ->getLoggerService() |
| 112 | + ->error( |
| 113 | + 'INVALID_WEB_TESTING_CAMPAIGNS_JSON', |
| 114 | + ['an' => ApiEnum::GET_FLAG, 'uuid' => $context->getUUID(), 'sId' => $context->getSessionId()] |
| 115 | + ); |
| 116 | + } |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + // Booleans/numbers/other odd types are invalid. |
| 121 | + if (!is_null($webTestingCampaignsInput)) { |
| 122 | + $kind = strtolower(gettype($webTestingCampaignsInput)); |
| 123 | + $serviceContainer |
| 124 | + ->getLoggerService() |
| 125 | + ->error( |
| 126 | + 'INVALID_WEB_TESTING_CAMPAIGNS_TYPE', |
| 127 | + ['kind' => $kind, 'an' => ApiEnum::GET_FLAG, 'uuid' => $context->getUUID(), 'sId' => $context->getSessionId()] |
| 128 | + ); |
| 129 | + } |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Evaluates campaignVariation operand encoding: |
| 135 | + * - "!C" — user is not in campaign C (no entry in map) |
| 136 | + * - "C_!V" — user is in campaign C and assigned variation is not V |
| 137 | + * - "C_V" — user is in campaign C with variation V |
| 138 | + * - "C" (digits only) — user is in campaign C (any variation) |
| 139 | + */ |
| 140 | + public static function evaluateWebTestingCampaignVariation( |
| 141 | + string $campaignVariationOperand, |
| 142 | + ?array $assignedVariationsByCampaignId |
| 143 | + ): array { |
| 144 | + // Null means empty assignments map. |
| 145 | + $assignments = $assignedVariationsByCampaignId ?? []; |
| 146 | + |
| 147 | + // !123 — user should not be in campaign 123. |
| 148 | + if (preg_match('/^!(\d+)$/', $campaignVariationOperand, $match)) { |
| 149 | + $campaignId = $match[1]; |
| 150 | + return ['result' => !array_key_exists($campaignId, $assignments), 'invalidFormat' => false]; |
| 151 | + } |
| 152 | + |
| 153 | + // 123_!4 — in campaign 123 but not the variation 4. |
| 154 | + if (preg_match('/^(\d+)_!(\d+)$/', $campaignVariationOperand, $match)) { |
| 155 | + $campaignId = $match[1]; |
| 156 | + $variationId = $match[2]; |
| 157 | + if (!array_key_exists($campaignId, $assignments)) { |
| 158 | + return ['result' => false, 'invalidFormat' => false]; |
| 159 | + } |
| 160 | + return ['result' => $assignments[$campaignId] !== $variationId, 'invalidFormat' => false]; |
| 161 | + } |
| 162 | + |
| 163 | + // 123_4 — must be exactly that campaign and variation. |
| 164 | + if (preg_match('/^(\d+)_(\d+)$/', $campaignVariationOperand, $match)) { |
| 165 | + $campaignId = $match[1]; |
| 166 | + $variationId = $match[2]; |
| 167 | + if (!array_key_exists($campaignId, $assignments)) { |
| 168 | + return ['result' => false, 'invalidFormat' => false]; |
| 169 | + } |
| 170 | + return ['result' => $assignments[$campaignId] === $variationId, 'invalidFormat' => false]; |
| 171 | + } |
| 172 | + |
| 173 | + // 123 — in the campaign, any variation counts. |
| 174 | + if (preg_match('/^(\d+)$/', $campaignVariationOperand, $match)) { |
| 175 | + $campaignId = $match[1]; |
| 176 | + return ['result' => array_key_exists($campaignId, $assignments), 'invalidFormat' => false]; |
| 177 | + } |
| 178 | + |
| 179 | + // Invalid format. |
| 180 | + return ['result' => false, 'invalidFormat' => true]; |
| 181 | + } |
| 182 | +} |
0 commit comments