/** * Specifies the data which should be serialized to JSON * @return scalar */ public function jsonSerialize() { if (!$this->links && $this->data === false && !$this->meta) { throw new JsonApiException('Could not get json value: A relationship MUST contain at least links, data or a meta top-level member'); } $value = array(); if ($this->links) { $value['links'] = $this->links; } if ($this->data !== false) { if (is_array($this->data)) { $value['data'] = array(); foreach ($this->data as $data) { $value['data'][] = $data->getJsonValue(false); } } elseif ($this->data) { $value['data'] = $this->data->getJsonValue(false); } else { $value['data'] = null; } } if ($this->meta) { $value['meta'] = $this->meta; } return $value; }
/** * Gets a representation of this element ready for JSON encoding * @param boolean $full Flag to see if the full version should be returned * @return scalar */ public function getJsonValue($full = true) { if ($this->data === false && !$this->errors && !$this->meta) { throw new JsonApiException('Could not get json value: A document MUST contain at least the data, errors or meta top-level member'); } $value = array('jsonapi' => array('version' => '1.0')); if ($this->links) { $value['links'] = $this->links; } if ($this->errors) { $value['errors'] = $this->errors; } elseif ($this->data !== false) { if (is_array($this->data)) { $value['data'] = array(); foreach ($this->data as $data) { $value['data'][] = $data->getJsonValue(true); } } elseif ($this->data) { $value['data'] = $this->data->getJsonValue(true); } else { $value['data'] = null; } if ($this->included) { $value['included'] = array(); foreach ($this->included as $type => $resources) { foreach ($resources as $resource) { $value['included'][] = $resource->getJsonValue(true); } } } } if ($this->meta) { $value['meta'] = $this->meta; } return $value; }