summaryrefslogtreecommitdiff
path: root/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers
diff options
context:
space:
mode:
authorEole <josselin.35@live.fr>2016-01-21 10:29:26 +0100
committerEole <josselin.35@live.fr>2016-01-21 10:29:26 +0100
commita44cc1d2e3c0f147e91a5c052ac7fd879e34e706 (patch)
treebdd6f72e0ba732c4fcc0479d1cfcf4d0baa5885d /server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers
parent35db27b0e62b4cdcb03b0d21bceb4efc769e6161 (diff)
Init Server Composer Components
Diffstat (limited to 'server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers')
-rw-r--r--server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/AbstractRetriever.php29
-rw-r--r--server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/Curl.php79
-rw-r--r--server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/FileGetContents.php87
-rw-r--r--server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/PredefinedArray.php54
-rw-r--r--server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/UriRetrieverInterface.php32
5 files changed, 281 insertions, 0 deletions
diff --git a/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/AbstractRetriever.php b/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/AbstractRetriever.php
new file mode 100644
index 0000000..f924ad8
--- /dev/null
+++ b/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/AbstractRetriever.php
@@ -0,0 +1,29 @@
+<?php
+/**
+ * JsonSchema
+ * @filesource
+ */
+namespace JsonSchema\Uri\Retrievers;
+
+/**
+ * AbstractRetriever implements the default shared behavior
+ * that all decendant Retrievers should inherit
+ * @author Steven Garcia <webwhammy@gmail.com>
+ */
+abstract class AbstractRetriever implements UriRetrieverInterface
+{
+ /**
+ * Media content type
+ * @var string
+ */
+ protected $contentType;
+
+ /**
+ * {@inheritDoc}
+ * @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::getContentType()
+ */
+ public function getContentType()
+ {
+ return $this->contentType;
+ }
+}
diff --git a/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/Curl.php b/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/Curl.php
new file mode 100644
index 0000000..cd8414f
--- /dev/null
+++ b/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/Curl.php
@@ -0,0 +1,79 @@
+<?php
+
+/*
+ * This file is part of the JsonSchema package.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace JsonSchema\Uri\Retrievers;
+
+use JsonSchema\Validator;
+
+/**
+ * Tries to retrieve JSON schemas from a URI using cURL library
+ *
+ * @author Sander Coolen <sander@jibber.nl>
+ */
+class Curl extends AbstractRetriever
+{
+ protected $messageBody;
+
+ public function __construct()
+ {
+ if (!function_exists('curl_init')) {
+ throw new \RuntimeException("cURL not installed");
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve()
+ */
+ public function retrieve($uri)
+ {
+ $ch = curl_init();
+
+ curl_setopt($ch, CURLOPT_URL, $uri);
+ curl_setopt($ch, CURLOPT_HEADER, true);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: ' . Validator::SCHEMA_MEDIA_TYPE));
+
+ $response = curl_exec($ch);
+ if (false === $response) {
+ throw new \JsonSchema\Exception\ResourceNotFoundException('JSON schema not found');
+ }
+
+ $this->fetchMessageBody($response);
+ $this->fetchContentType($response);
+
+ curl_close($ch);
+
+ return $this->messageBody;
+ }
+
+ /**
+ * @param string $response cURL HTTP response
+ */
+ private function fetchMessageBody($response)
+ {
+ preg_match("/(?:\r\n){2}(.*)$/ms", $response, $match);
+ $this->messageBody = $match[1];
+ }
+
+ /**
+ * @param string $response cURL HTTP response
+ * @return boolean Whether the Content-Type header was found or not
+ */
+ protected function fetchContentType($response)
+ {
+ if (0 < preg_match("/Content-Type:(\V*)/ims", $response, $match)) {
+ $this->contentType = trim($match[1]);
+
+ return true;
+ }
+
+ return false;
+ }
+} \ No newline at end of file
diff --git a/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/FileGetContents.php b/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/FileGetContents.php
new file mode 100644
index 0000000..bc43de6
--- /dev/null
+++ b/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/FileGetContents.php
@@ -0,0 +1,87 @@
+<?php
+
+/*
+ * This file is part of the JsonSchema package.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace JsonSchema\Uri\Retrievers;
+
+use JsonSchema\Exception\ResourceNotFoundException;
+use JsonSchema\Validator;
+
+/**
+ * Tries to retrieve JSON schemas from a URI using file_get_contents()
+ *
+ * @author Sander Coolen <sander@jibber.nl>
+ */
+class FileGetContents extends AbstractRetriever
+{
+ protected $messageBody;
+
+ /**
+ * {@inheritDoc}
+ * @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve()
+ */
+ public function retrieve($uri)
+ {
+ $context = stream_context_create(array(
+ 'http' => array(
+ 'method' => 'GET',
+ 'header' => "Accept: " . Validator::SCHEMA_MEDIA_TYPE
+ )));
+
+ set_error_handler(function() use ($uri) {
+ throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
+ });
+ $response = file_get_contents($uri);
+ restore_error_handler();
+
+ if (false === $response) {
+ throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
+ }
+ if ($response == ''
+ && substr($uri, 0, 7) == 'file://' && substr($uri, -1) == '/'
+ ) {
+ throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
+ }
+
+ $this->messageBody = $response;
+ if (! empty($http_response_header)) {
+ $this->fetchContentType($http_response_header);
+ } else {
+ // Could be a "file://" url or something else - fake up the response
+ $this->contentType = null;
+ }
+
+ return $this->messageBody;
+ }
+
+ /**
+ * @param array $headers HTTP Response Headers
+ * @return boolean Whether the Content-Type header was found or not
+ */
+ private function fetchContentType(array $headers)
+ {
+ foreach ($headers as $header) {
+ if ($this->contentType = self::getContentTypeMatchInHeader($header)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * @param string $header
+ * @return string|null
+ */
+ protected static function getContentTypeMatchInHeader($header)
+ {
+ if (0 < preg_match("/Content-Type:(\V*)/ims", $header, $match)) {
+ return trim($match[1]);
+ }
+ }
+}
diff --git a/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/PredefinedArray.php b/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/PredefinedArray.php
new file mode 100644
index 0000000..7652c42
--- /dev/null
+++ b/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/PredefinedArray.php
@@ -0,0 +1,54 @@
+<?php
+
+namespace JsonSchema\Uri\Retrievers;
+
+use JsonSchema\Validator;
+
+/**
+ * URI retrieved based on a predefined array of schemas
+ *
+ * @example
+ *
+ * $retriever = new PredefinedArray(array(
+ * 'http://acme.com/schemas/person#' => '{ ... }',
+ * 'http://acme.com/schemas/address#' => '{ ... }',
+ * ))
+ *
+ * $schema = $retriever->retrieve('http://acme.com/schemas/person#');
+ */
+class PredefinedArray extends AbstractRetriever
+{
+ /**
+ * Contains schemas as URI => JSON
+ * @var array
+ */
+ private $schemas;
+
+ /**
+ * Constructor
+ *
+ * @param array $schemas
+ * @param string $contentType
+ */
+ public function __construct(array $schemas, $contentType = Validator::SCHEMA_MEDIA_TYPE)
+ {
+ $this->schemas = $schemas;
+ $this->contentType = $contentType;
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve()
+ */
+ public function retrieve($uri)
+ {
+ if (!array_key_exists($uri, $this->schemas)) {
+ throw new \JsonSchema\Exception\ResourceNotFoundException(sprintf(
+ 'The JSON schema "%s" was not found.',
+ $uri
+ ));
+ }
+
+ return $this->schemas[$uri];
+ }
+}
diff --git a/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/UriRetrieverInterface.php b/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/UriRetrieverInterface.php
new file mode 100644
index 0000000..c324998
--- /dev/null
+++ b/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/UriRetrieverInterface.php
@@ -0,0 +1,32 @@
+<?php
+
+/*
+ * This file is part of the JsonSchema package.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace JsonSchema\Uri\Retrievers;
+
+/**
+ * Interface for URI retrievers
+ *
+ * @author Sander Coolen <sander@jibber.nl>
+ */
+interface UriRetrieverInterface
+{
+ /**
+ * Retrieve a schema from the specified URI
+ * @param string $uri URI that resolves to a JSON schema
+ * @throws \JsonSchema\Exception\ResourceNotFoundException
+ * @return mixed string|null
+ */
+ public function retrieve($uri);
+
+ /**
+ * Get media content type
+ * @return string
+ */
+ public function getContentType();
+} \ No newline at end of file