summaryrefslogtreecommitdiff
path: root/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/UriResolver.php
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/UriResolver.php
parent35db27b0e62b4cdcb03b0d21bceb4efc769e6161 (diff)
Init Server Composer Components
Diffstat (limited to 'server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/UriResolver.php')
-rw-r--r--server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/UriResolver.php157
1 files changed, 157 insertions, 0 deletions
diff --git a/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/UriResolver.php b/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/UriResolver.php
new file mode 100644
index 0000000..9784114
--- /dev/null
+++ b/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/UriResolver.php
@@ -0,0 +1,157 @@
+<?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;
+
+use JsonSchema\Exception\UriResolverException;
+
+/**
+ * Resolves JSON Schema URIs
+ *
+ * @author Sander Coolen <sander@jibber.nl>
+ */
+class UriResolver
+{
+ /**
+ * Parses a URI into five main components
+ *
+ * @param string $uri
+ * @return array
+ */
+ public function parse($uri)
+ {
+ preg_match('|^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?|', $uri, $match);
+
+ $components = array();
+ if (5 < count($match)) {
+ $components = array(
+ 'scheme' => $match[2],
+ 'authority' => $match[4],
+ 'path' => $match[5]
+ );
+ }
+ if (7 < count($match)) {
+ $components['query'] = $match[7];
+ }
+ if (9 < count($match)) {
+ $components['fragment'] = $match[9];
+ }
+
+ return $components;
+ }
+
+ /**
+ * Builds a URI based on n array with the main components
+ *
+ * @param array $components
+ * @return string
+ */
+ public function generate(array $components)
+ {
+ $uri = $components['scheme'] . '://'
+ . $components['authority']
+ . $components['path'];
+
+ if (array_key_exists('query', $components)) {
+ $uri .= $components['query'];
+ }
+ if (array_key_exists('fragment', $components)) {
+ $uri .= '#' . $components['fragment'];
+ }
+
+ return $uri;
+ }
+
+ /**
+ * Resolves a URI
+ *
+ * @param string $uri Absolute or relative
+ * @param string $baseUri Optional base URI
+ * @return string Absolute URI
+ */
+ public function resolve($uri, $baseUri = null)
+ {
+ if ($uri == '') {
+ return $baseUri;
+ }
+
+ $components = $this->parse($uri);
+ $path = $components['path'];
+
+ if (! empty($components['scheme'])) {
+ return $uri;
+ }
+ $baseComponents = $this->parse($baseUri);
+ $basePath = $baseComponents['path'];
+
+ $baseComponents['path'] = self::combineRelativePathWithBasePath($path, $basePath);
+ if (isset($components['fragment'])) {
+ $baseComponents['fragment'] = $components['fragment'];
+ }
+
+ return $this->generate($baseComponents);
+ }
+
+ /**
+ * Tries to glue a relative path onto an absolute one
+ *
+ * @param string $relativePath
+ * @param string $basePath
+ * @return string Merged path
+ * @throws UriResolverException
+ */
+ public static function combineRelativePathWithBasePath($relativePath, $basePath)
+ {
+ $relativePath = self::normalizePath($relativePath);
+ if ($relativePath == '') {
+ return $basePath;
+ }
+ if ($relativePath{0} == '/') {
+ return $relativePath;
+ }
+
+ $basePathSegments = explode('/', $basePath);
+
+ preg_match('|^/?(\.\./(?:\./)*)*|', $relativePath, $match);
+ $numLevelUp = strlen($match[0]) /3 + 1;
+ if ($numLevelUp >= count($basePathSegments)) {
+ throw new UriResolverException(sprintf("Unable to resolve URI '%s' from base '%s'", $relativePath, $basePath));
+ }
+
+ $basePathSegments = array_slice($basePathSegments, 0, -$numLevelUp);
+ $path = preg_replace('|^/?(\.\./(\./)*)*|', '', $relativePath);
+
+ return implode('/', $basePathSegments) . '/' . $path;
+ }
+
+ /**
+ * Normalizes a URI path component by removing dot-slash and double slashes
+ *
+ * @param string $path
+ * @return string
+ */
+ private static function normalizePath($path)
+ {
+ $path = preg_replace('|((?<!\.)\./)*|', '', $path);
+ $path = preg_replace('|//|', '/', $path);
+
+ return $path;
+ }
+
+ /**
+ * @param string $uri
+ * @return boolean
+ */
+ public function isValid($uri)
+ {
+ $components = $this->parse($uri);
+
+ return !empty($components);
+ }
+}