public function TripalWebServiceResource::addProperty

3.x TripalWebServiceResource.inc public TripalWebServiceResource::addProperty($key, $value)

Adds a new key/value pair to the web serivces response.

The value must either be a scalar or another TripalWebServiceResource object. If the same key is usesd multiple times then the resulting resource will be presented as an array of elements.

Parameters

unknown $key: The name of the $key to add. This key must already be present in the web service context by first adding it using the addContextItem() member function.

unknown $value: The value of the key which must either be a scalar or a TripalWebServiceResource instance.

File

tripal_ws/includes/TripalWebServiceResource.inc, line 286

Class

TripalWebServiceResource

Code

public function addProperty($key, $value) {

  $this->checkKey($key);
  $this->checkValue($value);

  // If this is a numeric keyed array then add each item.
  if (is_array($value) and count(array_filter(array_keys($value), 'is_int')) == count(array_keys($value))) {
    if (!array_key_exists($key, $this->data)) {
      $this->data[$key] = array();
    }
    foreach ($value as $item) {
      $this->addProperty($key, $item);
    }
    return;
  }

  // If this key doesn't exist then add the value directly to the key.
  if (!array_key_exists($key, $this->data)) {
    $this->data[$key] = $value;
  }
  // Else if it does exist then we need to make sure that the element is
  // an array and add it.
  else {
    // If this is the second element, then convert it to an array.  The
    // second test in the condition below is for for a numeric array.
    if (!is_array($this->data[$key]) or count(array_filter(array_keys($this->data[$key]), 'is_string')) > 0) {
      $element = $this->data[$key];
      $this->data[$key] = array();
      $this->data[$key][] = $element;
    }
    $this->data[$key][] = $value;
  }
}