/**
  * Retrieves all process instances within a given context.
  * @link http://docs.camunda.org/api-references/rest/#!/process-instance/get-query
  * @link http://docs.camunda.org/api-references/rest/#!/process-instance/post-query
  *
  * @param ProcessInstanceRequest $request filter parameters
  * @param bool $isPostRequest switch for GET/POST request
  * @throws \Exception
  * @return object list of process instances
  */
 public function getInstances(ProcessInstanceRequest $request, $isPostRequest = false)
 {
     $this->setRequestUrl('/process-instance/');
     $this->setRequestObject($request);
     if ($isPostRequest == true) {
         $this->setRequestMethod('POST');
     } else {
         $this->setRequestMethod('GET');
     }
     try {
         $prepare = $this->execute();
         $response = array();
         foreach ($prepare as $index => $data) {
             $processInstance = new ProcessInstance();
             $response['instance_' . $index] = $processInstance->cast($data);
         }
         return (object) $response;
     } catch (Exception $e) {
         throw $e;
     }
 }
 /**
  * Instantiates a given process definition.
  * @link http://docs.camunda.org/api-references/rest/#!/process-definition/post-start-process-instance
  *
  * @param String $key process definition key
  * @param ProcessDefinitionRequest $request variables
  * @throws \Exception
  * @return \org\camunda\php\sdk\entity\response\ProcessInstance $this started process instance
  */
 public function startInstanceByKey($key, ProcessDefinitionRequest $request)
 {
     $processInstance = new ProcessInstance();
     $this->setRequestUrl('/process-definition/key/' . $key . '/start');
     $this->setRequestObject($request);
     $this->setRequestMethod('POST');
     try {
         return $processInstance->cast($this->execute());
     } catch (Exception $e) {
         throw $e;
     }
 }