1 | <?php |
||||
2 | |||||
3 | declare(strict_types=1); |
||||
4 | |||||
5 | namespace Spiechu\SymfonyCommonsBundle\Service\SchemaValidator; |
||||
6 | |||||
7 | use JsonSchema\SchemaStorage; |
||||
8 | use JsonSchema\Uri\UriRetriever; |
||||
9 | use JsonSchema\Validator; |
||||
10 | use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException; |
||||
11 | |||||
12 | class JsonSchemaValidatorFactory extends AbstractSchemaValidatorFactory implements SchemaValidatorFactoryInterface |
||||
13 | { |
||||
14 | /** |
||||
15 | * {@inheritdoc} |
||||
16 | */ |
||||
17 | 6 | public function getValidator(string $id): SchemaValidatorInterface |
|||
18 | { |
||||
19 | 6 | $this->assertHasSchema($id); |
|||
20 | |||||
21 | 5 | if (!$this->registeredSchemas[$id] instanceof JsonSchemaValidator) { |
|||
22 | 5 | $this->registeredSchemas[$id] = new JsonSchemaValidator($this->createSchema($id), new Validator()); |
|||
23 | } |
||||
24 | |||||
25 | 5 | return $this->registeredSchemas[$id]; |
|||
26 | } |
||||
27 | |||||
28 | /** |
||||
29 | * @param string $id |
||||
30 | * |
||||
31 | * @throws \InvalidArgumentException |
||||
32 | * @throws FileLocatorFileNotFoundException |
||||
33 | * |
||||
34 | * @return \stdClass |
||||
35 | */ |
||||
36 | 5 | protected function createSchema(string $id): \stdClass |
|||
37 | { |
||||
38 | 5 | $mainSchemaFilePath = $this->fileLocator->locate($this->registeredSchemas[$id]); |
|||
39 | 5 | $schemaUri = basename($mainSchemaFilePath); |
|||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
40 | 5 | $schemaBaseUri = sprintf('file://%s/%s', \dirname($mainSchemaFilePath), $schemaUri); |
|||
0 ignored issues
–
show
It seems like
$mainSchemaFilePath can also be of type array ; however, parameter $path of dirname() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
41 | |||||
42 | 5 | $retriever = new UriRetriever(); |
|||
43 | 5 | $schema = $retriever->retrieve($schemaUri, $schemaBaseUri); |
|||
44 | |||||
45 | 5 | $schemaStorage = new SchemaStorage($retriever); |
|||
46 | 5 | $schemaStorage->addSchema($schemaBaseUri, $schema); |
|||
47 | |||||
48 | 5 | if (!$schema instanceof \stdClass) { |
|||
49 | throw new \InvalidArgumentException('Retriever result is not instance of \stdClass'); |
||||
50 | } |
||||
51 | |||||
52 | 5 | return $schema; |
|||
53 | } |
||||
54 | } |
||||
55 |