• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

optimizely / php-sdk / 4536635044

pending completion
4536635044

Pull #265

github

GitHub
Merge 019774507 into 34bebf03a
Pull Request #265: [FSSDK-8940] Correct return type hints

2871 of 2957 relevant lines covered (97.09%)

63.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.88
/src/Optimizely/UserProfile/UserProfileUtils.php
1
<?php
2
/**
3
 * Copyright 2017, Optimizely Inc and Contributors
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\UserProfile;
19

20
class UserProfileUtils
21
{
22
    const USER_ID_KEY = 'user_id';
23

24
    const EXPERIMENT_BUCKET_MAP_KEY = 'experiment_bucket_map';
25

26
    const VARIATION_ID_KEY = 'variation_id';
27

28
    /**
29
     * Grab the revenue value from the event tags. "revenue" is a reserved keyword.
30
     *
31
     * @param  $userProfileMap array Representing the user profile.
32
     * @return true if the given user profile map is valid, false otherwise.
33
     */
34
    public static function isValidUserProfileMap($userProfileMap)
35
    {
36
        if (!is_array($userProfileMap)) {
5✔
37
            return false;
1✔
38
        }
39

40
        if (!isset($userProfileMap[self::USER_ID_KEY])) {
5✔
41
            return false;
1✔
42
        }
43

44
        if (!isset($userProfileMap[self::EXPERIMENT_BUCKET_MAP_KEY])) {
5✔
45
            return false;
×
46
        }
47

48
        if (!is_array($userProfileMap[self::EXPERIMENT_BUCKET_MAP_KEY])) {
5✔
49
            return false;
×
50
        }
51

52
        // validate the experiment bucket map
53
        $experimentBucketMap = $userProfileMap[self::EXPERIMENT_BUCKET_MAP_KEY];
5✔
54
        foreach ($experimentBucketMap as $experimentId => $decision) {
5✔
55
            if (!is_array($decision)) {
4✔
56
                return false;
×
57
            }
58

59
            // make sure that there is a variation ID property in every decision
60
            if (!isset($decision[self::VARIATION_ID_KEY])) {
4✔
61
                return false;
×
62
            }
63
        }
5✔
64

65
        // Looks good to me
66
        return true;
5✔
67
    }
68

69
    /**
70
     * Convert the given user profile map into a UserProfile object.
71
     *
72
     * @param $userProfileMap array
73
     *
74
     * @return UserProfile The user profile object constructed from the given map.
75
     */
76
    public static function convertMapToUserProfile($userProfileMap)
77
    {
78
        $userId = $userProfileMap[self::USER_ID_KEY];
5✔
79
        $experimentBucketMap = array();
5✔
80
        foreach ($userProfileMap[self::EXPERIMENT_BUCKET_MAP_KEY] as $experimentId => $decisionMap) {
5✔
81
            $variationId = $decisionMap[self::VARIATION_ID_KEY];
4✔
82
            $experimentBucketMap[$experimentId] = new Decision($variationId);
4✔
83
        }
5✔
84

85
        return new UserProfile($userId, $experimentBucketMap);
5✔
86
    }
87

88
    /**
89
     * Convert the given UserProfile object into a map.
90
     *
91
     * @param $userProfile UserProfile The user profile object to convert to a map.
92
     *
93
     * @return array The map representing the user profile object.
94
     */
95
    public static function convertUserProfileToMap($userProfile)
96
    {
97
        $userProfileMap = array(
98
            self::USER_ID_KEY => $userProfile->getUserId(),
6✔
99
            self::EXPERIMENT_BUCKET_MAP_KEY => array()
6✔
100
        );
6✔
101
        $experimentBucketMap = $userProfile->getExperimentBucketMap();
6✔
102
        foreach ($experimentBucketMap as $experimentId => $decision) {
6✔
103
            $userProfileMap[self::EXPERIMENT_BUCKET_MAP_KEY][$experimentId] = array(
6✔
104
                    self::VARIATION_ID_KEY => $decision->getVariationId()
6✔
105
                );
6✔
106
        }
6✔
107
        return $userProfileMap;
6✔
108
    }
109
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc