protected function getObject($item, $memory) {
    $result = new \stdClass();
    $result->properties = array();

    // Add itemtype.
    if ($itemtype = $item->itemType()) {
      $result->type = $itemtype;
    }
    // Add itemid. 
    if ($itemid = $item->itemid()) {
      $result->id = $itemid;
    }
    // Add properties.
    foreach ($item->properties() as $elem) {
      if ($elem->itemScope()) {
        // Cannot use in_array() for comparison when values are arrays, so
        // iterate and check for equality with each item in memory.
        foreach ($memory as $memory_item) {
          if ($memory_item === $elem) {
            $value = 'ERROR';
          }
        }
        // If the item is not in memory, there are no cycles, and thus no error.
        // Recurse into the item to build out its properties.
        if (!isset($value)) {
          $memory[] = $item;
          $value = $this->getObject($elem, $memory);
          array_pop($memory);
        }
      }
      else {
        $value = $elem->itemValue();
      }
      foreach ($elem->itemProp() as $prop) {
        $result->properties[$prop][] = $value;
      }

      $value = NULL;
    }
    return $result;
  }