Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/JsonSchema/Constraints/Undefined.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace JsonSchema\Constraints;

use JsonSchema\Validator;

/**
* The Undefined Constraints
*
Expand Down Expand Up @@ -89,6 +91,9 @@ protected function validateCommonProperties($value, $schema = null, $path = null
{
// if it extends another schema, it must pass that schema as well
if (isset($schema->extends)) {
if (is_string($schema->extends)) {
$schema->extends = $this->validateUri($schema->extends, $schema, $path, $i);
}
$this->checkUndefined($value, $schema->extends, $path, $i);
}

Expand All @@ -115,4 +120,14 @@ protected function validateCommonProperties($value, $schema = null, $path = null
}
}
}

protected function validateUri($schemaUri = null, $schema, $path = null, $i = null)
{
$resolver = new \JsonSchema\Uri\UriResolver();

if ($resolver->isValid($schemaUri)) {
$schemaId = property_exists($schema, 'id') ? $schema->id : null;
return Validator::retrieveUri($resolver->resolve($schemaUri, $schemaId));
}
}
}
17 changes: 17 additions & 0 deletions src/JsonSchema/Exception/InvalidSchemaMediaTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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\Exception;

/**
* Wrapper for the InvalidSchemaMediaType
*/
class InvalidSchemaMediaTypeException extends \RuntimeException
{
}
40 changes: 40 additions & 0 deletions src/JsonSchema/Exception/JsonDecodingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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\Exception;

/**
* Wrapper for the JsonDecodingException
*/
class JsonDecodingException extends \RuntimeException
{
public function __construct($code = JSON_ERROR_NONE, \Exception $previous = null)
{
switch ($code) {
case JSON_ERROR_DEPTH:
$message = 'The maximum stack depth has been exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$message = 'Invalid or malformed JSON';
break;
case JSON_ERROR_CTRL_CHAR:
$message = 'Control character error, possibly incorrectly encoded';
break;
case JSON_ERROR_UTF8:
$message = 'Malformed UTF-8 characters, possibly incorrectly encoded';
break;
case JSON_ERROR_SYNTAX:
$message = 'The maximum stack depth has been exceeded';
break;
default:
$message = 'Syntax error';
}
parent::__construct($message, $code, $previous);
}
}
17 changes: 17 additions & 0 deletions src/JsonSchema/Exception/ResourceNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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\Exception;

/**
* Wrapper for the ResourceNotFoundException
*/
class ResourceNotFoundException extends \RuntimeException
{
}
17 changes: 17 additions & 0 deletions src/JsonSchema/Exception/UriResolverException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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\Exception;

/**
* Wrapper for the UriResolverException
*/
class UriResolverException extends \RuntimeException
{
}
82 changes: 82 additions & 0 deletions src/JsonSchema/Uri/Retrievers/Curl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?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;

use JsonSchema\Exception\ResourceNotFoundException;

/**
* Tries to retrieve JSON schemas from a URI using cURL library
*
* @author Sander Coolen <sander@jibber.nl>
*/
class Curl implements UriRetrieverInterface
{
protected $contentType;
protected $messageBody;

public function __construct()
{
if (!function_exists('curl_init')) {
throw new \RuntimeException("cURL not installed");
}
}

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 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;
}

public function getContentType()
{
return $this->contentType;
}
}
73 changes: 73 additions & 0 deletions src/JsonSchema/Uri/Retrievers/FileGetContents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?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 file_get_contents()
*
* @author Sander Coolen <sander@jibber.nl>
*/
class FileGetContents implements UriRetrieverInterface
{
protected $contentType;
protected $messageBody;

public function retrieve($uri)
{
$context = stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => "Accept: " . Validator::SCHEMA_MEDIA_TYPE
)));

$response = file_get_contents($uri);
if (false === $response) {
throw new ResourceNotFoundException('JSON schema not found');
}

$this->messageBody = $response;
$this->fetchContentType($http_response_header);

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]);
}
}

public function getContentType()
{
return $this->contentType;
}
}
22 changes: 22 additions & 0 deletions src/JsonSchema/Uri/Retrievers/UriRetrieverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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
{
public function retrieve($uri);

public function getContentType();
}
Loading