You are here

public function AcquiaLiftAPI::getPossibleValues in Acquia Lift Connector 7

Gets array of possible targeting values in the format expected by the plugin.

Parameters

$agent_name: The name of the agent to get targeting values for.

$mutex_separator: The string used as a separator for mutually exclusive context values. This parameter is provided so that the logic can be unit tested.

$non_mutex_separator: The string used as a separator for non-mutually exclusive context values. This parameter is provided so that the logic can be unit tested.

Return value

array An associative array of targeting values with feature names as keys and an associative array structured as follows for each value: array( 'mutex' => TRUE, // If values for this feature type are mutually exclusive. 'values' => array( 'some-feature-value-code' => 'Some feature value friendly name' ) )

File

includes/acquia_lift.classes.inc, line 1059
Provides an agent type for Acquia Lift

Class

AcquiaLiftAPI
@file Provides an agent type for Acquia Lift

Code

public function getPossibleValues($agent_name, $mutex_separator = NULL, $non_mutex_separator = NULL) {
  $possible_values = array();

  // We make these separators injectable so that we can write unit tests to test the
  // logic here.
  if (empty($mutex_separator)) {
    $mutex_separator = self::FEATURE_STRING_SEPARATOR_MUTEX;
  }
  if (empty($non_mutex_separator)) {
    $non_mutex_separator = self::FEATURE_STRING_SEPARATOR_NONMUTEX;
  }
  $result = $this
    ->getPotentialTargetingValues($agent_name);
  if (isset($result['data']['potential']['features']) && !empty($result['data']['potential']['features'])) {

    // To determine whether values are mutually exclusive we need to check whether
    // they contain the separator that designates them as such, but the separator that
    // designates them as non-mutex could be contained in this so we need some logic
    // here to figure out how to check for this.
    $check = $non_mutex_separator;
    if (strpos($mutex_separator, $non_mutex_separator) !== FALSE) {

      // The mutex separator contains the non-mutex separator so we should use that
      // for our check.
      $check = $mutex_separator;
    }
    foreach ($result['data']['potential']['features'] as $feature) {
      $code = $feature['code'];

      // This logic is seriously gnarly. The absence of the string we check for signifies
      // something different depending on what that string was. E.g. if we are checking for
      // the presence of the non-mutex separator, then its absence means we are dealing with
      // mutex values.
      $mutex = strpos($code, $check) === FALSE ? $check === $non_mutex_separator : $check === $mutex_separator;
      $separator = $mutex ? $mutex_separator : $non_mutex_separator;
      $separated = explode($separator, $code);
      if (count($separated) !== 2) {
        continue;
      }
      list($name, $value) = $separated;
      $name = trim($name);
      $value = trim($value);
      $friendly_name = isset($feature['typeName']) ? $feature['typeName'] : $name;
      if (!isset($possible_values[$name])) {
        $possible_values[$name] = array(
          'value type' => 'predefined',
          'mutex' => $mutex,
          'friendly name' => $friendly_name,
          'values' => array(),
        );
      }
      $possible_values[$name]['values'][$value] = $feature['name'];
    }
  }
  return $possible_values;
}