Skip to content

Commit eaa0083

Browse files
author
Alexander Miertsch
authored
Merge pull request #12 from event-engine/feature/from-json-schema-file
Add fromJsonSchemaFile() and fromJsonSchema() method
2 parents f702fbe + 4f7dd40 commit eaa0083

File tree

8 files changed

+141
-7
lines changed

8 files changed

+141
-7
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.idea
22
composer.lock
3-
vendor
3+
vendor
4+
.php_cs.cache
5+
.phpunit.result.cache

.php_cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
$config = new Prooph\CS\Config\Prooph();
4+
$config->getFinder()->in(__DIR__);
5+
6+
$cacheDir = getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__;
7+
8+
$config->setCacheFile($cacheDir . '/.php_cs.cache');
9+
10+
return $config;

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717
],
1818
"require": {
1919
"php": "^7.4 || ^8.0",
20+
"ext-json": "*",
2021
"event-engine/php-data": "^2.0",
2122
"event-engine/php-engine-utils": "^0.2",
2223
"event-engine/php-schema": "^0.2",
2324
"ramsey/uuid": "^3.6 || ^4.0"
2425
},
2526
"require-dev": {
26-
"ext-json": "*",
2727
"justinrainbow/json-schema": "^5.2",
2828
"malukenho/docheader": "^0.1.4",
2929
"opis/json-schema": "^1.0",
3030
"phpunit/phpunit": "^8.0 || ^9.0",
31-
"prooph/php-cs-fixer-config": "^0.4",
31+
"prooph/php-cs-fixer-config": "^v0.4.0",
3232
"roave/security-advisories": "dev-master",
3333
"php-coveralls/php-coveralls": "^2.0"
3434
},
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/**
4+
* This file is part of event-engine/php-json-schema.
5+
* (c) 2018-2021 prooph software GmbH <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace EventEngine\JsonSchema\Exception;
14+
15+
final class CouldNotReadFileException extends RuntimeException
16+
{
17+
public static function forFile(string $file): self
18+
{
19+
return new self(
20+
\sprintf('Could not read "%s" file.', $file)
21+
);
22+
}
23+
}

src/Exception/RuntimeException.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* This file is part of event-engine/php-json-schema.
5+
* (c) 2018-2021 prooph software GmbH <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace EventEngine\JsonSchema\Exception;
14+
15+
use RuntimeException as PhpRuntimeException;
16+
17+
class RuntimeException extends PhpRuntimeException implements JsonSchemaException
18+
{
19+
}

src/JsonSchemaArray.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This file is part of event-engine/php-json-schema.
45
* (c) 2018-2021 prooph software GmbH <[email protected]>
@@ -11,16 +12,31 @@
1112

1213
namespace EventEngine\JsonSchema;
1314

15+
use EventEngine\JsonSchema\Exception\CouldNotReadFileException;
1416
use EventEngine\Schema\InputTypeSchema;
1517
use EventEngine\Schema\PayloadSchema;
1618
use EventEngine\Schema\ResponseTypeSchema;
19+
use const JSON_THROW_ON_ERROR;
1720

1821
final class JsonSchemaArray implements PayloadSchema, ResponseTypeSchema, InputTypeSchema
1922
{
20-
/**
21-
* @var array
22-
*/
23-
private $schema;
23+
private array $schema;
24+
25+
public static function fromFile(string $file, int $flags = JSON_THROW_ON_ERROR, int $depth = 512): self
26+
{
27+
if (! \file_exists($file) || ! \is_readable($file)) {
28+
throw CouldNotReadFileException::forFile($file);
29+
}
30+
31+
return self::fromString(\file_get_contents($file), $depth, $flags);
32+
}
33+
34+
public static function fromString(string $jsonSchema, int $flags = JSON_THROW_ON_ERROR, int $depth = 512): self
35+
{
36+
return new self(
37+
\json_decode($jsonSchema, true, $depth, $flags)
38+
);
39+
}
2440

2541
public function __construct(array $schema)
2642
{

tests/JsonSchemaArrayTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/**
4+
* This file is part of event-engine/php-json-schema.
5+
* (c) 2018-2021 prooph software GmbH <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace EventEngineTest\JsonSchema;
14+
15+
use EventEngine\JsonSchema\Exception\CouldNotReadFileException;
16+
use EventEngine\JsonSchema\JsonSchemaArray;
17+
18+
final class JsonSchemaArrayTest extends BasicTestCase
19+
{
20+
private const FILE = __DIR__ . DIRECTORY_SEPARATOR . '_files/schema.json';
21+
22+
/**
23+
* @test
24+
*/
25+
public function it_creates_json_schema_from_file(): void
26+
{
27+
$cut = JsonSchemaArray::fromFile(self::FILE);
28+
$this->assertArrayHasKey('properties', $cut->toArray());
29+
}
30+
31+
/**
32+
* @test
33+
*/
34+
public function it_creates_json_schema_from_string(): void
35+
{
36+
$cut = JsonSchemaArray::fromString(\file_get_contents(self::FILE));
37+
$this->assertArrayHasKey('properties', $cut->toArray());
38+
}
39+
40+
/**
41+
* @test
42+
*/
43+
public function it_throws_exception_if_file_is_not_found(): void
44+
{
45+
$this->expectException(CouldNotReadFileException::class);
46+
JsonSchemaArray::fromFile('unknown.json');
47+
}
48+
}

tests/_files/schema.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"id": {
5+
"type": "string"
6+
},
7+
"name": {
8+
"type": "string"
9+
}
10+
},
11+
"required": [
12+
"id",
13+
"name"
14+
],
15+
"additionalProperties": false
16+
}

0 commit comments

Comments
 (0)