Skip to content

Commit bb2037b

Browse files
author
Paul Hendriks
committed
Add format to class property
1 parent db960c1 commit bb2037b

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

src/Attribute/Field.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
namespace Spiral\JsonSchemaGenerator\Attribute;
66

7+
use Spiral\JsonSchemaGenerator\Schema\Format;
8+
79
#[\Attribute(\Attribute::TARGET_PROPERTY)]
810
class Field
911
{
1012
public function __construct(
1113
public readonly string $title = '',
1214
public readonly string $description = '',
1315
public readonly mixed $default = null,
16+
public readonly ?Format $format = null,
1417
) {}
1518
}

src/Generator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,14 @@ protected function generateProperty(PropertyInterface $property): ?Property
111111
$title = '';
112112
$description = '';
113113
$default = null;
114+
$format = null;
114115

115116
$attribute = $property->findAttribute(Field::class);
116117
if ($attribute !== null) {
117118
$title = $attribute->title;
118119
$description = $attribute->description;
119120
$default = $attribute->default;
121+
$format = $attribute->format;
120122
}
121123

122124
if ($default === null && $property->hasDefaultValue()) {
@@ -135,14 +137,14 @@ protected function generateProperty(PropertyInterface $property): ?Property
135137

136138
$required = $default === null && !$type->allowsNull();
137139
if ($type->isBuiltin()) {
138-
return new Property($type->getName(), $options, $title, $description, $required, $default);
140+
return new Property($type->getName(), $options, $title, $description, $required, $default, $format);
139141
}
140142

141143
// Class or enum
142144
$class = $type->getName();
143145

144146
return \is_string($class) && \class_exists($class)
145-
? new Property($class, [], $title, $description, $required, $default)
147+
? new Property($class, [], $title, $description, $required, $default, $format)
146148
: null;
147149
}
148150
}

src/Schema/Format.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\JsonSchemaGenerator\Schema;
6+
7+
/**
8+
* Based on https://opis.io/json-schema/2.x/formats.html
9+
*/
10+
enum Format: string
11+
{
12+
case Email = 'email';
13+
case IdnEmail = 'idn-email';
14+
case Hostname = 'hostname';
15+
case IdnHostname = 'idn-hostname';
16+
case Ipv4 = 'ipv4';
17+
case Ipv6 = 'ipv6';
18+
case JsonPointer = 'json-pointer';
19+
case RelativeJsonPointer = 'relative-json-pointer';
20+
case Uri = 'uri';
21+
case UriReference = 'uri-reference';
22+
case UriTemplate = 'uri-template';
23+
case Iri = 'iri';
24+
case IriReference = 'iri-reference';
25+
case Uuid = 'uuid';
26+
}

src/Schema/Property.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function __construct(
2121
public readonly string $description = '',
2222
public readonly bool $required = false,
2323
public readonly mixed $default = null,
24+
public readonly ?Format $format = null,
2425
) {
2526
if (\is_string($this->type) && !\class_exists($this->type)) {
2627
throw new InvalidTypeException('Invalid type definition.');
@@ -44,6 +45,11 @@ public function jsonSerialize(): array
4445
$property['default'] = $this->default;
4546
}
4647

48+
if ($this->format !== null) {
49+
$property['format'] = $this->format instanceof Format ? $this->format->value : null;
50+
}
51+
52+
4753
if ($this->type === Type::Union) {
4854
$property['anyOf'] = $this->options->jsonSerialize();
4955
return $property;

0 commit comments

Comments
 (0)