2222 *
2323 * @author Johannes Wachter <johannes@sulu.io>
2424 */
25- final class NumberSchemaDefinition implements \JsonSerializable
25+ final class NumberSchemaDefinition extends AbstractSchemaDefinition
2626{
2727 /**
2828 * @param string $title Human-readable title for the field
@@ -33,16 +33,32 @@ final class NumberSchemaDefinition implements \JsonSerializable
3333 * @param int|float|null $maximum Optional maximum value (inclusive)
3434 */
3535 public function __construct (
36- public readonly string $ title ,
36+ string $ title ,
3737 public readonly bool $ integerOnly = false ,
38- public readonly ?string $ description = null ,
38+ ?string $ description = null ,
3939 public readonly int |float |null $ default = null ,
4040 public readonly int |float |null $ minimum = null ,
4141 public readonly int |float |null $ maximum = null ,
4242 ) {
43+ parent ::__construct ($ title , $ description );
44+
4345 if (null !== $ minimum && null !== $ maximum && $ minimum > $ maximum ) {
4446 throw new InvalidArgumentException ('minimum cannot be greater than maximum. ' );
4547 }
48+
49+ if (null !== $ default && null !== $ minimum && $ default < $ minimum ) {
50+ throw new InvalidArgumentException ('default value cannot be less than minimum. ' );
51+ }
52+
53+ if (null !== $ default && null !== $ maximum && $ default > $ maximum ) {
54+ throw new InvalidArgumentException ('default value cannot be greater than maximum. ' );
55+ }
56+
57+ if ($ integerOnly && null !== $ default && $ default !== (int ) $ default ) {
58+ throw new InvalidArgumentException (
59+ 'default value must be an integer when integerOnly is true. '
60+ );
61+ }
4662 }
4763
4864 /**
@@ -57,9 +73,7 @@ public function __construct(
5773 */
5874 public static function fromArray (array $ data ): self
5975 {
60- if (!isset ($ data ['title ' ]) || !\is_string ($ data ['title ' ])) {
61- throw new InvalidArgumentException ('Missing or invalid "title" for number schema definition. ' );
62- }
76+ self ::validateTitle ($ data , 'number ' );
6377
6478 $ type = $ data ['type ' ] ?? 'number ' ;
6579 $ integerOnly = 'integer ' === $ type ;
@@ -86,14 +100,7 @@ public static function fromArray(array $data): self
86100 */
87101 public function jsonSerialize (): array
88102 {
89- $ data = [
90- 'type ' => $ this ->integerOnly ? 'integer ' : 'number ' ,
91- 'title ' => $ this ->title ,
92- ];
93-
94- if (null !== $ this ->description ) {
95- $ data ['description ' ] = $ this ->description ;
96- }
103+ $ data = $ this ->buildBaseJson ($ this ->integerOnly ? 'integer ' : 'number ' );
97104
98105 if (null !== $ this ->default ) {
99106 $ data ['default ' ] = $ this ->default ;
0 commit comments