1313
1414namespace Ixnode \PhpJsonSchemaValidator ;
1515
16+ use Composer \Autoload \ClassLoader ;
1617use Exception ;
1718use Ixnode \PhpChecker \CheckerClass ;
1819use Ixnode \PhpContainer \File ;
1920use Ixnode \PhpContainer \Json ;
21+ use Ixnode \PhpException \File \FileNotFoundException ;
2022use Ixnode \PhpException \Function \FunctionJsonEncodeException ;
2123use Ixnode \PhpException \Type \TypeInvalidException ;
2224use JsonException ;
2527use Opis \JsonSchema \Exceptions \InvalidKeywordException ;
2628use Opis \JsonSchema \Resolvers \SchemaResolver ;
2729use Opis \JsonSchema \Validator as OpisJsonSchemaValidator ;
30+ use ReflectionClass ;
2831use 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 ()),
0 commit comments