Skip to content

Add fromJsonSchemaFile() and fromJsonSchema() method #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2021
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea
composer.lock
vendor
vendor
.php_cs.cache
.phpunit.result.cache
10 changes: 10 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

$config = new Prooph\CS\Config\Prooph();
$config->getFinder()->in(__DIR__);

$cacheDir = getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__;

$config->setCacheFile($cacheDir . '/.php_cs.cache');

return $config;
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
],
"require": {
"php": "^7.4 || ^8.0",
"ext-json": "*",
"event-engine/php-data": "^2.0",
"event-engine/php-engine-utils": "^0.2",
"event-engine/php-schema": "^0.2",
"ramsey/uuid": "^3.6 || ^4.0"
},
"require-dev": {
"ext-json": "*",
"justinrainbow/json-schema": "^5.2",
"malukenho/docheader": "^0.1.4",
"opis/json-schema": "^1.0",
"phpunit/phpunit": "^8.0 || ^9.0",
"prooph/php-cs-fixer-config": "^0.4",
"prooph/php-cs-fixer-config": "^v0.4.0",
"roave/security-advisories": "dev-master",
"php-coveralls/php-coveralls": "^2.0"
},
Expand Down
23 changes: 23 additions & 0 deletions src/Exception/CouldNotReadFileException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* This file is part of event-engine/php-json-schema.
* (c) 2018-2021 prooph software GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace EventEngine\JsonSchema\Exception;

final class CouldNotReadFileException extends RuntimeException
{
public static function forFile(string $file): self
{
return new self(
\sprintf('Could not read "%s" file.', $file)
);
}
}
19 changes: 19 additions & 0 deletions src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* This file is part of event-engine/php-json-schema.
* (c) 2018-2021 prooph software GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace EventEngine\JsonSchema\Exception;

use RuntimeException as PhpRuntimeException;

class RuntimeException extends PhpRuntimeException implements JsonSchemaException
{
}
24 changes: 20 additions & 4 deletions src/JsonSchemaArray.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of event-engine/php-json-schema.
* (c) 2018-2021 prooph software GmbH <[email protected]>
Expand All @@ -11,16 +12,31 @@

namespace EventEngine\JsonSchema;

use EventEngine\JsonSchema\Exception\CouldNotReadFileException;
use EventEngine\Schema\InputTypeSchema;
use EventEngine\Schema\PayloadSchema;
use EventEngine\Schema\ResponseTypeSchema;
use const JSON_THROW_ON_ERROR;

final class JsonSchemaArray implements PayloadSchema, ResponseTypeSchema, InputTypeSchema
{
/**
* @var array
*/
private $schema;
private array $schema;

public static function fromFile(string $file, int $flags = JSON_THROW_ON_ERROR, int $depth = 512): self
{
if (! \file_exists($file) || ! \is_readable($file)) {
throw CouldNotReadFileException::forFile($file);
}

return self::fromString(\file_get_contents($file), $depth, $flags);
}

public static function fromString(string $jsonSchema, int $flags = JSON_THROW_ON_ERROR, int $depth = 512): self
{
return new self(
\json_decode($jsonSchema, true, $depth, $flags)
);
}

public function __construct(array $schema)
{
Expand Down
48 changes: 48 additions & 0 deletions tests/JsonSchemaArrayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* This file is part of event-engine/php-json-schema.
* (c) 2018-2021 prooph software GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace EventEngineTest\JsonSchema;

use EventEngine\JsonSchema\Exception\CouldNotReadFileException;
use EventEngine\JsonSchema\JsonSchemaArray;

final class JsonSchemaArrayTest extends BasicTestCase
{
private const FILE = __DIR__ . DIRECTORY_SEPARATOR . '_files/schema.json';

/**
* @test
*/
public function it_creates_json_schema_from_file(): void
{
$cut = JsonSchemaArray::fromFile(self::FILE);
$this->assertArrayHasKey('properties', $cut->toArray());
}

/**
* @test
*/
public function it_creates_json_schema_from_string(): void
{
$cut = JsonSchemaArray::fromString(\file_get_contents(self::FILE));
$this->assertArrayHasKey('properties', $cut->toArray());
}

/**
* @test
*/
public function it_throws_exception_if_file_is_not_found(): void
{
$this->expectException(CouldNotReadFileException::class);
JsonSchemaArray::fromFile('unknown.json');
}
}
16 changes: 16 additions & 0 deletions tests/_files/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"id",
"name"
],
"additionalProperties": false
}