Completed
Push — develop ( 316159...00443b )
by Zack
20:22
created
json-schema/src/JsonSchema/Constraints/ConstraintInterface.php 1 patch
Indentation   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -18,48 +18,48 @@
 block discarded – undo
18 18
  */
19 19
 interface ConstraintInterface
20 20
 {
21
-    /**
22
-     * returns all collected errors
23
-     *
24
-     * @return array
25
-     */
26
-    public function getErrors();
21
+	/**
22
+	 * returns all collected errors
23
+	 *
24
+	 * @return array
25
+	 */
26
+	public function getErrors();
27 27
 
28
-    /**
29
-     * adds errors to this validator
30
-     *
31
-     * @param array $errors
32
-     */
33
-    public function addErrors(array $errors);
28
+	/**
29
+	 * adds errors to this validator
30
+	 *
31
+	 * @param array $errors
32
+	 */
33
+	public function addErrors(array $errors);
34 34
 
35
-    /**
36
-     * adds an error
37
-     *
38
-     * @param JsonPointer|null $path
39
-     * @param string           $message
40
-     * @param string           $constraint the constraint/rule that is broken, e.g.: 'minLength'
41
-     * @param array            $more       more array elements to add to the error
42
-     */
43
-    public function addError(JsonPointer $path = null, $message, $constraint='', array $more = null);
35
+	/**
36
+	 * adds an error
37
+	 *
38
+	 * @param JsonPointer|null $path
39
+	 * @param string           $message
40
+	 * @param string           $constraint the constraint/rule that is broken, e.g.: 'minLength'
41
+	 * @param array            $more       more array elements to add to the error
42
+	 */
43
+	public function addError(JsonPointer $path = null, $message, $constraint='', array $more = null);
44 44
 
45
-    /**
46
-     * checks if the validator has not raised errors
47
-     *
48
-     * @return bool
49
-     */
50
-    public function isValid();
45
+	/**
46
+	 * checks if the validator has not raised errors
47
+	 *
48
+	 * @return bool
49
+	 */
50
+	public function isValid();
51 51
 
52
-    /**
53
-     * invokes the validation of an element
54
-     *
55
-     * @abstract
56
-     *
57
-     * @param mixed            $value
58
-     * @param mixed            $schema
59
-     * @param JsonPointer|null $path
60
-     * @param mixed            $i
61
-     *
62
-     * @throws \JsonSchema\Exception\ExceptionInterface
63
-     */
64
-    public function check(&$value, $schema = null, JsonPointer $path = null, $i = null);
52
+	/**
53
+	 * invokes the validation of an element
54
+	 *
55
+	 * @abstract
56
+	 *
57
+	 * @param mixed            $value
58
+	 * @param mixed            $schema
59
+	 * @param JsonPointer|null $path
60
+	 * @param mixed            $i
61
+	 *
62
+	 * @throws \JsonSchema\Exception\ExceptionInterface
63
+	 */
64
+	public function check(&$value, $schema = null, JsonPointer $path = null, $i = null);
65 65
 }
Please login to merge, or discard this patch.
vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Factory.php 1 patch
Indentation   +196 added lines, -196 removed lines patch added patch discarded remove patch
@@ -21,200 +21,200 @@
 block discarded – undo
21 21
  */
22 22
 class Factory
23 23
 {
24
-    /**
25
-     * @var SchemaStorage
26
-     */
27
-    protected $schemaStorage;
28
-
29
-    /**
30
-     * @var UriRetriever
31
-     */
32
-    protected $uriRetriever;
33
-
34
-    /**
35
-     * @var int
36
-     */
37
-    private $checkMode = Constraint::CHECK_MODE_NORMAL;
38
-
39
-    /**
40
-     * @var TypeCheck\TypeCheckInterface[]
41
-     */
42
-    private $typeCheck = array();
43
-
44
-    /**
45
-     * @var int Validation context
46
-     */
47
-    protected $errorContext = Validator::ERROR_DOCUMENT_VALIDATION;
48
-
49
-    /**
50
-     * @var array
51
-     */
52
-    protected $constraintMap = array(
53
-        'array' => 'JsonSchema\Constraints\CollectionConstraint',
54
-        'collection' => 'JsonSchema\Constraints\CollectionConstraint',
55
-        'object' => 'JsonSchema\Constraints\ObjectConstraint',
56
-        'type' => 'JsonSchema\Constraints\TypeConstraint',
57
-        'undefined' => 'JsonSchema\Constraints\UndefinedConstraint',
58
-        'string' => 'JsonSchema\Constraints\StringConstraint',
59
-        'number' => 'JsonSchema\Constraints\NumberConstraint',
60
-        'enum' => 'JsonSchema\Constraints\EnumConstraint',
61
-        'format' => 'JsonSchema\Constraints\FormatConstraint',
62
-        'schema' => 'JsonSchema\Constraints\SchemaConstraint',
63
-        'validator' => 'JsonSchema\Validator'
64
-    );
65
-
66
-    /**
67
-     * @var array<ConstraintInterface>
68
-     */
69
-    private $instanceCache = array();
70
-
71
-    /**
72
-     * @param SchemaStorage         $schemaStorage
73
-     * @param UriRetrieverInterface $uriRetriever
74
-     * @param int                   $checkMode
75
-     */
76
-    public function __construct(
77
-        SchemaStorageInterface $schemaStorage = null,
78
-        UriRetrieverInterface $uriRetriever = null,
79
-        $checkMode = Constraint::CHECK_MODE_NORMAL
80
-    ) {
81
-        // set provided config options
82
-        $this->setConfig($checkMode);
83
-
84
-        $this->uriRetriever = $uriRetriever ?: new UriRetriever();
85
-        $this->schemaStorage = $schemaStorage ?: new SchemaStorage($this->uriRetriever);
86
-    }
87
-
88
-    /**
89
-     * Set config values
90
-     *
91
-     * @param int $checkMode Set checkMode options - does not preserve existing flags
92
-     */
93
-    public function setConfig($checkMode = Constraint::CHECK_MODE_NORMAL)
94
-    {
95
-        $this->checkMode = $checkMode;
96
-    }
97
-
98
-    /**
99
-     * Enable checkMode flags
100
-     *
101
-     * @param int $options
102
-     */
103
-    public function addConfig($options)
104
-    {
105
-        $this->checkMode |= $options;
106
-    }
107
-
108
-    /**
109
-     * Disable checkMode flags
110
-     *
111
-     * @param int $options
112
-     */
113
-    public function removeConfig($options)
114
-    {
115
-        $this->checkMode &= ~$options;
116
-    }
117
-
118
-    /**
119
-     * Get checkMode option
120
-     *
121
-     * @param int $options Options to get, if null then return entire bitmask
122
-     *
123
-     * @return int
124
-     */
125
-    public function getConfig($options = null)
126
-    {
127
-        if ($options === null) {
128
-            return $this->checkMode;
129
-        }
130
-
131
-        return $this->checkMode & $options;
132
-    }
133
-
134
-    /**
135
-     * @return UriRetrieverInterface
136
-     */
137
-    public function getUriRetriever()
138
-    {
139
-        return $this->uriRetriever;
140
-    }
141
-
142
-    public function getSchemaStorage()
143
-    {
144
-        return $this->schemaStorage;
145
-    }
146
-
147
-    public function getTypeCheck()
148
-    {
149
-        if (!isset($this->typeCheck[$this->checkMode])) {
150
-            $this->typeCheck[$this->checkMode] = ($this->checkMode & Constraint::CHECK_MODE_TYPE_CAST)
151
-                ? new TypeCheck\LooseTypeCheck()
152
-                : new TypeCheck\StrictTypeCheck();
153
-        }
154
-
155
-        return $this->typeCheck[$this->checkMode];
156
-    }
157
-
158
-    /**
159
-     * @param string $name
160
-     * @param string $class
161
-     *
162
-     * @return Factory
163
-     */
164
-    public function setConstraintClass($name, $class)
165
-    {
166
-        // Ensure class exists
167
-        if (!class_exists($class)) {
168
-            throw new InvalidArgumentException('Unknown constraint ' . $name);
169
-        }
170
-        // Ensure class is appropriate
171
-        if (!in_array('JsonSchema\Constraints\ConstraintInterface', class_implements($class))) {
172
-            throw new InvalidArgumentException('Invalid class ' . $name);
173
-        }
174
-        $this->constraintMap[$name] = $class;
175
-
176
-        return $this;
177
-    }
178
-
179
-    /**
180
-     * Create a constraint instance for the given constraint name.
181
-     *
182
-     * @param string $constraintName
183
-     *
184
-     * @throws InvalidArgumentException if is not possible create the constraint instance
185
-     *
186
-     * @return ConstraintInterface|ObjectConstraint
187
-     */
188
-    public function createInstanceFor($constraintName)
189
-    {
190
-        if (!isset($this->constraintMap[$constraintName])) {
191
-            throw new InvalidArgumentException('Unknown constraint ' . $constraintName);
192
-        }
193
-
194
-        if (!isset($this->instanceCache[$constraintName])) {
195
-            $this->instanceCache[$constraintName] = new $this->constraintMap[$constraintName]($this);
196
-        }
197
-
198
-        return clone $this->instanceCache[$constraintName];
199
-    }
200
-
201
-    /**
202
-     * Get the error context
203
-     *
204
-     * @return string
205
-     */
206
-    public function getErrorContext()
207
-    {
208
-        return $this->errorContext;
209
-    }
210
-
211
-    /**
212
-     * Set the error context
213
-     *
214
-     * @param string $validationContext
215
-     */
216
-    public function setErrorContext($errorContext)
217
-    {
218
-        $this->errorContext = $errorContext;
219
-    }
24
+	/**
25
+	 * @var SchemaStorage
26
+	 */
27
+	protected $schemaStorage;
28
+
29
+	/**
30
+	 * @var UriRetriever
31
+	 */
32
+	protected $uriRetriever;
33
+
34
+	/**
35
+	 * @var int
36
+	 */
37
+	private $checkMode = Constraint::CHECK_MODE_NORMAL;
38
+
39
+	/**
40
+	 * @var TypeCheck\TypeCheckInterface[]
41
+	 */
42
+	private $typeCheck = array();
43
+
44
+	/**
45
+	 * @var int Validation context
46
+	 */
47
+	protected $errorContext = Validator::ERROR_DOCUMENT_VALIDATION;
48
+
49
+	/**
50
+	 * @var array
51
+	 */
52
+	protected $constraintMap = array(
53
+		'array' => 'JsonSchema\Constraints\CollectionConstraint',
54
+		'collection' => 'JsonSchema\Constraints\CollectionConstraint',
55
+		'object' => 'JsonSchema\Constraints\ObjectConstraint',
56
+		'type' => 'JsonSchema\Constraints\TypeConstraint',
57
+		'undefined' => 'JsonSchema\Constraints\UndefinedConstraint',
58
+		'string' => 'JsonSchema\Constraints\StringConstraint',
59
+		'number' => 'JsonSchema\Constraints\NumberConstraint',
60
+		'enum' => 'JsonSchema\Constraints\EnumConstraint',
61
+		'format' => 'JsonSchema\Constraints\FormatConstraint',
62
+		'schema' => 'JsonSchema\Constraints\SchemaConstraint',
63
+		'validator' => 'JsonSchema\Validator'
64
+	);
65
+
66
+	/**
67
+	 * @var array<ConstraintInterface>
68
+	 */
69
+	private $instanceCache = array();
70
+
71
+	/**
72
+	 * @param SchemaStorage         $schemaStorage
73
+	 * @param UriRetrieverInterface $uriRetriever
74
+	 * @param int                   $checkMode
75
+	 */
76
+	public function __construct(
77
+		SchemaStorageInterface $schemaStorage = null,
78
+		UriRetrieverInterface $uriRetriever = null,
79
+		$checkMode = Constraint::CHECK_MODE_NORMAL
80
+	) {
81
+		// set provided config options
82
+		$this->setConfig($checkMode);
83
+
84
+		$this->uriRetriever = $uriRetriever ?: new UriRetriever();
85
+		$this->schemaStorage = $schemaStorage ?: new SchemaStorage($this->uriRetriever);
86
+	}
87
+
88
+	/**
89
+	 * Set config values
90
+	 *
91
+	 * @param int $checkMode Set checkMode options - does not preserve existing flags
92
+	 */
93
+	public function setConfig($checkMode = Constraint::CHECK_MODE_NORMAL)
94
+	{
95
+		$this->checkMode = $checkMode;
96
+	}
97
+
98
+	/**
99
+	 * Enable checkMode flags
100
+	 *
101
+	 * @param int $options
102
+	 */
103
+	public function addConfig($options)
104
+	{
105
+		$this->checkMode |= $options;
106
+	}
107
+
108
+	/**
109
+	 * Disable checkMode flags
110
+	 *
111
+	 * @param int $options
112
+	 */
113
+	public function removeConfig($options)
114
+	{
115
+		$this->checkMode &= ~$options;
116
+	}
117
+
118
+	/**
119
+	 * Get checkMode option
120
+	 *
121
+	 * @param int $options Options to get, if null then return entire bitmask
122
+	 *
123
+	 * @return int
124
+	 */
125
+	public function getConfig($options = null)
126
+	{
127
+		if ($options === null) {
128
+			return $this->checkMode;
129
+		}
130
+
131
+		return $this->checkMode & $options;
132
+	}
133
+
134
+	/**
135
+	 * @return UriRetrieverInterface
136
+	 */
137
+	public function getUriRetriever()
138
+	{
139
+		return $this->uriRetriever;
140
+	}
141
+
142
+	public function getSchemaStorage()
143
+	{
144
+		return $this->schemaStorage;
145
+	}
146
+
147
+	public function getTypeCheck()
148
+	{
149
+		if (!isset($this->typeCheck[$this->checkMode])) {
150
+			$this->typeCheck[$this->checkMode] = ($this->checkMode & Constraint::CHECK_MODE_TYPE_CAST)
151
+				? new TypeCheck\LooseTypeCheck()
152
+				: new TypeCheck\StrictTypeCheck();
153
+		}
154
+
155
+		return $this->typeCheck[$this->checkMode];
156
+	}
157
+
158
+	/**
159
+	 * @param string $name
160
+	 * @param string $class
161
+	 *
162
+	 * @return Factory
163
+	 */
164
+	public function setConstraintClass($name, $class)
165
+	{
166
+		// Ensure class exists
167
+		if (!class_exists($class)) {
168
+			throw new InvalidArgumentException('Unknown constraint ' . $name);
169
+		}
170
+		// Ensure class is appropriate
171
+		if (!in_array('JsonSchema\Constraints\ConstraintInterface', class_implements($class))) {
172
+			throw new InvalidArgumentException('Invalid class ' . $name);
173
+		}
174
+		$this->constraintMap[$name] = $class;
175
+
176
+		return $this;
177
+	}
178
+
179
+	/**
180
+	 * Create a constraint instance for the given constraint name.
181
+	 *
182
+	 * @param string $constraintName
183
+	 *
184
+	 * @throws InvalidArgumentException if is not possible create the constraint instance
185
+	 *
186
+	 * @return ConstraintInterface|ObjectConstraint
187
+	 */
188
+	public function createInstanceFor($constraintName)
189
+	{
190
+		if (!isset($this->constraintMap[$constraintName])) {
191
+			throw new InvalidArgumentException('Unknown constraint ' . $constraintName);
192
+		}
193
+
194
+		if (!isset($this->instanceCache[$constraintName])) {
195
+			$this->instanceCache[$constraintName] = new $this->constraintMap[$constraintName]($this);
196
+		}
197
+
198
+		return clone $this->instanceCache[$constraintName];
199
+	}
200
+
201
+	/**
202
+	 * Get the error context
203
+	 *
204
+	 * @return string
205
+	 */
206
+	public function getErrorContext()
207
+	{
208
+		return $this->errorContext;
209
+	}
210
+
211
+	/**
212
+	 * Set the error context
213
+	 *
214
+	 * @param string $validationContext
215
+	 */
216
+	public function setErrorContext($errorContext)
217
+	{
218
+		$this->errorContext = $errorContext;
219
+	}
220 220
 }
Please login to merge, or discard this patch.
vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/Curl.php 1 patch
Indentation   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -19,65 +19,65 @@
 block discarded – undo
19 19
  */
20 20
 class Curl extends AbstractRetriever
21 21
 {
22
-    protected $messageBody;
22
+	protected $messageBody;
23 23
 
24
-    public function __construct()
25
-    {
26
-        if (!function_exists('curl_init')) {
27
-            // Cannot test this, because curl_init is present on all test platforms plus mock
28
-            throw new RuntimeException('cURL not installed'); // @codeCoverageIgnore
29
-        }
30
-    }
24
+	public function __construct()
25
+	{
26
+		if (!function_exists('curl_init')) {
27
+			// Cannot test this, because curl_init is present on all test platforms plus mock
28
+			throw new RuntimeException('cURL not installed'); // @codeCoverageIgnore
29
+		}
30
+	}
31 31
 
32
-    /**
33
-     * {@inheritdoc}
34
-     *
35
-     * @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve()
36
-     */
37
-    public function retrieve($uri)
38
-    {
39
-        $ch = curl_init();
32
+	/**
33
+	 * {@inheritdoc}
34
+	 *
35
+	 * @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve()
36
+	 */
37
+	public function retrieve($uri)
38
+	{
39
+		$ch = curl_init();
40 40
 
41
-        curl_setopt($ch, CURLOPT_URL, $uri);
42
-        curl_setopt($ch, CURLOPT_HEADER, true);
43
-        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
44
-        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: ' . Validator::SCHEMA_MEDIA_TYPE));
41
+		curl_setopt($ch, CURLOPT_URL, $uri);
42
+		curl_setopt($ch, CURLOPT_HEADER, true);
43
+		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
44
+		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: ' . Validator::SCHEMA_MEDIA_TYPE));
45 45
 
46
-        $response = curl_exec($ch);
47
-        if (false === $response) {
48
-            throw new \JsonSchema\Exception\ResourceNotFoundException('JSON schema not found');
49
-        }
46
+		$response = curl_exec($ch);
47
+		if (false === $response) {
48
+			throw new \JsonSchema\Exception\ResourceNotFoundException('JSON schema not found');
49
+		}
50 50
 
51
-        $this->fetchMessageBody($response);
52
-        $this->fetchContentType($response);
51
+		$this->fetchMessageBody($response);
52
+		$this->fetchContentType($response);
53 53
 
54
-        curl_close($ch);
54
+		curl_close($ch);
55 55
 
56
-        return $this->messageBody;
57
-    }
56
+		return $this->messageBody;
57
+	}
58 58
 
59
-    /**
60
-     * @param string $response cURL HTTP response
61
-     */
62
-    private function fetchMessageBody($response)
63
-    {
64
-        preg_match("/(?:\r\n){2}(.*)$/ms", $response, $match);
65
-        $this->messageBody = $match[1];
66
-    }
59
+	/**
60
+	 * @param string $response cURL HTTP response
61
+	 */
62
+	private function fetchMessageBody($response)
63
+	{
64
+		preg_match("/(?:\r\n){2}(.*)$/ms", $response, $match);
65
+		$this->messageBody = $match[1];
66
+	}
67 67
 
68
-    /**
69
-     * @param string $response cURL HTTP response
70
-     *
71
-     * @return bool Whether the Content-Type header was found or not
72
-     */
73
-    protected function fetchContentType($response)
74
-    {
75
-        if (0 < preg_match("/Content-Type:(\V*)/ims", $response, $match)) {
76
-            $this->contentType = trim($match[1]);
68
+	/**
69
+	 * @param string $response cURL HTTP response
70
+	 *
71
+	 * @return bool Whether the Content-Type header was found or not
72
+	 */
73
+	protected function fetchContentType($response)
74
+	{
75
+		if (0 < preg_match("/Content-Type:(\V*)/ims", $response, $match)) {
76
+			$this->contentType = trim($match[1]);
77 77
 
78
-            return true;
79
-        }
78
+			return true;
79
+		}
80 80
 
81
-        return false;
82
-    }
81
+		return false;
82
+	}
83 83
 }
Please login to merge, or discard this patch.
json-schema/src/JsonSchema/Uri/Retrievers/AbstractRetriever.php 1 patch
Indentation   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -15,20 +15,20 @@
 block discarded – undo
15 15
  */
16 16
 abstract class AbstractRetriever implements UriRetrieverInterface
17 17
 {
18
-    /**
19
-     * Media content type
20
-     *
21
-     * @var string
22
-     */
23
-    protected $contentType;
18
+	/**
19
+	 * Media content type
20
+	 *
21
+	 * @var string
22
+	 */
23
+	protected $contentType;
24 24
 
25
-    /**
26
-     * {@inheritdoc}
27
-     *
28
-     * @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::getContentType()
29
-     */
30
-    public function getContentType()
31
-    {
32
-        return $this->contentType;
33
-    }
25
+	/**
26
+	 * {@inheritdoc}
27
+	 *
28
+	 * @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::getContentType()
29
+	 */
30
+	public function getContentType()
31
+	{
32
+		return $this->contentType;
33
+	}
34 34
 }
Please login to merge, or discard this patch.
justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/PredefinedArray.php 1 patch
Indentation   +32 added lines, -32 removed lines patch added patch discarded remove patch
@@ -18,39 +18,39 @@
 block discarded – undo
18 18
  */
19 19
 class PredefinedArray extends AbstractRetriever
20 20
 {
21
-    /**
22
-     * Contains schemas as URI => JSON
23
-     *
24
-     * @var array
25
-     */
26
-    private $schemas;
21
+	/**
22
+	 * Contains schemas as URI => JSON
23
+	 *
24
+	 * @var array
25
+	 */
26
+	private $schemas;
27 27
 
28
-    /**
29
-     * Constructor
30
-     *
31
-     * @param array  $schemas
32
-     * @param string $contentType
33
-     */
34
-    public function __construct(array $schemas, $contentType = Validator::SCHEMA_MEDIA_TYPE)
35
-    {
36
-        $this->schemas     = $schemas;
37
-        $this->contentType = $contentType;
38
-    }
28
+	/**
29
+	 * Constructor
30
+	 *
31
+	 * @param array  $schemas
32
+	 * @param string $contentType
33
+	 */
34
+	public function __construct(array $schemas, $contentType = Validator::SCHEMA_MEDIA_TYPE)
35
+	{
36
+		$this->schemas     = $schemas;
37
+		$this->contentType = $contentType;
38
+	}
39 39
 
40
-    /**
41
-     * {@inheritdoc}
42
-     *
43
-     * @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve()
44
-     */
45
-    public function retrieve($uri)
46
-    {
47
-        if (!array_key_exists($uri, $this->schemas)) {
48
-            throw new \JsonSchema\Exception\ResourceNotFoundException(sprintf(
49
-                'The JSON schema "%s" was not found.',
50
-                $uri
51
-            ));
52
-        }
40
+	/**
41
+	 * {@inheritdoc}
42
+	 *
43
+	 * @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve()
44
+	 */
45
+	public function retrieve($uri)
46
+	{
47
+		if (!array_key_exists($uri, $this->schemas)) {
48
+			throw new \JsonSchema\Exception\ResourceNotFoundException(sprintf(
49
+				'The JSON schema "%s" was not found.',
50
+				$uri
51
+			));
52
+		}
53 53
 
54
-        return $this->schemas[$uri];
55
-    }
54
+		return $this->schemas[$uri];
55
+	}
56 56
 }
Please login to merge, or discard this patch.
justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/FileGetContents.php 1 patch
Indentation   +62 added lines, -62 removed lines patch added patch discarded remove patch
@@ -18,76 +18,76 @@
 block discarded – undo
18 18
  */
19 19
 class FileGetContents extends AbstractRetriever
20 20
 {
21
-    protected $messageBody;
21
+	protected $messageBody;
22 22
 
23
-    /**
24
-     * {@inheritdoc}
25
-     *
26
-     * @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve()
27
-     */
28
-    public function retrieve($uri)
29
-    {
30
-        $errorMessage = null;
31
-        set_error_handler(function ($errno, $errstr) use (&$errorMessage) {
32
-            $errorMessage = $errstr;
33
-        });
34
-        $response = file_get_contents($uri);
35
-        restore_error_handler();
23
+	/**
24
+	 * {@inheritdoc}
25
+	 *
26
+	 * @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve()
27
+	 */
28
+	public function retrieve($uri)
29
+	{
30
+		$errorMessage = null;
31
+		set_error_handler(function ($errno, $errstr) use (&$errorMessage) {
32
+			$errorMessage = $errstr;
33
+		});
34
+		$response = file_get_contents($uri);
35
+		restore_error_handler();
36 36
 
37
-        if ($errorMessage) {
38
-            throw new ResourceNotFoundException($errorMessage);
39
-        }
37
+		if ($errorMessage) {
38
+			throw new ResourceNotFoundException($errorMessage);
39
+		}
40 40
 
41
-        if (false === $response) {
42
-            throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
43
-        }
41
+		if (false === $response) {
42
+			throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
43
+		}
44 44
 
45
-        if ($response == ''
46
-            && substr($uri, 0, 7) == 'file://' && substr($uri, -1) == '/'
47
-        ) {
48
-            throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
49
-        }
45
+		if ($response == ''
46
+			&& substr($uri, 0, 7) == 'file://' && substr($uri, -1) == '/'
47
+		) {
48
+			throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
49
+		}
50 50
 
51
-        $this->messageBody = $response;
52
-        if (!empty($http_response_header)) {
53
-            // $http_response_header cannot be tested, because it's defined in the method's local scope
54
-            // See http://php.net/manual/en/reserved.variables.httpresponseheader.php for more info.
55
-            $this->fetchContentType($http_response_header); // @codeCoverageIgnore
56
-        } else {                                            // @codeCoverageIgnore
57
-            // Could be a "file://" url or something else - fake up the response
58
-            $this->contentType = null;
59
-        }
51
+		$this->messageBody = $response;
52
+		if (!empty($http_response_header)) {
53
+			// $http_response_header cannot be tested, because it's defined in the method's local scope
54
+			// See http://php.net/manual/en/reserved.variables.httpresponseheader.php for more info.
55
+			$this->fetchContentType($http_response_header); // @codeCoverageIgnore
56
+		} else {                                            // @codeCoverageIgnore
57
+			// Could be a "file://" url or something else - fake up the response
58
+			$this->contentType = null;
59
+		}
60 60
 
61
-        return $this->messageBody;
62
-    }
61
+		return $this->messageBody;
62
+	}
63 63
 
64
-    /**
65
-     * @param array $headers HTTP Response Headers
66
-     *
67
-     * @return bool Whether the Content-Type header was found or not
68
-     */
69
-    private function fetchContentType(array $headers)
70
-    {
71
-        foreach ($headers as $header) {
72
-            if ($this->contentType = self::getContentTypeMatchInHeader($header)) {
73
-                return true;
74
-            }
75
-        }
64
+	/**
65
+	 * @param array $headers HTTP Response Headers
66
+	 *
67
+	 * @return bool Whether the Content-Type header was found or not
68
+	 */
69
+	private function fetchContentType(array $headers)
70
+	{
71
+		foreach ($headers as $header) {
72
+			if ($this->contentType = self::getContentTypeMatchInHeader($header)) {
73
+				return true;
74
+			}
75
+		}
76 76
 
77
-        return false;
78
-    }
77
+		return false;
78
+	}
79 79
 
80
-    /**
81
-     * @param string $header
82
-     *
83
-     * @return string|null
84
-     */
85
-    protected static function getContentTypeMatchInHeader($header)
86
-    {
87
-        if (0 < preg_match("/Content-Type:(\V*)/ims", $header, $match)) {
88
-            return trim($match[1]);
89
-        }
80
+	/**
81
+	 * @param string $header
82
+	 *
83
+	 * @return string|null
84
+	 */
85
+	protected static function getContentTypeMatchInHeader($header)
86
+	{
87
+		if (0 < preg_match("/Content-Type:(\V*)/ims", $header, $match)) {
88
+			return trim($match[1]);
89
+		}
90 90
 
91
-        return null;
92
-    }
91
+		return null;
92
+	}
93 93
 }
Please login to merge, or discard this patch.
json-schema/src/JsonSchema/Uri/Retrievers/UriRetrieverInterface.php 1 patch
Indentation   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -16,21 +16,21 @@
 block discarded – undo
16 16
  */
17 17
 interface UriRetrieverInterface
18 18
 {
19
-    /**
20
-     * Retrieve a schema from the specified URI
21
-     *
22
-     * @param string $uri URI that resolves to a JSON schema
23
-     *
24
-     * @throws \JsonSchema\Exception\ResourceNotFoundException
25
-     *
26
-     * @return mixed string|null
27
-     */
28
-    public function retrieve($uri);
19
+	/**
20
+	 * Retrieve a schema from the specified URI
21
+	 *
22
+	 * @param string $uri URI that resolves to a JSON schema
23
+	 *
24
+	 * @throws \JsonSchema\Exception\ResourceNotFoundException
25
+	 *
26
+	 * @return mixed string|null
27
+	 */
28
+	public function retrieve($uri);
29 29
 
30
-    /**
31
-     * Get media content type
32
-     *
33
-     * @return string
34
-     */
35
-    public function getContentType();
30
+	/**
31
+	 * Get media content type
32
+	 *
33
+	 * @return string
34
+	 */
35
+	public function getContentType();
36 36
 }
Please login to merge, or discard this patch.
vendor/justinrainbow/json-schema/src/JsonSchema/Uri/UriResolver.php 1 patch
Indentation   +153 added lines, -153 removed lines patch added patch discarded remove patch
@@ -19,157 +19,157 @@
 block discarded – undo
19 19
  */
20 20
 class UriResolver implements UriResolverInterface
21 21
 {
22
-    /**
23
-     * Parses a URI into five main components
24
-     *
25
-     * @param string $uri
26
-     *
27
-     * @return array
28
-     */
29
-    public function parse($uri)
30
-    {
31
-        preg_match('|^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?|', $uri, $match);
32
-
33
-        $components = array();
34
-        if (5 < count($match)) {
35
-            $components =  array(
36
-                'scheme'    => $match[2],
37
-                'authority' => $match[4],
38
-                'path'      => $match[5]
39
-            );
40
-        }
41
-        if (7 < count($match)) {
42
-            $components['query'] = $match[7];
43
-        }
44
-        if (9 < count($match)) {
45
-            $components['fragment'] = $match[9];
46
-        }
47
-
48
-        return $components;
49
-    }
50
-
51
-    /**
52
-     * Builds a URI based on n array with the main components
53
-     *
54
-     * @param array $components
55
-     *
56
-     * @return string
57
-     */
58
-    public function generate(array $components)
59
-    {
60
-        $uri = $components['scheme'] . '://'
61
-             . $components['authority']
62
-             . $components['path'];
63
-
64
-        if (array_key_exists('query', $components) && strlen($components['query'])) {
65
-            $uri .= '?' . $components['query'];
66
-        }
67
-        if (array_key_exists('fragment', $components)) {
68
-            $uri .= '#' . $components['fragment'];
69
-        }
70
-
71
-        return $uri;
72
-    }
73
-
74
-    /**
75
-     * {@inheritdoc}
76
-     */
77
-    public function resolve($uri, $baseUri = null)
78
-    {
79
-        // treat non-uri base as local file path
80
-        if (
81
-            !is_null($baseUri) &&
82
-            !filter_var($baseUri, \FILTER_VALIDATE_URL) &&
83
-            !preg_match('|^[^/]+://|u', $baseUri)
84
-        ) {
85
-            if (is_file($baseUri)) {
86
-                $baseUri = 'file://' . realpath($baseUri);
87
-            } elseif (is_dir($baseUri)) {
88
-                $baseUri = 'file://' . realpath($baseUri) . '/';
89
-            } else {
90
-                $baseUri = 'file://' . getcwd() . '/' . $baseUri;
91
-            }
92
-        }
93
-
94
-        if ($uri == '') {
95
-            return $baseUri;
96
-        }
97
-
98
-        $components = $this->parse($uri);
99
-        $path = $components['path'];
100
-
101
-        if (!empty($components['scheme'])) {
102
-            return $uri;
103
-        }
104
-        $baseComponents = $this->parse($baseUri);
105
-        $basePath = $baseComponents['path'];
106
-
107
-        $baseComponents['path'] = self::combineRelativePathWithBasePath($path, $basePath);
108
-        if (isset($components['fragment'])) {
109
-            $baseComponents['fragment'] = $components['fragment'];
110
-        }
111
-
112
-        return $this->generate($baseComponents);
113
-    }
114
-
115
-    /**
116
-     * Tries to glue a relative path onto an absolute one
117
-     *
118
-     * @param string $relativePath
119
-     * @param string $basePath
120
-     *
121
-     * @throws UriResolverException
122
-     *
123
-     * @return string Merged path
124
-     */
125
-    public static function combineRelativePathWithBasePath($relativePath, $basePath)
126
-    {
127
-        $relativePath = self::normalizePath($relativePath);
128
-        if ($relativePath == '') {
129
-            return $basePath;
130
-        }
131
-        if ($relativePath[0] == '/') {
132
-            return $relativePath;
133
-        }
134
-
135
-        $basePathSegments = explode('/', $basePath);
136
-
137
-        preg_match('|^/?(\.\./(?:\./)*)*|', $relativePath, $match);
138
-        $numLevelUp = strlen($match[0]) /3 + 1;
139
-        if ($numLevelUp >= count($basePathSegments)) {
140
-            throw new UriResolverException(sprintf("Unable to resolve URI '%s' from base '%s'", $relativePath, $basePath));
141
-        }
142
-
143
-        $basePathSegments = array_slice($basePathSegments, 0, -$numLevelUp);
144
-        $path = preg_replace('|^/?(\.\./(\./)*)*|', '', $relativePath);
145
-
146
-        return implode('/', $basePathSegments) . '/' . $path;
147
-    }
148
-
149
-    /**
150
-     * Normalizes a URI path component by removing dot-slash and double slashes
151
-     *
152
-     * @param string $path
153
-     *
154
-     * @return string
155
-     */
156
-    private static function normalizePath($path)
157
-    {
158
-        $path = preg_replace('|((?<!\.)\./)*|', '', $path);
159
-        $path = preg_replace('|//|', '/', $path);
160
-
161
-        return $path;
162
-    }
163
-
164
-    /**
165
-     * @param string $uri
166
-     *
167
-     * @return bool
168
-     */
169
-    public function isValid($uri)
170
-    {
171
-        $components = $this->parse($uri);
172
-
173
-        return !empty($components);
174
-    }
22
+	/**
23
+	 * Parses a URI into five main components
24
+	 *
25
+	 * @param string $uri
26
+	 *
27
+	 * @return array
28
+	 */
29
+	public function parse($uri)
30
+	{
31
+		preg_match('|^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?|', $uri, $match);
32
+
33
+		$components = array();
34
+		if (5 < count($match)) {
35
+			$components =  array(
36
+				'scheme'    => $match[2],
37
+				'authority' => $match[4],
38
+				'path'      => $match[5]
39
+			);
40
+		}
41
+		if (7 < count($match)) {
42
+			$components['query'] = $match[7];
43
+		}
44
+		if (9 < count($match)) {
45
+			$components['fragment'] = $match[9];
46
+		}
47
+
48
+		return $components;
49
+	}
50
+
51
+	/**
52
+	 * Builds a URI based on n array with the main components
53
+	 *
54
+	 * @param array $components
55
+	 *
56
+	 * @return string
57
+	 */
58
+	public function generate(array $components)
59
+	{
60
+		$uri = $components['scheme'] . '://'
61
+			 . $components['authority']
62
+			 . $components['path'];
63
+
64
+		if (array_key_exists('query', $components) && strlen($components['query'])) {
65
+			$uri .= '?' . $components['query'];
66
+		}
67
+		if (array_key_exists('fragment', $components)) {
68
+			$uri .= '#' . $components['fragment'];
69
+		}
70
+
71
+		return $uri;
72
+	}
73
+
74
+	/**
75
+	 * {@inheritdoc}
76
+	 */
77
+	public function resolve($uri, $baseUri = null)
78
+	{
79
+		// treat non-uri base as local file path
80
+		if (
81
+			!is_null($baseUri) &&
82
+			!filter_var($baseUri, \FILTER_VALIDATE_URL) &&
83
+			!preg_match('|^[^/]+://|u', $baseUri)
84
+		) {
85
+			if (is_file($baseUri)) {
86
+				$baseUri = 'file://' . realpath($baseUri);
87
+			} elseif (is_dir($baseUri)) {
88
+				$baseUri = 'file://' . realpath($baseUri) . '/';
89
+			} else {
90
+				$baseUri = 'file://' . getcwd() . '/' . $baseUri;
91
+			}
92
+		}
93
+
94
+		if ($uri == '') {
95
+			return $baseUri;
96
+		}
97
+
98
+		$components = $this->parse($uri);
99
+		$path = $components['path'];
100
+
101
+		if (!empty($components['scheme'])) {
102
+			return $uri;
103
+		}
104
+		$baseComponents = $this->parse($baseUri);
105
+		$basePath = $baseComponents['path'];
106
+
107
+		$baseComponents['path'] = self::combineRelativePathWithBasePath($path, $basePath);
108
+		if (isset($components['fragment'])) {
109
+			$baseComponents['fragment'] = $components['fragment'];
110
+		}
111
+
112
+		return $this->generate($baseComponents);
113
+	}
114
+
115
+	/**
116
+	 * Tries to glue a relative path onto an absolute one
117
+	 *
118
+	 * @param string $relativePath
119
+	 * @param string $basePath
120
+	 *
121
+	 * @throws UriResolverException
122
+	 *
123
+	 * @return string Merged path
124
+	 */
125
+	public static function combineRelativePathWithBasePath($relativePath, $basePath)
126
+	{
127
+		$relativePath = self::normalizePath($relativePath);
128
+		if ($relativePath == '') {
129
+			return $basePath;
130
+		}
131
+		if ($relativePath[0] == '/') {
132
+			return $relativePath;
133
+		}
134
+
135
+		$basePathSegments = explode('/', $basePath);
136
+
137
+		preg_match('|^/?(\.\./(?:\./)*)*|', $relativePath, $match);
138
+		$numLevelUp = strlen($match[0]) /3 + 1;
139
+		if ($numLevelUp >= count($basePathSegments)) {
140
+			throw new UriResolverException(sprintf("Unable to resolve URI '%s' from base '%s'", $relativePath, $basePath));
141
+		}
142
+
143
+		$basePathSegments = array_slice($basePathSegments, 0, -$numLevelUp);
144
+		$path = preg_replace('|^/?(\.\./(\./)*)*|', '', $relativePath);
145
+
146
+		return implode('/', $basePathSegments) . '/' . $path;
147
+	}
148
+
149
+	/**
150
+	 * Normalizes a URI path component by removing dot-slash and double slashes
151
+	 *
152
+	 * @param string $path
153
+	 *
154
+	 * @return string
155
+	 */
156
+	private static function normalizePath($path)
157
+	{
158
+		$path = preg_replace('|((?<!\.)\./)*|', '', $path);
159
+		$path = preg_replace('|//|', '/', $path);
160
+
161
+		return $path;
162
+	}
163
+
164
+	/**
165
+	 * @param string $uri
166
+	 *
167
+	 * @return bool
168
+	 */
169
+	public function isValid($uri)
170
+	{
171
+		$components = $this->parse($uri);
172
+
173
+		return !empty($components);
174
+	}
175 175
 }
Please login to merge, or discard this patch.
vendor/justinrainbow/json-schema/src/JsonSchema/Uri/UriRetriever.php 1 patch
Indentation   +322 added lines, -322 removed lines patch added patch discarded remove patch
@@ -24,326 +24,326 @@
 block discarded – undo
24 24
  */
25 25
 class UriRetriever implements BaseUriRetrieverInterface
26 26
 {
27
-    /**
28
-     * @var array Map of URL translations
29
-     */
30
-    protected $translationMap = array(
31
-        // use local copies of the spec schemas
32
-        '|^https?://json-schema.org/draft-(0[34])/schema#?|' => 'package://dist/schema/json-schema-draft-$1.json'
33
-    );
34
-
35
-    /**
36
-     * @var array A list of endpoints for media type check exclusion
37
-     */
38
-    protected $allowedInvalidContentTypeEndpoints = array(
39
-        'http://json-schema.org/',
40
-        'https://json-schema.org/'
41
-    );
42
-
43
-    /**
44
-     * @var null|UriRetrieverInterface
45
-     */
46
-    protected $uriRetriever = null;
47
-
48
-    /**
49
-     * @var array|object[]
50
-     *
51
-     * @see loadSchema
52
-     */
53
-    private $schemaCache = array();
54
-
55
-    /**
56
-     * Adds an endpoint to the media type validation exclusion list
57
-     *
58
-     * @param string $endpoint
59
-     */
60
-    public function addInvalidContentTypeEndpoint($endpoint)
61
-    {
62
-        $this->allowedInvalidContentTypeEndpoints[] = $endpoint;
63
-    }
64
-
65
-    /**
66
-     * Guarantee the correct media type was encountered
67
-     *
68
-     * @param UriRetrieverInterface $uriRetriever
69
-     * @param string                $uri
70
-     *
71
-     * @return bool|void
72
-     */
73
-    public function confirmMediaType($uriRetriever, $uri)
74
-    {
75
-        $contentType = $uriRetriever->getContentType();
76
-
77
-        if (is_null($contentType)) {
78
-            // Well, we didn't get an invalid one
79
-            return;
80
-        }
81
-
82
-        if (in_array($contentType, array(Validator::SCHEMA_MEDIA_TYPE, 'application/json'))) {
83
-            return;
84
-        }
85
-
86
-        foreach ($this->allowedInvalidContentTypeEndpoints as $endpoint) {
87
-            if (strpos($uri, $endpoint) === 0) {
88
-                return true;
89
-            }
90
-        }
91
-
92
-        throw new InvalidSchemaMediaTypeException(sprintf('Media type %s expected', Validator::SCHEMA_MEDIA_TYPE));
93
-    }
94
-
95
-    /**
96
-     * Get a URI Retriever
97
-     *
98
-     * If none is specified, sets a default FileGetContents retriever and
99
-     * returns that object.
100
-     *
101
-     * @return UriRetrieverInterface
102
-     */
103
-    public function getUriRetriever()
104
-    {
105
-        if (is_null($this->uriRetriever)) {
106
-            $this->setUriRetriever(new FileGetContents());
107
-        }
108
-
109
-        return $this->uriRetriever;
110
-    }
111
-
112
-    /**
113
-     * Resolve a schema based on pointer
114
-     *
115
-     * URIs can have a fragment at the end in the format of
116
-     * #/path/to/object and we are to look up the 'path' property of
117
-     * the first object then the 'to' and 'object' properties.
118
-     *
119
-     * @param object $jsonSchema JSON Schema contents
120
-     * @param string $uri        JSON Schema URI
121
-     *
122
-     * @throws ResourceNotFoundException
123
-     *
124
-     * @return object JSON Schema after walking down the fragment pieces
125
-     */
126
-    public function resolvePointer($jsonSchema, $uri)
127
-    {
128
-        $resolver = new UriResolver();
129
-        $parsed = $resolver->parse($uri);
130
-        if (empty($parsed['fragment'])) {
131
-            return $jsonSchema;
132
-        }
133
-
134
-        $path = explode('/', $parsed['fragment']);
135
-        while ($path) {
136
-            $pathElement = array_shift($path);
137
-            if (!empty($pathElement)) {
138
-                $pathElement = str_replace('~1', '/', $pathElement);
139
-                $pathElement = str_replace('~0', '~', $pathElement);
140
-                if (!empty($jsonSchema->$pathElement)) {
141
-                    $jsonSchema = $jsonSchema->$pathElement;
142
-                } else {
143
-                    throw new ResourceNotFoundException(
144
-                        'Fragment "' . $parsed['fragment'] . '" not found'
145
-                        . ' in ' . $uri
146
-                    );
147
-                }
148
-
149
-                if (!is_object($jsonSchema)) {
150
-                    throw new ResourceNotFoundException(
151
-                        'Fragment part "' . $pathElement . '" is no object '
152
-                        . ' in ' . $uri
153
-                    );
154
-                }
155
-            }
156
-        }
157
-
158
-        return $jsonSchema;
159
-    }
160
-
161
-    /**
162
-     * {@inheritdoc}
163
-     */
164
-    public function retrieve($uri, $baseUri = null, $translate = true)
165
-    {
166
-        $resolver = new UriResolver();
167
-        $resolvedUri = $fetchUri = $resolver->resolve($uri, $baseUri);
168
-
169
-        //fetch URL without #fragment
170
-        $arParts = $resolver->parse($resolvedUri);
171
-        if (isset($arParts['fragment'])) {
172
-            unset($arParts['fragment']);
173
-            $fetchUri = $resolver->generate($arParts);
174
-        }
175
-
176
-        // apply URI translations
177
-        if ($translate) {
178
-            $fetchUri = $this->translate($fetchUri);
179
-        }
180
-
181
-        $jsonSchema = $this->loadSchema($fetchUri);
182
-
183
-        // Use the JSON pointer if specified
184
-        $jsonSchema = $this->resolvePointer($jsonSchema, $resolvedUri);
185
-
186
-        if ($jsonSchema instanceof \stdClass) {
187
-            $jsonSchema->id = $resolvedUri;
188
-        }
189
-
190
-        return $jsonSchema;
191
-    }
192
-
193
-    /**
194
-     * Fetch a schema from the given URI, json-decode it and return it.
195
-     * Caches schema objects.
196
-     *
197
-     * @param string $fetchUri Absolute URI
198
-     *
199
-     * @return object JSON schema object
200
-     */
201
-    protected function loadSchema($fetchUri)
202
-    {
203
-        if (isset($this->schemaCache[$fetchUri])) {
204
-            return $this->schemaCache[$fetchUri];
205
-        }
206
-
207
-        $uriRetriever = $this->getUriRetriever();
208
-        $contents = $this->uriRetriever->retrieve($fetchUri);
209
-        $this->confirmMediaType($uriRetriever, $fetchUri);
210
-        $jsonSchema = json_decode($contents);
211
-
212
-        if (JSON_ERROR_NONE < $error = json_last_error()) {
213
-            throw new JsonDecodingException($error);
214
-        }
215
-
216
-        $this->schemaCache[$fetchUri] = $jsonSchema;
217
-
218
-        return $jsonSchema;
219
-    }
220
-
221
-    /**
222
-     * Set the URI Retriever
223
-     *
224
-     * @param UriRetrieverInterface $uriRetriever
225
-     *
226
-     * @return $this for chaining
227
-     */
228
-    public function setUriRetriever(UriRetrieverInterface $uriRetriever)
229
-    {
230
-        $this->uriRetriever = $uriRetriever;
231
-
232
-        return $this;
233
-    }
234
-
235
-    /**
236
-     * Parses a URI into five main components
237
-     *
238
-     * @param string $uri
239
-     *
240
-     * @return array
241
-     */
242
-    public function parse($uri)
243
-    {
244
-        preg_match('|^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?|', $uri, $match);
245
-
246
-        $components = array();
247
-        if (5 < count($match)) {
248
-            $components =  array(
249
-                'scheme'    => $match[2],
250
-                'authority' => $match[4],
251
-                'path'      => $match[5]
252
-            );
253
-        }
254
-
255
-        if (7 < count($match)) {
256
-            $components['query'] = $match[7];
257
-        }
258
-
259
-        if (9 < count($match)) {
260
-            $components['fragment'] = $match[9];
261
-        }
262
-
263
-        return $components;
264
-    }
265
-
266
-    /**
267
-     * Builds a URI based on n array with the main components
268
-     *
269
-     * @param array $components
270
-     *
271
-     * @return string
272
-     */
273
-    public function generate(array $components)
274
-    {
275
-        $uri = $components['scheme'] . '://'
276
-             . $components['authority']
277
-             . $components['path'];
278
-
279
-        if (array_key_exists('query', $components)) {
280
-            $uri .= $components['query'];
281
-        }
282
-
283
-        if (array_key_exists('fragment', $components)) {
284
-            $uri .= $components['fragment'];
285
-        }
286
-
287
-        return $uri;
288
-    }
289
-
290
-    /**
291
-     * Resolves a URI
292
-     *
293
-     * @param string $uri     Absolute or relative
294
-     * @param string $baseUri Optional base URI
295
-     *
296
-     * @return string
297
-     */
298
-    public function resolve($uri, $baseUri = null)
299
-    {
300
-        $components = $this->parse($uri);
301
-        $path = $components['path'];
302
-
303
-        if ((array_key_exists('scheme', $components)) && ('http' === $components['scheme'])) {
304
-            return $uri;
305
-        }
306
-
307
-        $baseComponents = $this->parse($baseUri);
308
-        $basePath = $baseComponents['path'];
309
-
310
-        $baseComponents['path'] = UriResolver::combineRelativePathWithBasePath($path, $basePath);
311
-
312
-        return $this->generate($baseComponents);
313
-    }
314
-
315
-    /**
316
-     * @param string $uri
317
-     *
318
-     * @return bool
319
-     */
320
-    public function isValid($uri)
321
-    {
322
-        $components = $this->parse($uri);
323
-
324
-        return !empty($components);
325
-    }
326
-
327
-    /**
328
-     * Set a URL translation rule
329
-     */
330
-    public function setTranslation($from, $to)
331
-    {
332
-        $this->translationMap[$from] = $to;
333
-    }
334
-
335
-    /**
336
-     * Apply URI translation rules
337
-     */
338
-    public function translate($uri)
339
-    {
340
-        foreach ($this->translationMap as $from => $to) {
341
-            $uri = preg_replace($from, $to, $uri);
342
-        }
343
-
344
-        // translate references to local files within the json-schema package
345
-        $uri = preg_replace('|^package://|', sprintf('file://%s/', realpath(__DIR__ . '/../../..')), $uri);
346
-
347
-        return $uri;
348
-    }
27
+	/**
28
+	 * @var array Map of URL translations
29
+	 */
30
+	protected $translationMap = array(
31
+		// use local copies of the spec schemas
32
+		'|^https?://json-schema.org/draft-(0[34])/schema#?|' => 'package://dist/schema/json-schema-draft-$1.json'
33
+	);
34
+
35
+	/**
36
+	 * @var array A list of endpoints for media type check exclusion
37
+	 */
38
+	protected $allowedInvalidContentTypeEndpoints = array(
39
+		'http://json-schema.org/',
40
+		'https://json-schema.org/'
41
+	);
42
+
43
+	/**
44
+	 * @var null|UriRetrieverInterface
45
+	 */
46
+	protected $uriRetriever = null;
47
+
48
+	/**
49
+	 * @var array|object[]
50
+	 *
51
+	 * @see loadSchema
52
+	 */
53
+	private $schemaCache = array();
54
+
55
+	/**
56
+	 * Adds an endpoint to the media type validation exclusion list
57
+	 *
58
+	 * @param string $endpoint
59
+	 */
60
+	public function addInvalidContentTypeEndpoint($endpoint)
61
+	{
62
+		$this->allowedInvalidContentTypeEndpoints[] = $endpoint;
63
+	}
64
+
65
+	/**
66
+	 * Guarantee the correct media type was encountered
67
+	 *
68
+	 * @param UriRetrieverInterface $uriRetriever
69
+	 * @param string                $uri
70
+	 *
71
+	 * @return bool|void
72
+	 */
73
+	public function confirmMediaType($uriRetriever, $uri)
74
+	{
75
+		$contentType = $uriRetriever->getContentType();
76
+
77
+		if (is_null($contentType)) {
78
+			// Well, we didn't get an invalid one
79
+			return;
80
+		}
81
+
82
+		if (in_array($contentType, array(Validator::SCHEMA_MEDIA_TYPE, 'application/json'))) {
83
+			return;
84
+		}
85
+
86
+		foreach ($this->allowedInvalidContentTypeEndpoints as $endpoint) {
87
+			if (strpos($uri, $endpoint) === 0) {
88
+				return true;
89
+			}
90
+		}
91
+
92
+		throw new InvalidSchemaMediaTypeException(sprintf('Media type %s expected', Validator::SCHEMA_MEDIA_TYPE));
93
+	}
94
+
95
+	/**
96
+	 * Get a URI Retriever
97
+	 *
98
+	 * If none is specified, sets a default FileGetContents retriever and
99
+	 * returns that object.
100
+	 *
101
+	 * @return UriRetrieverInterface
102
+	 */
103
+	public function getUriRetriever()
104
+	{
105
+		if (is_null($this->uriRetriever)) {
106
+			$this->setUriRetriever(new FileGetContents());
107
+		}
108
+
109
+		return $this->uriRetriever;
110
+	}
111
+
112
+	/**
113
+	 * Resolve a schema based on pointer
114
+	 *
115
+	 * URIs can have a fragment at the end in the format of
116
+	 * #/path/to/object and we are to look up the 'path' property of
117
+	 * the first object then the 'to' and 'object' properties.
118
+	 *
119
+	 * @param object $jsonSchema JSON Schema contents
120
+	 * @param string $uri        JSON Schema URI
121
+	 *
122
+	 * @throws ResourceNotFoundException
123
+	 *
124
+	 * @return object JSON Schema after walking down the fragment pieces
125
+	 */
126
+	public function resolvePointer($jsonSchema, $uri)
127
+	{
128
+		$resolver = new UriResolver();
129
+		$parsed = $resolver->parse($uri);
130
+		if (empty($parsed['fragment'])) {
131
+			return $jsonSchema;
132
+		}
133
+
134
+		$path = explode('/', $parsed['fragment']);
135
+		while ($path) {
136
+			$pathElement = array_shift($path);
137
+			if (!empty($pathElement)) {
138
+				$pathElement = str_replace('~1', '/', $pathElement);
139
+				$pathElement = str_replace('~0', '~', $pathElement);
140
+				if (!empty($jsonSchema->$pathElement)) {
141
+					$jsonSchema = $jsonSchema->$pathElement;
142
+				} else {
143
+					throw new ResourceNotFoundException(
144
+						'Fragment "' . $parsed['fragment'] . '" not found'
145
+						. ' in ' . $uri
146
+					);
147
+				}
148
+
149
+				if (!is_object($jsonSchema)) {
150
+					throw new ResourceNotFoundException(
151
+						'Fragment part "' . $pathElement . '" is no object '
152
+						. ' in ' . $uri
153
+					);
154
+				}
155
+			}
156
+		}
157
+
158
+		return $jsonSchema;
159
+	}
160
+
161
+	/**
162
+	 * {@inheritdoc}
163
+	 */
164
+	public function retrieve($uri, $baseUri = null, $translate = true)
165
+	{
166
+		$resolver = new UriResolver();
167
+		$resolvedUri = $fetchUri = $resolver->resolve($uri, $baseUri);
168
+
169
+		//fetch URL without #fragment
170
+		$arParts = $resolver->parse($resolvedUri);
171
+		if (isset($arParts['fragment'])) {
172
+			unset($arParts['fragment']);
173
+			$fetchUri = $resolver->generate($arParts);
174
+		}
175
+
176
+		// apply URI translations
177
+		if ($translate) {
178
+			$fetchUri = $this->translate($fetchUri);
179
+		}
180
+
181
+		$jsonSchema = $this->loadSchema($fetchUri);
182
+
183
+		// Use the JSON pointer if specified
184
+		$jsonSchema = $this->resolvePointer($jsonSchema, $resolvedUri);
185
+
186
+		if ($jsonSchema instanceof \stdClass) {
187
+			$jsonSchema->id = $resolvedUri;
188
+		}
189
+
190
+		return $jsonSchema;
191
+	}
192
+
193
+	/**
194
+	 * Fetch a schema from the given URI, json-decode it and return it.
195
+	 * Caches schema objects.
196
+	 *
197
+	 * @param string $fetchUri Absolute URI
198
+	 *
199
+	 * @return object JSON schema object
200
+	 */
201
+	protected function loadSchema($fetchUri)
202
+	{
203
+		if (isset($this->schemaCache[$fetchUri])) {
204
+			return $this->schemaCache[$fetchUri];
205
+		}
206
+
207
+		$uriRetriever = $this->getUriRetriever();
208
+		$contents = $this->uriRetriever->retrieve($fetchUri);
209
+		$this->confirmMediaType($uriRetriever, $fetchUri);
210
+		$jsonSchema = json_decode($contents);
211
+
212
+		if (JSON_ERROR_NONE < $error = json_last_error()) {
213
+			throw new JsonDecodingException($error);
214
+		}
215
+
216
+		$this->schemaCache[$fetchUri] = $jsonSchema;
217
+
218
+		return $jsonSchema;
219
+	}
220
+
221
+	/**
222
+	 * Set the URI Retriever
223
+	 *
224
+	 * @param UriRetrieverInterface $uriRetriever
225
+	 *
226
+	 * @return $this for chaining
227
+	 */
228
+	public function setUriRetriever(UriRetrieverInterface $uriRetriever)
229
+	{
230
+		$this->uriRetriever = $uriRetriever;
231
+
232
+		return $this;
233
+	}
234
+
235
+	/**
236
+	 * Parses a URI into five main components
237
+	 *
238
+	 * @param string $uri
239
+	 *
240
+	 * @return array
241
+	 */
242
+	public function parse($uri)
243
+	{
244
+		preg_match('|^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?|', $uri, $match);
245
+
246
+		$components = array();
247
+		if (5 < count($match)) {
248
+			$components =  array(
249
+				'scheme'    => $match[2],
250
+				'authority' => $match[4],
251
+				'path'      => $match[5]
252
+			);
253
+		}
254
+
255
+		if (7 < count($match)) {
256
+			$components['query'] = $match[7];
257
+		}
258
+
259
+		if (9 < count($match)) {
260
+			$components['fragment'] = $match[9];
261
+		}
262
+
263
+		return $components;
264
+	}
265
+
266
+	/**
267
+	 * Builds a URI based on n array with the main components
268
+	 *
269
+	 * @param array $components
270
+	 *
271
+	 * @return string
272
+	 */
273
+	public function generate(array $components)
274
+	{
275
+		$uri = $components['scheme'] . '://'
276
+			 . $components['authority']
277
+			 . $components['path'];
278
+
279
+		if (array_key_exists('query', $components)) {
280
+			$uri .= $components['query'];
281
+		}
282
+
283
+		if (array_key_exists('fragment', $components)) {
284
+			$uri .= $components['fragment'];
285
+		}
286
+
287
+		return $uri;
288
+	}
289
+
290
+	/**
291
+	 * Resolves a URI
292
+	 *
293
+	 * @param string $uri     Absolute or relative
294
+	 * @param string $baseUri Optional base URI
295
+	 *
296
+	 * @return string
297
+	 */
298
+	public function resolve($uri, $baseUri = null)
299
+	{
300
+		$components = $this->parse($uri);
301
+		$path = $components['path'];
302
+
303
+		if ((array_key_exists('scheme', $components)) && ('http' === $components['scheme'])) {
304
+			return $uri;
305
+		}
306
+
307
+		$baseComponents = $this->parse($baseUri);
308
+		$basePath = $baseComponents['path'];
309
+
310
+		$baseComponents['path'] = UriResolver::combineRelativePathWithBasePath($path, $basePath);
311
+
312
+		return $this->generate($baseComponents);
313
+	}
314
+
315
+	/**
316
+	 * @param string $uri
317
+	 *
318
+	 * @return bool
319
+	 */
320
+	public function isValid($uri)
321
+	{
322
+		$components = $this->parse($uri);
323
+
324
+		return !empty($components);
325
+	}
326
+
327
+	/**
328
+	 * Set a URL translation rule
329
+	 */
330
+	public function setTranslation($from, $to)
331
+	{
332
+		$this->translationMap[$from] = $to;
333
+	}
334
+
335
+	/**
336
+	 * Apply URI translation rules
337
+	 */
338
+	public function translate($uri)
339
+	{
340
+		foreach ($this->translationMap as $from => $to) {
341
+			$uri = preg_replace($from, $to, $uri);
342
+		}
343
+
344
+		// translate references to local files within the json-schema package
345
+		$uri = preg_replace('|^package://|', sprintf('file://%s/', realpath(__DIR__ . '/../../..')), $uri);
346
+
347
+		return $uri;
348
+	}
349 349
 }
Please login to merge, or discard this patch.