Skip to content

Commit 81bc3ab

Browse files
authored
Merge pull request #8 from PaulHendriks/issue_7_format_property
Add format to class property
2 parents db960c1 + 27771b5 commit 81bc3ab

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 Date = 'date';
13+
case Time = 'time';
14+
case DateTime = 'date-time';
15+
case Duration = 'duration';
16+
case Regex = 'regex';
17+
case Email = 'email';
18+
case IdnEmail = 'idn-email';
19+
case Hostname = 'hostname';
20+
case IdnHostname = 'idn-hostname';
21+
case Ipv4 = 'ipv4';
22+
case Ipv6 = 'ipv6';
23+
case JsonPointer = 'json-pointer';
24+
case RelativeJsonPointer = 'relative-json-pointer';
25+
case Uri = 'uri';
26+
case UriReference = 'uri-reference';
27+
case UriTemplate = 'uri-template';
28+
case Iri = 'iri';
29+
case IriReference = 'iri-reference';
30+
case Uuid = 'uuid';
31+
}

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 instanceof Format) {
49+
$property['format'] = $this->format->value;
50+
}
51+
52+
4753
if ($this->type === Type::Union) {
4854
$property['anyOf'] = $this->options->jsonSerialize();
4955
return $property;

0 commit comments

Comments
 (0)