GraphAware\Neo4j\Client\Formatter\ResponseFormatter::format PHP Method

format() public method

Formats the Neo4j Response.
public format ( array $response, array $statements ) : ResultCollection
$response array
$statements array
return GraphAware\Neo4j\Client\Result\ResultCollection
    public function format(array $response, array $statements)
    {
        if (isset($response['errors'][0])) {
            $e = new Neo4jException($response['errors'][0]['message']);
            $e->setNeo4jStatusCode($response['errors'][0]['code']);
            throw $e;
        }
        $results = new ResultCollection();
        foreach ($response['results'] as $k => $result) {
            $resultO = new Result($statements[$k]);
            $resultO->setFields($result['columns']);
            foreach ($result['data'] as $data) {
                $resultO->pushRecord($data['rest'], $data['graph']);
            }
            if (array_key_exists('stats', $result)) {
                $resultO->setStats($result['stats']);
            }
            $results->add($resultO, $statements[$k]->getTag());
        }
        return $results;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * @param int   $transactionId
  * @param array $statementsStack
  *
  * @return \GraphAware\Common\Result\ResultCollection
  *
  * @throws Neo4jException
  */
 public function pushToTransaction($transactionId, array $statementsStack)
 {
     $statements = [];
     foreach ($statementsStack as $statement) {
         $st = ['statement' => $statement->text(), 'resultDataContents' => ['REST', 'GRAPH'], 'includeStats' => true];
         if (!empty($statement->parameters())) {
             $st['parameters'] = $this->formatParams($statement->parameters());
         }
         $statements[] = $st;
     }
     $headers = [['X-Stream' => true, 'Content-Type' => 'application/json']];
     $body = json_encode(['statements' => $statements]);
     $request = new Request('POST', sprintf('%s/db/data/transaction/%d', $this->uri, $transactionId), $headers, $body);
     try {
         $response = $this->httpClient->send($request);
         $data = json_decode((string) $response->getBody(), true);
         if (!empty($data['errors'])) {
             $msg = sprintf('Neo4j Exception with code "%s" and message "%s"', $data['errors'][0]['code'], $data['errors'][0]['message']);
             $exception = new Neo4jException($msg);
             $exception->setNeo4jStatusCode($data['errors'][0]['code']);
             throw $exception;
         }
         return $this->responseFormatter->format(json_decode($response->getBody(), true), $statementsStack);
     } catch (RequestException $e) {
         if ($e->hasResponse()) {
             $body = json_decode($e->getResponse()->getBody(), true);
             if (!isset($body['code'])) {
                 throw $e;
             }
             $msg = sprintf('Neo4j Exception with code "%s" and message "%s"', $body['errors'][0]['code'], $body['errors'][0]['message']);
             $exception = new Neo4jException($msg);
             $exception->setNeo4jStatusCode($body['errors'][0]['code']);
             throw $exception;
         }
         throw $e;
     }
 }
ResponseFormatter