/** * Creates BatchError object. * * @param MicrosoftAzure\Storage\Common\ServiceException $error The error object. * @param array $headers The response headers. * * @return \MicrosoftAzure\Storage\Table\Models\BatchError */ public static function create($error, $headers) { Validate::isTrue($error instanceof ServiceException, Resources::INVALID_EXC_OBJ_MSG); Validate::isArray($headers, 'headers'); $result = new BatchError(); $clean = array_change_key_case($headers); $result->setError($error); $contentId = Utilities::tryGetValue($clean, Resources::CONTENT_ID); $result->setContentId(is_null($contentId) ? null : intval($contentId)); return $result; }
/** * @covers MicrosoftAzure\Storage\Table\Models\BatchError::create * @covers MicrosoftAzure\Storage\Table\Models\BatchError::getError * @covers MicrosoftAzure\Storage\Table\Models\BatchError::getContentId */ public function testCreate() { // Setup $error = new ServiceException('200'); $contentId = 1; $headers = array('content-id' => strval($contentId)); // Test $batchError = BatchError::create($error, $headers); // Assert $this->assertEquals($error, $batchError->getError()); $this->assertEquals($contentId, $batchError->getContentId()); return $batchError; }
/** * Creates BatchResult object. * * @param string $body The HTTP response body. * @param array $operations The batch operations. * @param array $contexts The batch operations context. * @param IAtomReaderWriter $atomSerializer The Atom reader and writer. * @param IMimeReaderWriter $mimeSerializer The MIME reader and writer. * * @return \MicrosoftAzure\Storage\Table\Models\BatchResult * * @throws \InvalidArgumentException */ public static function create($body, $operations, $contexts, $atomSerializer, $mimeSerializer) { $result = new BatchResult(); $responses = self::_constructResponses($body, $mimeSerializer); $callbackName = __CLASS__ . '::_compareUsingContentId'; $count = count($responses); $entries = array(); // Sort $responses based on Content-ID so they match order of $operations. uasort($responses, $callbackName); for ($i = 0; $i < $count; $i++) { $context = $contexts[$i]; $response = $responses[$i]; $operation = $operations[$i]; $type = $operation->getType(); $body = $response->body; $headers = HttpFormatter::formatHeaders($response->headers); try { ServiceRestProxy::throwIfError($response->statusCode, $response->reason, $response->body, $context->getStatusCodes()); switch ($type) { case BatchOperationType::INSERT_ENTITY_OPERATION: $entries[] = InsertEntityResult::create($body, $headers, $atomSerializer); break; case BatchOperationType::UPDATE_ENTITY_OPERATION: case BatchOperationType::MERGE_ENTITY_OPERATION: case BatchOperationType::INSERT_REPLACE_ENTITY_OPERATION: case BatchOperationType::INSERT_MERGE_ENTITY_OPERATION: $entries[] = UpdateEntityResult::create($headers); break; case BatchOperationType::DELETE_ENTITY_OPERATION: $entries[] = Resources::BATCH_ENTITY_DEL_MSG; break; default: throw new \InvalidArgumentException(); } } catch (ServiceException $e) { $entries[] = BatchError::create($e, $response->headers); } } $result->setEntries($entries); return $result; }