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,30 @@ 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 ('default value must be an integer when integerOnly is true. ' );
59+ }
4660 }
4761
4862 /**
@@ -57,9 +71,7 @@ public function __construct(
5771 */
5872 public static function fromArray (array $ data ): self
5973 {
60- if (!isset ($ data ['title ' ]) || !\is_string ($ data ['title ' ])) {
61- throw new InvalidArgumentException ('Missing or invalid "title" for number schema definition. ' );
62- }
74+ self ::validateTitle ($ data , 'number ' );
6375
6476 $ type = $ data ['type ' ] ?? 'number ' ;
6577 $ integerOnly = 'integer ' === $ type ;
@@ -86,14 +98,7 @@ public static function fromArray(array $data): self
8698 */
8799 public function jsonSerialize (): array
88100 {
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- }
101+ $ data = $ this ->buildBaseJson ($ this ->integerOnly ? 'integer ' : 'number ' );
97102
98103 if (null !== $ this ->default ) {
99104 $ data ['default ' ] = $ this ->default ;
0 commit comments