Skip to content

Commit acfcc0a

Browse files
authored
Merge pull request #40 from DoclerLabs/fix-float-for-maximum-minimum
fix float for maximum and minimum constraints
2 parents b8b0ed5 + 4b38004 commit acfcc0a

File tree

7 files changed

+39
-29
lines changed

7 files changed

+39
-29
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [5.6.1] - 2021-05-25
8+
### Fixed
9+
- Minimum and Maximum constraints work for floats
10+
711
## [5.6.0] - 2021-05-10
812
### Added
913
- Api Client Generator version to README generation

src/Entity/Constraint/MaximumConstraint.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55
namespace DoclerLabs\ApiClientGenerator\Entity\Constraint;
66

77
use DoclerLabs\ApiClientGenerator\Ast\Builder\CodeBuilder;
8+
use DoclerLabs\ApiClientGenerator\Entity\FieldType;
89
use PhpParser\Node\Expr;
910
use PhpParser\Node\Expr\Variable;
1011

1112
class MaximumConstraint implements ConstraintInterface
1213
{
13-
private ?int $maximum = null;
14+
private ?float $maximum = null;
15+
1416
private ?bool $exclusiveMaximum = null;
1517

16-
public function __construct(?int $maximum, ?bool $exclusiveMaximum)
18+
private FieldType $fieldType;
19+
20+
public function __construct(?float $maximum, ?bool $exclusiveMaximum, FieldType $fieldType)
1721
{
1822
$this->maximum = $maximum;
1923
$this->exclusiveMaximum = $exclusiveMaximum;
20-
}
21-
22-
public function getMaximum(): ?int
23-
{
24-
return $this->maximum;
24+
$this->fieldType = $fieldType;
2525
}
2626

2727
public function isExclusiveMaximum(): ?bool
@@ -39,7 +39,7 @@ public function getIfCondition(Variable $variable, CodeBuilder $builder): Expr
3939
return $builder->compare(
4040
$variable,
4141
$this->exclusiveMaximum === true ? '>=' : '>',
42-
$builder->val($this->maximum)
42+
$builder->val($this->fieldType->isInteger() ? (int)$this->maximum : $this->maximum)
4343
);
4444
}
4545

@@ -48,7 +48,7 @@ public function getExceptionMessage(): string
4848
return sprintf(
4949
'Cannot be greater than %s%s.',
5050
$this->exclusiveMaximum === true ? 'or equal to ' : '',
51-
$this->maximum
51+
$this->fieldType->isInteger() ? (int)$this->maximum : $this->maximum
5252
);
5353
}
5454
}

src/Entity/Constraint/MinimumConstraint.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@
55
namespace DoclerLabs\ApiClientGenerator\Entity\Constraint;
66

77
use DoclerLabs\ApiClientGenerator\Ast\Builder\CodeBuilder;
8+
use DoclerLabs\ApiClientGenerator\Entity\FieldType;
89
use PhpParser\Node\Expr;
910
use PhpParser\Node\Expr\Variable;
1011

1112
class MinimumConstraint implements ConstraintInterface
1213
{
13-
private ?int $minimum = null;
14+
private ?float $minimum = null;
15+
1416
private ?bool $exclusiveMinimum = null;
1517

16-
public function __construct(?int $minimum, ?bool $exclusiveMinimum)
18+
private FieldType $fieldType;
19+
20+
public function __construct(?float $minimum, ?bool $exclusiveMinimum, FieldType $fieldType)
1721
{
1822
$this->minimum = $minimum;
1923
$this->exclusiveMinimum = $exclusiveMinimum;
24+
$this->fieldType = $fieldType;
2025
}
2126

2227
public function exists(): bool
@@ -29,7 +34,7 @@ public function getIfCondition(Variable $variable, CodeBuilder $builder): Expr
2934
return $builder->compare(
3035
$variable,
3136
$this->exclusiveMinimum === true ? '<=' : '<',
32-
$builder->val($this->minimum)
37+
$builder->val($this->fieldType->isInteger() ? (int)$this->minimum : $this->minimum)
3338
);
3439
}
3540

@@ -38,7 +43,7 @@ public function getExceptionMessage(): string
3843
return sprintf(
3944
'Cannot be less than %s%s.',
4045
$this->exclusiveMinimum === true ? 'or equal to ' : '',
41-
$this->minimum
46+
$this->fieldType->isInteger() ? (int)$this->minimum : $this->minimum
4247
);
4348
}
4449
}

src/Input/Factory/FieldFactory.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,13 @@ public function create(
9494
$objectProperties = $this->mapProperties($operationName, $schema, $referenceName);
9595
}
9696

97-
$field = new Field(
97+
$fieldType = new FieldType($type);
98+
$field = new Field(
9899
$fieldName,
99-
new FieldType($type),
100+
$fieldType,
100101
new ConstraintCollection(
101-
new MinimumConstraint($schema->minimum, $schema->exclusiveMinimum),
102-
new MaximumConstraint($schema->maximum, $schema->exclusiveMaximum),
102+
new MinimumConstraint($schema->minimum, $schema->exclusiveMinimum, $fieldType),
103+
new MaximumConstraint($schema->maximum, $schema->exclusiveMaximum, $fieldType),
103104
new MinLengthConstraint($schema->minLength),
104105
new MaxLengthConstraint($schema->maxLength),
105106
new PatternConstraint($schema->pattern),

test/suite/functional/Generator/Schema/ItemPhp70.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,10 @@ public function setOptionalIntegerBetweenExcluded(int $optionalIntegerBetweenExc
315315
*/
316316
public function setOptionalNumberBetweenIncluded(float $optionalNumberBetweenIncluded): self
317317
{
318-
if ($optionalNumberBetweenIncluded < 0) {
318+
if ($optionalNumberBetweenIncluded < 0.0) {
319319
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s`. Cannot be less than 0.', 'optionalNumberBetweenIncluded', $optionalNumberBetweenIncluded));
320320
}
321-
if ($optionalNumberBetweenIncluded > 5) {
321+
if ($optionalNumberBetweenIncluded > 5.0) {
322322
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s`. Cannot be greater than 5.', 'optionalNumberBetweenIncluded', $optionalNumberBetweenIncluded));
323323
}
324324
$this->optionalNumberBetweenIncluded = $optionalNumberBetweenIncluded;
@@ -331,10 +331,10 @@ public function setOptionalNumberBetweenIncluded(float $optionalNumberBetweenInc
331331
*/
332332
public function setOptionalNumberBetweenExcluded(float $optionalNumberBetweenExcluded): self
333333
{
334-
if ($optionalNumberBetweenExcluded <= 0) {
334+
if ($optionalNumberBetweenExcluded <= 0.0) {
335335
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s`. Cannot be less than or equal to 0.', 'optionalNumberBetweenExcluded', $optionalNumberBetweenExcluded));
336336
}
337-
if ($optionalNumberBetweenExcluded >= 5) {
337+
if ($optionalNumberBetweenExcluded >= 5.0) {
338338
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s`. Cannot be greater than or equal to 5.', 'optionalNumberBetweenExcluded', $optionalNumberBetweenExcluded));
339339
}
340340
$this->optionalNumberBetweenExcluded = $optionalNumberBetweenExcluded;

test/suite/functional/Generator/Schema/ItemPhp72.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,10 @@ public function setOptionalIntegerBetweenExcluded(int $optionalIntegerBetweenExc
308308
*/
309309
public function setOptionalNumberBetweenIncluded(float $optionalNumberBetweenIncluded): self
310310
{
311-
if ($optionalNumberBetweenIncluded < 0) {
311+
if ($optionalNumberBetweenIncluded < 0.0) {
312312
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s`. Cannot be less than 0.', 'optionalNumberBetweenIncluded', $optionalNumberBetweenIncluded));
313313
}
314-
if ($optionalNumberBetweenIncluded > 5) {
314+
if ($optionalNumberBetweenIncluded > 5.0) {
315315
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s`. Cannot be greater than 5.', 'optionalNumberBetweenIncluded', $optionalNumberBetweenIncluded));
316316
}
317317
$this->optionalNumberBetweenIncluded = $optionalNumberBetweenIncluded;
@@ -324,10 +324,10 @@ public function setOptionalNumberBetweenIncluded(float $optionalNumberBetweenInc
324324
*/
325325
public function setOptionalNumberBetweenExcluded(float $optionalNumberBetweenExcluded): self
326326
{
327-
if ($optionalNumberBetweenExcluded <= 0) {
327+
if ($optionalNumberBetweenExcluded <= 0.0) {
328328
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s`. Cannot be less than or equal to 0.', 'optionalNumberBetweenExcluded', $optionalNumberBetweenExcluded));
329329
}
330-
if ($optionalNumberBetweenExcluded >= 5) {
330+
if ($optionalNumberBetweenExcluded >= 5.0) {
331331
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s`. Cannot be greater than or equal to 5.', 'optionalNumberBetweenExcluded', $optionalNumberBetweenExcluded));
332332
}
333333
$this->optionalNumberBetweenExcluded = $optionalNumberBetweenExcluded;

test/suite/functional/Generator/Schema/ItemPhp74.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,10 @@ public function setOptionalIntegerBetweenExcluded(int $optionalIntegerBetweenExc
280280
*/
281281
public function setOptionalNumberBetweenIncluded(float $optionalNumberBetweenIncluded): self
282282
{
283-
if ($optionalNumberBetweenIncluded < 0) {
283+
if ($optionalNumberBetweenIncluded < 0.0) {
284284
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s`. Cannot be less than 0.', 'optionalNumberBetweenIncluded', $optionalNumberBetweenIncluded));
285285
}
286-
if ($optionalNumberBetweenIncluded > 5) {
286+
if ($optionalNumberBetweenIncluded > 5.0) {
287287
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s`. Cannot be greater than 5.', 'optionalNumberBetweenIncluded', $optionalNumberBetweenIncluded));
288288
}
289289
$this->optionalNumberBetweenIncluded = $optionalNumberBetweenIncluded;
@@ -296,10 +296,10 @@ public function setOptionalNumberBetweenIncluded(float $optionalNumberBetweenInc
296296
*/
297297
public function setOptionalNumberBetweenExcluded(float $optionalNumberBetweenExcluded): self
298298
{
299-
if ($optionalNumberBetweenExcluded <= 0) {
299+
if ($optionalNumberBetweenExcluded <= 0.0) {
300300
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s`. Cannot be less than or equal to 0.', 'optionalNumberBetweenExcluded', $optionalNumberBetweenExcluded));
301301
}
302-
if ($optionalNumberBetweenExcluded >= 5) {
302+
if ($optionalNumberBetweenExcluded >= 5.0) {
303303
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s`. Cannot be greater than or equal to 5.', 'optionalNumberBetweenExcluded', $optionalNumberBetweenExcluded));
304304
}
305305
$this->optionalNumberBetweenExcluded = $optionalNumberBetweenExcluded;

0 commit comments

Comments
 (0)