/** * @param $id * @return Result */ protected function call($method, $resource, $body = null, $acceptedCodes = array(200)) { try { $response = $this->client->request($method, $resource, array('body' => $body)); $responseBody = (string) $response->getBody(); if ($responseBody) { /** @var Result $result */ $result = $this->serializer->deserialize($responseBody, $this->getResultClass(), 'json'); $result->deserializeData($this->serializer, $this->getModel()); } else { $result = new Result(); } $result->setSuccess(in_array($response->getStatusCode(), $acceptedCodes))->setMessage($response->getReasonPhrase()); return $result; } catch (GuzzleException $ge) { if ($ge->getCode() == \Symfony\Component\HttpFoundation\Response::HTTP_TOO_MANY_REQUESTS && php_sapi_name() == "cli") { sleep(5); return $this->call($method, $resource, $body, $acceptedCodes); } else { $result = new Result(); $result->setSuccess(false)->setMessage(sprintf("Client error: %s", $ge->getMessage())); return $result; } } catch (\Exception $e) { $result = new Result(); $result->setSuccess(false)->setMessage(sprintf("General error: %s", $e->getMessage())); return $result; } }
/** * @param array $data * @param string|null|array $serializationLevel * @return Response */ public function createResponse(array $data, $serializationLevel = null) { $responseData = [static::KEY_STATUS => static::STATUS_SUCCESS, static::KEY_DATA => $data]; $serializedData = $this->serializer->serialize($responseData, self::STANDARD_RESPONSE_FORMAT, $serializationLevel ? SerializationContext::create()->setGroups($serializationLevel) : SerializationContext::create()); $response = $this->getJsonResponse($serializedData); return $response; }
/** * {@inheritdoc} */ public function notifyCallback($callbackUrl, $statusResponse) { $requestBody = $this->serializer->serialize($statusResponse, 'json'); $request = new \cURL\Request($callbackUrl); $request->getOptions()->set(CURLOPT_FILE, fopen('/dev/null', 'w'))->set(CURLOPT_TIMEOUT, 5)->set(CURLOPT_RETURNTRANSFER, false)->set(CURLOPT_CUSTOMREQUEST, 'POST')->set(CURLOPT_POSTFIELDS, $requestBody)->set(CURLOPT_HTTPHEADER, array('Content-Type: application/json', sprintf('Content-Length: %d', strlen($requestBody)))); $request->send(); }
/** * @param $data * @param int $status * @param array $headers * @return \Symfony\Component\HttpFoundation\Response|static */ protected function success($data, $status = 200, $headers = []) { $serialized_data = $this->serializer->serialize($data, 'json'); $response = $this->jsonResponse; $response->setContent($serialized_data)->setStatusCode($status)->headers->add($headers); return $response; }
/** * @param string|array $serialized * @param int $format * * @return GameInterface */ public function deserialize($serialized, $format) { if ($format === self::FORMAT_ARRAY) { $serialized = json_encode($serialized, true); } return $this->serializer->deserialize($serialized, Game::class, self::FORMAT_JSON, $this->deserializationContext); }
public function testSerializationToJson() { $subject = new Message('to', 'from', 'subject', 'message'); $data = $this->serializer->serialize($subject, 'json'); $object = $this->serializer->deserialize($data, get_class($subject), 'json'); $this->assertEquals($subject, $object); }
/** * @param $track * @param $debug * * @throws RussianPostApiException * @throws InvalidTrackException * * @return OperationHistoryData */ public function getTrackOperationHistory($track, &$debug, $language = "RUS") { if (!TrackValidator::validateTrack($track)) { throw new InvalidTrackException(); } $track = TrackValidator::filterTrack($track); $client = $this->initClient(); $AuthorizationHeader = self::createAuthorizationHeader($this->login, $this->password); $historyRequest = self::createOperationHistoryRequest($track, 0, $language); $parameters = ["AuthorizationHeader" => $this->serializer->serialize($AuthorizationHeader, 'array'), "historyRequest" => $this->serializer->serialize($historyRequest, 'array')]; //($operation, $params=array(), $namespace='http://tempuri.org', $soapAction='', $headers=false, $rpcParams=null, $style='rpc', $use='encoded') $result = $client->call('GetOperationHistory', $parameters, 'http://russianpost.org/operationhistory'); $debug = array("Request" => $client->request, "Response" => $client->response, "Debug" => $client->debug_str); if ($client->fault) { throw new RussianPostApiException(print_r($result, true)); } else { $err = $client->getError(); if ($err) { throw new RussianPostApiException($err); } else { /** @var OperationHistoryData $object */ $object = $this->serializer->deserialize($result, 'a3mg\\RussianPostBundle\\Model\\OperationHistoryData', 'array'); return $object; } } }
public function deserializeData(SerializerInterface $serializer, $getModel) { foreach ($this->data as $objectArray) { $obj = $serializer->deserialize($objectArray, $getModel, 'array'); $this->objects[] = $obj; } }
/** * {@inheritdoc} */ public function publish(Event $event) { try { $this->client->post(self::PUBLISH_URL, ['body' => $this->serializer->serialize($event, 'json')]); } catch (BadResponseException $exception) { throw new PublishException($exception->getMessage()); } }
public function testSerializationToJson() { $exception = new \Exception('foobar', 100); $subject = new ExceptionResponse($exception); $data = $this->serializer->serialize($subject, 'json'); $object = $this->serializer->deserialize($data, ExceptionResponse::class, 'json'); $this->assertEquals($subject, $object); }
/** * Deserialize XML into a BaseResponse * * @param $xml * @return BaseResponse * @throws SerializationException */ public function deserialize($xml) { try { return $this->serializer->deserialize($xml, 'SMH\\Enom\\Response\\BaseResponse', 'xml'); } catch (\RuntimeException $ex) { throw new SerializationException($ex->getMessage(), null, $ex); } }
protected function jsonResponse($data) { $context = new SerializationContext(); $context->setSerializeNull(true); $content = $this->serializer->serialize($data, 'json', $context); $this->response->setContent($content); return $this->response; }
/** * {@inheritdoc} */ public function post($uri, $data = null, array $options = array()) { if (is_array($data)) { $options = array_merge($data, $options); $data = new \StdClass(); } $this->request('post', $uri, array('body' => $this->serializer->serialize($data, 'json', SerializationContext::create()->setAttribute('options', $options)))); }
/** * {@inheritdoc} */ public function createCampaignSuppressionImport($campaignId, $source, $location, $data) { try { return $this->client->getCommand('createCampaignSuppressionImport', array('campaignId' => $campaignId, 'suppression_import' => array('source' => $source, 'location' => $location, 'data' => $data)))->execute(); } catch (ClientErrorResponseException $e) { return $this->serializer->deserialize($e->getResponse()->getBody(), 'EBC\\AdvertiserClient\\Campaign\\InvalidImportResponse', 'json'); } }
/** * @param ApiResponse $apiResponse to convert to a Symfony Response. * @param Request $request that needs this Response. * * @return Response */ public function toSymfonyResponse(ApiResponse $apiResponse, Request $request) { $format = $request->getRequestFormat($this->defaultResponseFormat); $serialized = $this->serializer->serialize($apiResponse->getData(), $format); $response = new Response($serialized, $apiResponse->getStatusCode(), $apiResponse->getHeaders()); $response->headers->set('Content-Type', $request->getMimeType($format)); return $response; }
/** * @test */ public function it_can_serialize_with_context_and_format() { $format = 'yml'; $context = Mock::mock(SerializationContext::class); $this->jmsSerializer->shouldReceive('serialize')->once()->with([1234], $format, $context); $serializer = new JmsSerializer($this->jmsSerializer, $format, $context); $serializer->serialize([1234]); }
/** * @inheritdoc */ public function deserialize($data, $type, $format, $groups = null) { $context = null; if ($groups) { $context = DeserializationContext::create()->setGroups($groups); } return $this->serializer->deserialize($data, $type, $format, $context); }
/** * @param string $json * @param string $type * @param DeserializationContext $context * * @return object|null */ protected function deserialize($json, $type, DeserializationContext $context = null) { $object = $this->serializer->deserialize($json, $type, 'json', $context); if (!$object instanceof $type) { return null; } return $object; }
/** * {@inheritdoc} */ public function apply(Request $request, ParamConverter $configuration) { $json = $request->getContent(); /** @var Event $event */ $event = $this->serializer->deserialize($json, 'Ndewez\\EventsBundle\\Model\\Event', 'json'); $request->attributes->set('event', $event); return true; }
/** * @param string $title * @param object $object * * @return Event * * @throws LogicException */ private function buildEvent($title, $object) { if (!is_object($object)) { throw new LogicException(sprintf('Connector only allows publish objects, "%s" given', gettype($object))); } $event = new Event(); $event->setTitle($title)->setNamespace(get_class($object))->setPayload($this->serializer->serialize($object, 'json')); return $event; }
public function enrichRequestWithParsedBody(ServerRequestInterface $request) { if ($request->hasHeader(HeaderName::CONTENT_TYPE) && $request->getHeaderLine(HeaderName::CONTENT_TYPE) === 'application/json') { $parsedBody = $this->serializer->deserialize($request->getBody()->__toString(), 'array', 'json'); return $request->withParsedBody($parsedBody); } else { return $request->withParsedBody([]); } }
/** * {@inheritdoc} */ public function execute(AMQPMessage $amqpMessage) { // Ack message and get event $amqpMessage->delivery_info['channel']->basic_ack($amqpMessage->delivery_info['delivery_tag']); /** @var Event $event */ $event = $this->serializer->deserialize($amqpMessage->getBody(), 'Ndewez\\EventsBundle\\Model\\Event', 'json'); // Process $this->listen->listen($event); }
public function writeResponseContent(array $responseContent, $requestedContentType) { if (empty($responseContent)) { $serializedResponseContent = ""; } else { $serializedResponseContent = $this->serializer->serialize($responseContent, 'json'); } echo $serializedResponseContent; }
/** * Send a request to fastbill. * * @param mixed $request The request to send. * @param string $responseClass The class that should be used to unserialize the response. * @return ApiResponseInterface */ protected function sendRequest(RequestInterface $request, $responseClass) { if (!in_array(ApiResponseInterface::class, class_implements($responseClass))) { throw new \InvalidArgumentException('The response class must implement "' . ApiResponseInterface::class . '".'); } $body = $this->serializer->serialize($request, 'json'); $response = $this->transport->sendRequest($body); return $this->serializer->deserialize($response, $responseClass, 'json'); }
/** * {@inheritdoc} */ public function deserialize($json) { $context = DeserializationContext::create(); if (!is_null($this->groupName)) { $context->setGroups(array($this->groupName)); $context->setSerializeNull($this->serializeNull); } return $this->jmsSerializer->deserialize($json, $this->documentClass, 'json', $context); }
/** * @param Event $event */ public function listen(Event $event) { if (!in_array($event->getTitle(), $this->events, true)) { return; } $payload = $this->serializer->deserialize($event->getPayload(), $event->getNamespace(), 'json'); $ndewezEvent = new NdewezEvent($payload); $this->dispatcher->dispatch(sprintf('ndewez_events.%s', $event->getTitle()), $ndewezEvent); }
/** * @param string $format * @dataProvider provideSupportedFormats */ public function testSerializeToJsonWithMemberType($format) { $subject = new MemberTest(); $subject->aString = 'foobar'; $subject->enum = TaggedTestType::VALUE1(); $data = $this->serializer->serialize($subject, $format); $object = $this->serializer->deserialize($data, MemberTest::class, $format); $this->assertEquals($subject, $object); }
/** * @param Request $request * * @return array */ public function deserializeRequest(Request $request) { $format = $this->checkAcceptHeader($request); try { return $this->serializer->deserialize($request->getContent(), 'array', $format); } catch (\Exception $e) { throw new \RuntimeException('Could not deserialize content from the request.'); } }
/** * @return array */ public function getTagsFunction() { return $this->memoizeCache->memoize(function () { $tags = $this->tagManager->findAll(); $context = SerializationContext::create(); $context->setSerializeNull(true); $context->setGroups(['partialTag']); return $this->serializer->serialize($tags, 'array', $context); }); }
/** * @param Job $expectedJob * @param string $data * @param array $groups * @dataProvider provideSerializedJob */ public function testDeserialization($expectedJob, $data, array $groups = []) { $context = null; if (count($groups) > 0) { $context = new DeserializationContext(); $context->setGroups($groups); } $job = $this->serializer->deserialize($data, Job::class, 'json', $context); $this->assertEquals($expectedJob, $job); }