/** * 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']; }
/** * 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; }