You are here

protected function RestfulEntityBase::createOrUpdateSubResourceItem in RESTful 7

Create, update or return an already saved entity.

Parameters

int | array $value: The entity ID, or array to POST, PATCH, or PUT entity from.

\RestfulInterface $handler: The RESTful handler.

Return value

int The saved entity ID.

1 call to RestfulEntityBase::createOrUpdateSubResourceItem()
RestfulEntityBase::createOrUpdateSubResourceItems in plugins/restful/RestfulEntityBase.php
Create, update or return a set of already saved entities.

File

plugins/restful/RestfulEntityBase.php, line 883
Contains RestfulEntityBase.

Class

RestfulEntityBase
An abstract implementation of RestfulEntityInterface.

Code

protected function createOrUpdateSubResourceItem($value, \RestfulInterface $handler) {
  if (!is_array($value)) {

    // Item that was passed is already a reference to an existing entity.
    return $value;
  }

  // Value is actually a sub request, so for clarity we will name it $request.
  $request = $value;

  // Figure the method that should be used.
  if (empty($request['id'])) {
    $method_name = \RestfulInterface::POST;
    $path = '';
  }
  else {

    // Use PATCH by default, unless client has explicitly set the method in
    // the sub-resource.
    // As any request, under the the "__application" we may pass additional
    // metadata.
    $method_name = !empty($request['__application']['method']) ? strtoupper($request['__application']['method']) : \RestfulInterface::PATCH;
    $path = implode(',', array_unique(array_filter(explode(',', $request['id']))));

    // Unset the ID from the sub-request.
    unset($request['id']);
  }
  $result = $handler
    ->process($path, $request, $method_name, FALSE);
  return $result[0]['id'];
}