Skip to content

Commit 8490235

Browse files
committed
Allow shorthand string - Close #10
1 parent 0e21375 commit 8490235

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

src/Shorthand/Shorthand.php

+24-13
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,33 @@
1515

1616
final class Shorthand
1717
{
18+
private const STRING_NAME = '__this_is_my_temporary_property__';
19+
1820
/**
19-
* @param array<string, mixed> $shorthand
20-
* @param string|null $namespace
21+
* @param string|array<string, mixed> $shorthand
22+
* @param array<string, mixed> $customData
2123
* @return array<string, mixed>
2224
*/
23-
public static function convertToJsonSchema(array $shorthand, ?string $namespace = null): array
25+
public static function convertToJsonSchema($shorthand, array $customData = []): array
2426
{
25-
$schema = [
26-
'type' => 'object',
27-
'properties' => [
28-
29-
],
30-
'required' => [],
31-
'additionalProperties' => false,
32-
];
27+
$isString = \is_string($shorthand);
3328

34-
if ($namespace !== null) {
35-
$schema['namespace'] = $namespace;
29+
if ($isString === true) {
30+
$shorthand = [self::STRING_NAME => $shorthand];
3631
}
3732

33+
$schema = \array_merge(
34+
$customData,
35+
[
36+
'type' => 'object',
37+
'properties' => [
38+
39+
],
40+
'required' => [],
41+
'additionalProperties' => false,
42+
]
43+
);
44+
3845
foreach ($shorthand as $property => $shorthandDefinition) {
3946
if (! \is_string($property) || empty($property)) {
4047
throw InvalidShorthand::emptyString($shorthand);
@@ -90,6 +97,10 @@ public static function convertToJsonSchema(array $shorthand, ?string $namespace
9097
}
9198
}
9299

100+
if ($isString === true) {
101+
return \array_merge($customData, $schema['properties'][self::STRING_NAME]);
102+
}
103+
93104
return $schema;
94105
}
95106

src/Type/ReferenceType.php

+11
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ public function setRef(string $ref): self
8484
return $this;
8585
}
8686

87+
public function extractNameFromReference(): ?string
88+
{
89+
if ($this->ref === null) {
90+
return null;
91+
}
92+
93+
$referencePath = \explode('/', $this->ref);
94+
95+
return \array_pop($referencePath);
96+
}
97+
8798
public static function type(): string
8899
{
89100
return self::TYPE_REF;

tests/Shorthand/ShorthandTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ final class ShorthandTest extends TestCase
1717
{
1818
private const SHORTHAND_TYPES = ['string', 'integer', 'number', 'boolean'];
1919

20+
/**
21+
* @test
22+
*/
23+
public function it_converts_shorthand_from_string(): void
24+
{
25+
$schema = Shorthand::convertToJsonSchema('string|format:uuid|namespace:MyService');
26+
27+
$this->assertEquals(['type' => 'string', 'format' => 'uuid', 'namespace' => 'MyService'], $schema);
28+
}
29+
2030
/**
2131
* @test
2232
*/

0 commit comments

Comments
 (0)