Skip to content

Commit 458d00a

Browse files
committed
Add root directory to data path
1 parent bf70a3a commit 458d00a

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

src/Validator.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
namespace Ixnode\PhpJsonSchemaValidator;
1515

16+
use Composer\Autoload\ClassLoader;
1617
use Exception;
1718
use Ixnode\PhpChecker\CheckerClass;
1819
use Ixnode\PhpContainer\File;
1920
use Ixnode\PhpContainer\Json;
21+
use Ixnode\PhpException\File\FileNotFoundException;
2022
use Ixnode\PhpException\Function\FunctionJsonEncodeException;
2123
use Ixnode\PhpException\Type\TypeInvalidException;
2224
use JsonException;
@@ -25,6 +27,7 @@
2527
use Opis\JsonSchema\Exceptions\InvalidKeywordException;
2628
use Opis\JsonSchema\Resolvers\SchemaResolver;
2729
use Opis\JsonSchema\Validator as OpisJsonSchemaValidator;
30+
use ReflectionClass;
2831
use stdClass;
2932

3033
/**
@@ -49,12 +52,35 @@ class Validator
4952
*
5053
* @param File|Json $data
5154
* @param File|Json $schema
52-
* @param string|null $pathRoot
55+
* @param string|null $directoryRoot
5356
*/
54-
public function __construct(protected File|Json $data, protected File|Json $schema, protected ?string $pathRoot = null)
57+
public function __construct(protected File|Json $data, protected File|Json $schema, protected ?string $directoryRoot = null)
5558
{
5659
}
5760

61+
/**
62+
* Returns the root directory of this project.
63+
*
64+
* @return string
65+
* @throws FileNotFoundException
66+
*/
67+
private function getDirectoryRoot(): string
68+
{
69+
if (!is_null($this->directoryRoot)) {
70+
return $this->directoryRoot;
71+
}
72+
73+
$reflection = new ReflectionClass(ClassLoader::class);
74+
75+
$fileName = $reflection->getFileName();
76+
77+
if ($fileName === false) {
78+
throw new FileNotFoundException('reflection-class');
79+
}
80+
81+
return dirname($fileName, 3);
82+
}
83+
5884
/**
5985
* Returns the data as JSON representation.
6086
*
@@ -99,6 +125,7 @@ protected function getJsonDecoded(string $json): stdClass
99125
* Validates the given JSON files.
100126
*
101127
* @return bool
128+
* @throws FileNotFoundException
102129
* @throws FunctionJsonEncodeException
103130
* @throws JsonException
104131
* @throws TypeInvalidException
@@ -122,7 +149,7 @@ public function validate(): bool
122149
$this->schema instanceof Json => $resolver->registerRaw($this->schema->getJsonStringFormatted(), Constants::ID_JSON_SCHEMA_GENERAL)
123150
};
124151

125-
$resolver->registerFile(Constants::URL_JSON_SCHEMA_DRAFT_07, (new File(Constants::PATH_SCHEMA_DRAFT_07, $this->pathRoot))->getPathReal());
152+
$resolver->registerFile(Constants::URL_JSON_SCHEMA_DRAFT_07, (new File(Constants::PATH_SCHEMA_DRAFT_07, $this->getDirectoryRoot()))->getPathReal());
126153

127154
$data = match (true) {
128155
$this->data instanceof File => $this->getJsonDecoded($this->data->getContentAsJson()->getJsonStringFormatted()),

src/ValidatorDebugger.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Ixnode\PhpContainer\File;
1717
use Ixnode\PhpContainer\Json;
18+
use Ixnode\PhpException\File\FileNotFoundException;
1819
use Ixnode\PhpException\Function\FunctionJsonEncodeException;
1920
use Ixnode\PhpException\Type\TypeInvalidException;
2021
use JsonException;
@@ -56,6 +57,7 @@ public function __construct(protected Validator $validator)
5657
* @throws JsonException
5758
* @throws FunctionJsonEncodeException
5859
* @throws TypeInvalidException
60+
* @throws FileNotFoundException
5961
*/
6062
public function validate(): bool
6163
{

tests/Unit/ValidatorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Ixnode\PhpContainer\File;
1717
use Ixnode\PhpContainer\Json;
18+
use Ixnode\PhpException\File\FileNotFoundException;
1819
use Ixnode\PhpException\Function\FunctionJsonEncodeException;
1920
use Ixnode\PhpException\Type\TypeInvalidException;
2021
use Ixnode\PhpJsonSchemaValidator\Constants;
@@ -47,6 +48,7 @@ final class ValidatorTest extends TestCase
4748
* @throws FunctionJsonEncodeException
4849
* @throws TypeInvalidException
4950
* @throws JsonException
51+
* @throws FileNotFoundException
5052
*/
5153
public function wrapperGet(int $number, Json|File $data, Json|File $schema, bool $expected, array $expectedError): void
5254
{

0 commit comments

Comments
 (0)