You are here

public function WSDecoderBase::getData in Web Service Data 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/WSDecoderBase.php \Drupal\wsdata\Plugin\WSDecoderBase::getData()

Retrieve the value for the given data key.

This function retrieves data from the structured array in $this->data using $key as a key. $key should be a string, with the character ':' delimiting the parts of the key. I.E. The key something:someplace with retrive $this->data['foo']['bar'] N.B. This function can be overridden to work with whatever the ->decode function is implemented to return.

Parameters

string $key: Optional - Data key to load.

string $lang: Optional - Language key.

Return value

mixed Returns the requested data, FALSE otherwise.

Overrides WSDecoderInterface::getData

File

src/Plugin/WSDecoderBase.php, line 88

Class

WSDecoderBase
Base class for Wsdecoder plugin plugins.

Namespace

Drupal\wsdata\Plugin

Code

public function getData($key = NULL, $lang = NULL) {
  if (!is_array($this->data)) {
    return $this->data;
  }
  $return_data = FALSE;

  // Paths to load data from.
  $paths = [];

  // First, see if we want a specific language.
  if ($this->languages) {
    if (!is_null($lang) and array_key_exists($lang, $this->data)) {
      $paths[$lang] = !empty($key) ? $lang . ':' . $key : $lang;
    }
    else {
      foreach ($this->languages as $lang) {
        $paths[$lang] = !empty($key) ? $lang . ':' . $key : $lang;
      }
    }
  }
  else {
    if (!empty($key)) {
      $paths[$key] = $key;
    }
  }

  // Get the raw data.
  $return_data = $this->data;

  // Simplest case, return all data.
  if (empty($paths)) {
    return $return_data;
  }

  // Second simplest case, one specific value.
  if (!empty($paths[$key])) {
    $location = explode(':', $paths[$key]);
    foreach ($location as $l) {
      if (isset($return_data[$l])) {
        $return_data = $return_data[$l];
      }
      else {
        $return_data = FALSE;
      }
    }
    return $return_data;
  }

  // Third case, one specific value in a given language.
  if (!empty($paths[$lang]) and count($paths) == 1) {
    $location = explode(':', $paths[$lang]);
    foreach ($location as $l) {
      if (isset($return_data[$l])) {
        $return_data = $return_data[$l];
      }
      else {
        $return_data = FALSE;
      }
    }

    // Language specific data is always keyed by the language.
    $return_data[$lang] = $return_data;
    return $return_data;
  }

  // Lastly, the complicated case. Keyed value for all languages.
  if ($this->languages and count($paths) > 1) {
    $keyed_data = [];
    foreach ($paths as $p => $path) {

      // Reset return data.
      $return_data = $this->data;
      $location = explode(':', $path);
      foreach ($location as $l) {
        if (isset($return_data[$l])) {
          $return_data = $return_data[$l];
        }
        else {
          $return_data = FALSE;
        }
      }
      $keyed_data[$p] = $return_data;
    }

    // Finally, put the keyed data back into the return data.
    return $keyed_data;
  }
}