/** * 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; }
/** * Creates a new resource * @param string $type Name of the resource type * @param string $id Id of the resource * @param string $relationshipPath dot-separated list of the resource path * @return JsonApiResource */ public function createResource($type, $id, $relationshipPath = null) { $resource = new JsonApiResource($type, $id); $resource->setRelationshipPath($relationshipPath); return $resource; }
public function providerGetJsonValue() { $document1 = new JsonApiDocument(); $document1->setMeta('meta1', 'value1'); $document1->setLink('self', 'http://url'); $expected1 = array('jsonapi' => array('version' => '1.0'), 'meta' => array('meta1' => 'value1'), 'links' => array('self' => $document1->getLink('self'))); $error = new JsonApiError(); $error->setCode('my-error'); $document2 = new JsonApiDocument(); $document2->addError($error); $expected2 = array('jsonapi' => array('version' => '1.0'), 'errors' => array($error)); $resource = new JsonApiResource('type', 'id'); $resource->setAttribute('attribute1', 'value1'); $resource->setAttribute('attribute2', 'value2'); $document3 = new JsonApiDocument(); $document3->setResourceData('type', $resource); $expected3 = array('jsonapi' => array('version' => '1.0'), 'data' => $resource->getJsonValue(true)); $document4 = new JsonApiDocument(); $document4->setResourceData('type', null); $expected4 = array('jsonapi' => array('version' => '1.0'), 'data' => null); return array(array($expected1, $document1), array($expected2, $document2), array($expected3, $document3), array($expected4, $document4)); }