Skip to content

Commit

Permalink
Merge pull request #12 from event-engine/feature/from-json-schema-file
Browse files Browse the repository at this point in the history
Add fromJsonSchemaFile() and fromJsonSchema() method
  • Loading branch information
codeliner authored Aug 4, 2021
2 parents f702fbe + 4f7dd40 commit eaa0083
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 7 deletions.
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 <contact@prooph.de>
*
* 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 <contact@prooph.de>
*
* 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 <contact@prooph.de>
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 <contact@prooph.de>
*
* 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
}

0 comments on commit eaa0083

Please sign in to comment.