/** * Store a message in the cache. * * @param string $key The message key * @param MessageInterface $message */ private function saveIntoCache($key, MessageInterface $message) { if ($this->cache) { $item = $this->cache->getItem($key); $item->set([$message->getHeaders(), (string) $message->getBody()]); $this->cache->save($item); } }
public function str(MessageInterface $message) { if ($message instanceof RequestInterface) { $msg = trim($message->getMethod() . ' ' . $message->getRequestTarget()) . ' HTTP/' . $message->getProtocolVersion(); if (!$message->hasHeader('host')) { $msg .= "\r\nHost: " . $message->getUri()->getHost(); } } elseif ($message instanceof ResponseInterface) { $msg = 'HTTP/' . $message->getProtocolVersion() . ' ' . $message->getStatusCode() . ' ' . $message->getReasonPhrase(); } foreach ($message->getHeaders() as $name => $values) { $msg .= "\r\n{$name}: " . implode(', ', $values); } if ($message->getBody()->getSize() < ini_get('memory_limit')) { $msg .= "\r\n\r\n" . $message->getBody(); } return $msg; }
public function validateMessageBody(MessageInterface $message, MessageDefinition $definition) { $bodyString = (string) $message->getBody(); if ($bodyString !== '' && $definition->hasBodySchema()) { $contentType = $message->getHeaderLine('Content-Type'); $decodedBody = $this->decoder->decode($bodyString, DecoderUtils::extractFormatFromContentType($contentType)); $this->validate($decodedBody, $definition->getBodySchema(), 'body'); } }
private function addJsonToHttpMessage(MessageInterface $message, array $dataToTurnIntoJson, bool $castToObject = false) { if ($castToObject) { $dataToTurnIntoJson = (object) $dataToTurnIntoJson; } $json = json_encode($dataToTurnIntoJson); $message->getBody()->write($json); return $message; }
/** * @inheritdoc */ protected function deserialize(MessageInterface $message, $format) { if ($this->class) { $content = $message->getBody()->getContents(); $parsedResponse = SerializerBuilder::create()->build()->deserialize($content, $this->class, $format, $this->context); return $parsedResponse; } else { return null; } }
/** * Add the message body if the stream is seekable. * * @param MessageInterface $request * @param string $message * * @return string */ private function addBody(MessageInterface $request, $message) { $stream = $request->getBody(); if (!$stream->isSeekable() || $this->maxBodyLength === 0) { // Do not read the stream $message .= "\n"; } else { $message .= "\n" . mb_substr($stream->__toString(), 0, $this->maxBodyLength); $stream->rewind(); } return $message; }
/** * @inheritdoc */ public function parse(MessageInterface $message) { $content = $message->getBody()->getContents(); if ($this->removeNamespacePrefixes) { $content = preg_replace('/(<\\/?)(\\w+:)/', '$1', $content); } if ($this->class) { $parsedResponse = SerializerBuilder::create()->build()->deserialize($content, $this->class, 'xml', $this->context); } else { $parsedResponse = simplexml_load_string($content); } return $parsedResponse; }
public function testWithBody() { $mockBody = Mockery::mock(StreamInterface::class); $messageWithBody = $this->message->withBody($mockBody); static::assertNull($this->message->getBody()); // return null // The body MUST be a StreamInterface object. static::assertNotSame($this->message, $messageWithBody); // This method MUST be implemented in such a way as to retain the // immutability of the message, and MUST return a new instance that has the // new body stream. static::assertInstanceOf(StreamInterface::class, $messageWithBody->getBody()); static::assertSame($mockBody, $messageWithBody->getBody()); }
/** * Attempts to rewind a message body and throws an exception on failure. * * The body of the message will only be rewound if a call to `tell()` returns a * value other than `0`. * * @param MessageInterface $message Message to rewind * * @throws \RuntimeException */ function rewind_body(MessageInterface $message) { $body = $message->getBody(); if ($body->tell()) { $body->rewind(); } }
protected function runMatches(MessageInterface $message) { return (string) $message->getBody() == $this->expected; }
/** * Creates a Body instance to execute assertions in the body. * * @return Stream */ public function assertBody() { return new Stream($this->message->getBody(), $this); }
/** * Get body with headers. * * @param MessageInterface $stream * * @return string */ public function getBodyWithHeaders(MessageInterface $stream) { $result = sprintf('%s%s%s', $this->getString($stream->getHeaders()), PHP_EOL, $stream->getBody()->getContents()); return $result; }
/** * {@inheritdoc} */ public function getBody() { return $this->message->getBody(); }
/** * Parse the response from the API. * * @param MessageInterface $response * @return Response * @codeCoverageIgnore */ public function toResponse(MessageInterface $response) { $json = $response->getBody()->getContents(); $toArray = json_decode($json, true); return new Response($toArray); }
private function addJsonToHttpMessage(MessageInterface $message, array $dataToTurnIntoJson) { $json = json_encode($dataToTurnIntoJson); $message->getBody()->write($json); return $message; }
/** * Parse the body of a PSR-7 message, into a PHP array. * TODO: We really need to find a package to do this. It is built into the * Guzzle client as helper methods, but this is not specifically a client * function. * * @param $message MessageInterface * @return array|mixed */ public static function parseBody(MessageInterface $message) { // If a ServerRequest object, then parsing will be handled (and cached if necessary) // by the implementation. if ($message instanceof ServerRequestInterface) { return $message->getParsedBody(); } $data = []; if ($message->hasHeader('Content-Type')) { // Sage Pay returns responses generally with JSON, but the notify callback is Form URL // encoded, so we need to parse both. if ($message->getHeaderLine('Content-Type') === 'application/x-www-form-urlencoded') { parse_str((string) $message->getBody(), $data); } elseif ($message->getHeaderLine('Content-Type') === 'application/json') { $data = json_decode((string) $message->getBody(), true); } } return $data; }