diff --git a/src/JsonSchema/Uri/Retrievers/FileGetContents.php b/src/JsonSchema/Uri/Retrievers/FileGetContents.php index 34e6ede0..cf566d64 100644 --- a/src/JsonSchema/Uri/Retrievers/FileGetContents.php +++ b/src/JsonSchema/Uri/Retrievers/FileGetContents.php @@ -27,16 +27,7 @@ class FileGetContents extends AbstractRetriever */ public function retrieve($uri) { - $errorMessage = null; - set_error_handler(function ($errno, $errstr) use (&$errorMessage) { - $errorMessage = $errstr; - }); - $response = file_get_contents($uri); - restore_error_handler(); - - if ($errorMessage) { - throw new ResourceNotFoundException($errorMessage); - } + $response = $this->getContents($uri); if (false === $response) { throw new ResourceNotFoundException('JSON schema not found at ' . $uri); @@ -61,6 +52,27 @@ public function retrieve($uri) return $this->messageBody; } + /** + * @param string $uri + * + * @return string|bool + */ + protected function getContents($uri) + { + $errorMessage = null; + set_error_handler(function ($errno, $errstr) use (&$errorMessage) { + $errorMessage = $errstr; + }); + $response = file_get_contents($uri); + restore_error_handler(); + + if ($errorMessage) { + throw new ResourceNotFoundException($errorMessage); + } + + return $response; + } + /** * @param array $headers HTTP Response Headers * @@ -68,7 +80,7 @@ public function retrieve($uri) */ private function fetchContentType(array $headers) { - foreach (array_reverse($headers) as $header) { + foreach ($headers as $header) { if ($this->contentType = self::getContentTypeMatchInHeader($header)) { return true; }