-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dominic Tubach
committed
Mar 21, 2024
1 parent
45653ea
commit 98cb47b
Showing
12 changed files
with
603 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
/* | ||
* Copyright 2024 SYSTOPIA GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Systopia\JsonSchema\Exceptions; | ||
|
||
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
/* | ||
* Copyright 2024 SYSTOPIA GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Systopia\JsonSchema\KeywordValidators; | ||
|
||
use Opis\JsonSchema\Errors\ValidationError; | ||
use Opis\JsonSchema\JsonPointer; | ||
use Opis\JsonSchema\KeywordValidators\AbstractKeywordValidator; | ||
use Opis\JsonSchema\ValidationContext; | ||
use Systopia\JsonSchema\Tags\TaggedDataContainerUtil; | ||
|
||
final class TagKeywordValidator extends AbstractKeywordValidator | ||
{ | ||
/** | ||
* @var array<string, null|mixed> | ||
*/ | ||
private array $tags; | ||
|
||
/** | ||
* @param array<string, null|mixed> $tags mapping of tag to extra data | ||
*/ | ||
public function __construct(array $tags) | ||
{ | ||
$this->tags = $tags; | ||
} | ||
|
||
public function validate(ValidationContext $context): ?ValidationError | ||
{ | ||
// Run other validators first that might change the data. | ||
$error = null === $this->next ? null : $this->next->validate($context); | ||
|
||
$data = $context->currentData(); | ||
$dataPointer = JsonPointer::pathToString($context->currentDataPath()); | ||
foreach ($this->tags as $tag => $extra) { | ||
TaggedDataContainerUtil::getTaggedDataContainer($context)->add($tag, $dataPointer, $data, $extra); | ||
} | ||
|
||
return $error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/Parsers/KeywordValidators/TagKeywordValidatorParser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
/* | ||
* Copyright 2024 SYSTOPIA GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Systopia\JsonSchema\Parsers\KeywordValidators; | ||
|
||
use Opis\JsonSchema\Info\SchemaInfo; | ||
use Opis\JsonSchema\KeywordValidator; | ||
use Opis\JsonSchema\Parsers\KeywordValidatorParser; | ||
use Opis\JsonSchema\Parsers\SchemaParser; | ||
use Systopia\JsonSchema\KeywordValidators\TagKeywordValidator; | ||
|
||
final class TagKeywordValidatorParser extends KeywordValidatorParser | ||
{ | ||
public function __construct(string $keyword = '$tag') | ||
{ | ||
parent::__construct($keyword); | ||
} | ||
|
||
public function parse(SchemaInfo $info, SchemaParser $parser, object $shared): ?KeywordValidator | ||
{ | ||
if (!$this->keywordExists($info)) { | ||
return null; | ||
} | ||
|
||
$tags = (array) $this->keywordValue($info); | ||
$parsedTags = []; | ||
foreach ($tags as $key => $value) { | ||
if (\is_string($key)) { | ||
$parsedTags[$key] = $value; | ||
} elseif (\is_string($value)) { | ||
$parsedTags[$value] = null; | ||
} else { | ||
throw $this->keywordException('Invalid value for keyword {keyword}', $info); | ||
} | ||
} | ||
|
||
return [] === $parsedTags ? null : new TagKeywordValidator($parsedTags); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
/* | ||
* Copyright 2022 SYSTOPIA GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Systopia\JsonSchema\Tags; | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
final class DummyTaggedDataContainer implements TaggedDataContainerInterface | ||
{ | ||
private static self $instance; | ||
|
||
public static function getInstance(): self | ||
{ | ||
return self::$instance ??= new self(); | ||
} | ||
|
||
public function add(string $tag, string $dataPointer, $data, $extra): void {} | ||
|
||
public function get(string $tag, string $dataPointer) | ||
{ | ||
return null; | ||
} | ||
|
||
public function has(string $tag, string $dataPointer): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function getAll(): array | ||
{ | ||
return []; | ||
} | ||
|
||
public function getByTag(string $tag): array | ||
{ | ||
return []; | ||
} | ||
|
||
public function hasTag(string $tag): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function getExtra(string $tag, string $dataPointer) | ||
{ | ||
return null; | ||
} | ||
|
||
public function hasExtra(string $tag, string $dataPointer): bool | ||
{ | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
/* | ||
* Copyright 2022 SYSTOPIA GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Systopia\JsonSchema\Tags; | ||
|
||
use Systopia\JsonSchema\Exceptions\InvalidArgumentException; | ||
|
||
final class TaggedDataContainer implements TaggedDataContainerInterface | ||
{ | ||
/** | ||
* @var array<string, array<string, mixed>> | ||
*/ | ||
private array $data = []; | ||
|
||
/** | ||
* @var array<string, array<string, mixed>> | ||
*/ | ||
private array $extra = []; | ||
|
||
public function add(string $tag, string $dataPointer, $data, $extra): void | ||
{ | ||
if ($this->has($tag, $dataPointer)) { | ||
throw new InvalidArgumentException(sprintf('Data for tag "%s" at "%s" already exists', $tag, $dataPointer)); | ||
} | ||
|
||
$this->data[$tag][$dataPointer] = $data; | ||
$this->extra[$tag][$dataPointer] = $extra; | ||
} | ||
|
||
public function get(string $tag, string $dataPointer) | ||
{ | ||
return $this->data[$tag][$dataPointer] ?? null; | ||
} | ||
|
||
public function has(string $tag, string $dataPointer): bool | ||
{ | ||
return \array_key_exists($dataPointer, $this->data[$tag] ?? []); | ||
} | ||
|
||
public function getAll(): array | ||
{ | ||
return $this->data; | ||
} | ||
|
||
public function getByTag(string $tag): array | ||
{ | ||
return $this->data[$tag] ?? []; | ||
} | ||
|
||
public function hasTag(string $tag): bool | ||
{ | ||
return isset($this->data[$tag]); | ||
} | ||
|
||
public function getExtra(string $tag, string $dataPointer) | ||
{ | ||
return $this->extra[$tag][$dataPointer] ?? null; | ||
} | ||
|
||
public function hasExtra(string $tag, string $dataPointer): bool | ||
{ | ||
return isset($this->extra[$tag][$dataPointer]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
/* | ||
* Copyright 2022 SYSTOPIA GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Systopia\JsonSchema\Tags; | ||
|
||
interface TaggedDataContainerInterface | ||
{ | ||
/** | ||
* @param null|mixed $data | ||
* @param null|mixed $extra | ||
*/ | ||
public function add(string $tag, string $dataPointer, $data, $extra): void; | ||
|
||
/** | ||
* @return null|mixed | ||
*/ | ||
public function get(string $tag, string $dataPointer); | ||
|
||
/** | ||
* @return bool true if a value (including null) was added for the given tag and pointer | ||
*/ | ||
public function has(string $tag, string $dataPointer): bool; | ||
|
||
/** | ||
* @return array<string, array<string, null|mixed>> Mapping of tag to a mapping of JSON pointer to data | ||
*/ | ||
public function getAll(): array; | ||
|
||
/** | ||
* @return array<string, null|mixed> Mapping of JSON pointer to data | ||
*/ | ||
public function getByTag(string $tag): array; | ||
|
||
public function hasTag(string $tag): bool; | ||
|
||
/** | ||
* @return null|mixed | ||
*/ | ||
public function getExtra(string $tag, string $dataPointer); | ||
|
||
public function hasExtra(string $tag, string $dataPointer): bool; | ||
} |
Oops, something went wrong.