/**
  * Extracts the value from the result using Collection::getPath. Also adds some additional logic for keys that need
  * to access n-1 indexes (e.g., ImportExport, Kinesis). The n-1 logic only works for the known cases. We will switch
  * to a jmespath implementation in the future to cover all cases
  *
  * @param Model  $result
  * @param string $key
  *
  * @return mixed|null
  */
 protected function getValueFromResult(Model $result, $key)
 {
     // Special handling for keys that need to access n-1 indexes
     if (strpos($key, '#') !== false) {
         $keyParts = explode('#', $key, 2);
         $items = $result->getPath(trim($keyParts[0], '/'));
         if ($items && is_array($items)) {
             $index = count($items) - 1;
             $key = strtr($key, array('#' => $index));
         }
     }
     // Get the value
     return $result->getPath($key);
 }
Example #2
0
 /**
  * Check to see if the path of the output key is satisfied by the value
  *
  * @param Model  $model      Result model
  * @param string $key        Key to check
  * @param string $checkValue Compare the key to the value
  * @param bool   $all        Set to true to ensure all value match or false to only match one
  *
  * @return bool
  */
 protected function checkPath(Model $model, $key = null, $checkValue = array(), $all = true)
 {
     // If no key is set, then just assume true because the request succeeded
     if (!$key) {
         return true;
     }
     if (!($result = $model->getPath($key))) {
         return false;
     }
     $total = $matches = 0;
     foreach ((array) $result as $value) {
         $total++;
         foreach ((array) $checkValue as $check) {
             if ($value == $check) {
                 $matches++;
                 break;
             }
         }
     }
     // When matching all values, ensure that the match count matches the total count
     if ($all && $total != $matches) {
         return false;
     }
     return $matches > 0;
 }