/** * adds a resource to the primary collection * this will end up in response.data[] * * @note only the data-key of a resource is used * that is its type, id, attributes, relations, links, meta(data-level) * further, its included resources are separately added to response.included[] * * @see jsonapi\resource * @see ->fill_collection() for adding a whole array of resources directly * * @param \alsvanzelf\jsonapi\resource $resource * @return void */ public function add_resource(\alsvanzelf\jsonapi\resource $resource) { $resource_array = $resource->get_array(); $included_resources = $resource->get_included_resources(); if (!empty($included_resources)) { $this->fill_included_resources($included_resources); } $this->primary_collection[] = $resource_array['data']; }
<?php use alsvanzelf\jsonapi; ini_set('display_errors', 1); error_reporting(-1); require '../vendor/autoload.php'; /** * the collection you want to send out * * normally, you'd fetch this from a database */ require 'dataset.php'; $users = array(new user(1), new user(2), new user(42)); $collection = array(); foreach ($users as $user) { $resource = new jsonapi\resource($type = 'user', $user->id); $resource->set_self_link('/user/' . $user->id); $resource->fill_data($user); if ($user->id == 42) { $ship = new jsonapi\resource('ship', 5); $ship->add_data('name', 'Heart of Gold'); $ship->set_self_link('/ship/5'); $resource->add_relation('ship', $ship); } $collection[] = $resource; } /** * building up the json response * * you can set arrays, single data points, or whole objects * objects are converted into arrays using their public keys
/** * adds an included resource * this will end up in response.included[] * * prefer using ->add_relation() instead * * a $resource should have its 'id' set * * @note this can only be used by resource and collection, not by errors * * @param \alsvanzelf\jsonapi\resource $resource */ public function add_included_resource(\alsvanzelf\jsonapi\resource $resource) { if (property_exists($this, 'included_resources') == false) { throw new \Exception(get_class($this) . ' can not contain included resources'); } $resource_array = $resource->get_array(); if (empty($resource_array['data']['id'])) { return; } $resource_array = $resource_array['data']; unset($resource_array['relationships'], $resource_array['meta']); $key = $resource_array['type'] . '/' . $resource_array['id']; $this->included_data[$key] = $resource_array; // make a backup of the actual resource, to pass on to a collection $this->included_resources[$key] = $resource; }
/** * @param $linkPrefix * @return JsonApi\resource * @throws \Exception */ protected function prepareJsonApiResource($linkPrefix = '/forest') { $toReturn = new JsonApi\resource($this->getCollection()->getName(), $this->getId()); $toReturn->fill_data($this->getAttributes()); foreach ($this->getRelationships() as $relationship) { $resource = new JsonApi\resource($relationship->getType(), $relationship->getId()); $toReturn->add_relation($relationship->getType(), $resource); } foreach ($this->getIncluded() as $resource) { $toInclude = new JsonApi\resource($resource->getCollection()->getName(), $resource->getId()); // NOTE : alsvanzelf/jsonapi takes current request to build set_self_link // => included resources must set it "manually" $toInclude->set_self_link($linkPrefix . '/' . $resource->getCollection()->getName() . '/' . $resource->getId()); $toInclude->fill_data($resource->getAttributes()); $toReturn->add_included_resource($toInclude); } return $toReturn; }
<?php use alsvanzelf\jsonapi; ini_set('display_errors', 1); error_reporting(-1); require '../vendor/autoload.php'; /** * the resource you want to send out * * normally, you'd fetch this from a database */ require 'dataset.php'; $user = new user(42); /** * building up the json response * * you can set arrays, single data points, or whole objects * objects are converted into arrays using their public keys */ $jsonapi = new jsonapi\resource($type = 'user', $user->id); $jsonapi->fill_data($user); $jsonapi->add_data('location', $user->get_current_location()); /** * sending the response */ $jsonapi->send_response();