/** * Validate an HTTP message content-type against a message definition * * @param MessageInterface $message * @param MessageDefinition $definition * * @return bool Is valid */ public function validateContentType(MessageInterface $message, MessageDefinition $definition) { $isValid = true; $contentType = $message->getHeaderLine('Content-Type'); if (!in_array($contentType, $definition->getContentTypes())) { $isValid = false; if ($contentType === '') { $violationMessage = 'Content-Type should not be empty'; $constraint = 'required'; } else { $violationMessage = sprintf('%s is not a supported content type, supported: %s', $message->getHeaderLine('Content-Type'), implode(', ', $definition->getContentTypes())); $constraint = 'enum'; } $this->addViolation(new ConstraintViolation('Content-Type', $violationMessage, $constraint, 'header')); } return $isValid; }
/** * @param MessageInterface $message * @param string $headerName * @param string $value * * @return bool */ private function hasHeaderContains(MessageInterface $message, $headerName, $value) { return strpos($message->getHeaderLine($headerName), $value) !== false; }
public function testGetHeaderLineReturnsEmptyStringWhenHeaderDoesNotExist() { $this->assertEmpty($this->message->getHeaderLine('X-Foo-Bar')); }
/** * {@inheritdoc} */ public function getHeaderLine($header) { return $this->message->getHeaderLine($header); }
protected function runMatches(MessageInterface $message) { return $this->expected == $message->getHeaderLine($this->key); }
/** * 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; }