Skip to content

Commit 77156cd

Browse files
committed
refactor: Introduce DraftIdentifers enum
1 parent fc994a7 commit 77156cd

18 files changed

+102
-44
lines changed

src/JsonSchema/Constraints/Factory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace JsonSchema\Constraints;
1313

14+
use JsonSchema\DraftIdentifiers;
1415
use JsonSchema\Exception\InvalidArgumentException;
1516
use JsonSchema\SchemaStorage;
1617
use JsonSchema\SchemaStorageInterface;
@@ -51,7 +52,7 @@ class Factory
5152
protected $errorContext = Validator::ERROR_DOCUMENT_VALIDATION;
5253

5354
/** @var string */
54-
private $defaultDialect = 'http://json-schema.org/draft-06/schema#';
55+
private $defaultDialect = DraftIdentifiers::DRAFT_6;
5556

5657
/**
5758
* @var array

src/JsonSchema/Constraints/SchemaConstraint.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace JsonSchema\Constraints;
1313

1414
use JsonSchema\ConstraintError;
15+
use JsonSchema\DraftIdentifiers;
1516
use JsonSchema\Entity\JsonPointer;
1617
use JsonSchema\Exception\InvalidArgumentException;
1718
use JsonSchema\Exception\InvalidSchemaException;
@@ -26,7 +27,7 @@
2627
*/
2728
class SchemaConstraint extends Constraint
2829
{
29-
private const DEFAULT_SCHEMA_SPEC = 'http://json-schema.org/draft-04/schema#';
30+
private const DEFAULT_SCHEMA_SPEC = DraftIdentifiers::DRAFT_4;
3031

3132
/**
3233
* {@inheritdoc}

src/JsonSchema/DraftIdentifiers.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace JsonSchema;
6+
7+
/**
8+
* @extends Enum<DraftIdentifiers::*>
9+
*
10+
* @method static DraftIdentifiers DRAFT_3()
11+
* @method static DraftIdentifiers DRAFT_4()
12+
* @method static DraftIdentifiers DRAFT_6()
13+
* @method static DraftIdentifiers DRAFT_7()
14+
* @method static DraftIdentifiers DRAFT_2019_09()
15+
* @method static DraftIdentifiers DRAFT_2020_12()
16+
*/
17+
class DraftIdentifiers extends Enum
18+
{
19+
public const DRAFT_3 = 'http://json-schema.org/draft-03/schema#';
20+
public const DRAFT_4 = 'http://json-schema.org/draft-04/schema#';
21+
public const DRAFT_6 = 'http://json-schema.org/draft-06/schema#';
22+
public const DRAFT_7 = 'http://json-schema.org/draft-07/schema#';
23+
public const DRAFT_2019_09 = 'https://json-schema.org/draft/2019-09/schema';
24+
public const DRAFT_2020_12 = 'https://json-schema.org/draft/2020-12/schema';
25+
26+
public function toConstraintName(): string
27+
{
28+
switch ($this->getValue()) {
29+
case self::DRAFT_3:
30+
return 'draft03';
31+
case self::DRAFT_4:
32+
return 'draft04';
33+
case self::DRAFT_6:
34+
return 'draft06';
35+
case self::DRAFT_7:
36+
return 'draft07';
37+
case self::DRAFT_2019_09:
38+
return 'draft2019-09';
39+
case self::DRAFT_2020_12:
40+
return 'draft2020-12';
41+
42+
}
43+
44+
throw new \Exception('Unsupported schema URI: ' . $this->getValue());
45+
}
46+
47+
public function withoutFragment(): string
48+
{
49+
return rtrim($this->getValue(), '#');
50+
}
51+
}

src/JsonSchema/SchemaStorage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ public function addSchema(string $id, $schema = null): void
6262
// workaround for bug in draft-03 & draft-04 meta-schemas (id & $ref defined with incorrect format)
6363
// see https://github.com/json-schema-org/JSON-Schema-Test-Suite/issues/177#issuecomment-293051367
6464
if (is_object($schema) && property_exists($schema, 'id')) {
65-
if ($schema->id === 'http://json-schema.org/draft-04/schema#') {
65+
if ($schema->id === DraftIdentifiers::DRAFT_4) {
6666
$schema->properties->id->format = 'uri-reference';
67-
} elseif ($schema->id === 'http://json-schema.org/draft-03/schema#') {
67+
} elseif ($schema->id === DraftIdentifiers::DRAFT_3) {
6868
$schema->properties->id->format = 'uri-reference';
6969
$schema->properties->{'$ref'}->format = 'uri-reference';
7070
}

src/JsonSchema/Validator.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function validate(&$value, $schema = null, ?int $checkMode = null): int
8585
}
8686

8787
$validator = $this->factory->createInstanceFor(
88-
$this->schemaUriToConstraintName($dialect)
88+
DraftIdentifiers::byValue($dialect)->toConstraintName()
8989
);
9090
}
9191

@@ -127,18 +127,4 @@ public function coerce(&$value, $schema): int
127127
{
128128
return $this->validate($value, $schema, Constraint::CHECK_MODE_COERCE_TYPES);
129129
}
130-
131-
private function schemaUriToConstraintName(string $schemaUri): string
132-
{
133-
switch ($schemaUri) {
134-
case 'http://json-schema.org/draft-03/schema#':
135-
return 'draft03';
136-
case 'http://json-schema.org/draft-04/schema#':
137-
return 'draft04';
138-
case 'http://json-schema.org/draft-06/schema#':
139-
return 'draft06';
140-
}
141-
142-
throw new \Exception('Unsupported schema URI: ' . $schemaUri);
143-
}
144130
}

tests/Constraints/BaseTestCase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
use Generator;
88
use JsonSchema\Constraints\Constraint;
99
use JsonSchema\Constraints\Factory;
10+
use JsonSchema\DraftIdentifiers;
1011
use JsonSchema\SchemaStorage;
1112
use JsonSchema\Uri\UriResolver;
1213
use JsonSchema\Validator;
1314

1415
abstract class BaseTestCase extends VeryBaseTestCase
1516
{
16-
protected $schemaSpec = 'http://json-schema.org/draft-04/schema#';
17+
protected $schemaSpec = DraftIdentifiers::DRAFT_4;
1718
protected $validateSchema = false;
1819

1920
/**

tests/Constraints/BasicTypesTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
namespace JsonSchema\Tests\Constraints;
66

7+
use JsonSchema\DraftIdentifiers;
8+
79
class BasicTypesTest extends BaseTestCase
810
{
911
/** @var string */
10-
protected $schemaSpec = 'http://json-schema.org/draft-03/schema#';
12+
protected $schemaSpec = DraftIdentifiers::DRAFT_3;
1113
/** @var bool */
1214
protected $validateSchema = true;
1315

tests/Constraints/ConstTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
namespace JsonSchema\Tests\Constraints;
66

7+
use JsonSchema\DraftIdentifiers;
8+
79
class ConstTest extends BaseTestCase
810
{
911
/** @var string */
10-
protected $schemaSpec = 'http://json-schema.org/draft-06/schema#';
12+
protected $schemaSpec = DraftIdentifiers::DRAFT_6;
1113
/** @var bool */
1214
protected $validateSchema = true;
1315

tests/Constraints/DependenciesTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
namespace JsonSchema\Tests\Constraints;
66

7+
use JsonSchema\DraftIdentifiers;
8+
79
class DependenciesTest extends BaseTestCase
810
{
911
/** @var string */
10-
protected $schemaSpec = 'http://json-schema.org/draft-03/schema#';
12+
protected $schemaSpec = DraftIdentifiers::DRAFT_3;
1113
/** @var bool */
1214
protected $validateSchema = true;
1315

tests/Constraints/DisallowTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace JsonSchema\Tests\Constraints;
66

7+
use JsonSchema\DraftIdentifiers;
8+
79
/**
810
* Schemas in these tests look like draft-03, but the 'disallow' patterns provided are in
911
* violation of the spec - 'disallow' as defined in draft-03 accepts the same values as the
@@ -14,7 +16,7 @@
1416
class DisallowTest extends BaseTestCase
1517
{
1618
/** @var string */
17-
protected $schemaSpec = 'http://json-schema.org/draft-03/schema#';
19+
protected $schemaSpec = DraftIdentifiers::DRAFT_3;
1820

1921
public function getInvalidTests(): \Generator
2022
{

0 commit comments

Comments
 (0)