Skip to content

Commit e018273

Browse files
authored
Merge pull request #29 from DoclerLabs/extra-field-validations
adding extra field validations
2 parents 36496bb + ef2f5d3 commit e018273

20 files changed

+1172
-72
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ 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

77

8+
## [5.2.0] - 2021-04-06
9+
### Added
10+
- Support for `minimum`, `maximum`, `exclusiveMinimum`, `exclusiveMaximum`, `minItems`, `maxItems`, `pattern`, `maxLength`, `minLength` validations
11+
12+
### Changed
13+
- Example generated with 7.4
14+
815
## [5.1.0] - 2021-04-02
916
### Changed
1017
- Only content type serializers which are used are included
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoclerLabs\ApiClientGenerator\Entity\Constraint;
6+
7+
use ArrayIterator;
8+
use IteratorAggregate;
9+
use Traversable;
10+
11+
class ConstraintCollection implements IteratorAggregate
12+
{
13+
private array $contraints;
14+
15+
public function __construct(ConstraintInterface ...$constraints)
16+
{
17+
$this->contraints = $constraints;
18+
}
19+
20+
public function getIterator(): Traversable
21+
{
22+
return new ArrayIterator($this->contraints);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoclerLabs\ApiClientGenerator\Entity\Constraint;
6+
7+
use DoclerLabs\ApiClientGenerator\Ast\Builder\CodeBuilder;
8+
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\Variable;
10+
11+
interface ConstraintInterface
12+
{
13+
public function exists(): bool;
14+
15+
public function getIfCondition(Variable $variable, CodeBuilder $builder): Expr;
16+
17+
public function getExceptionMessage(): string;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoclerLabs\ApiClientGenerator\Entity\Constraint;
6+
7+
use DoclerLabs\ApiClientGenerator\Ast\Builder\CodeBuilder;
8+
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\Variable;
10+
11+
class MaxItemsConstraint implements ConstraintInterface
12+
{
13+
private ?int $maxItems = null;
14+
15+
public function __construct(?int $maxItems)
16+
{
17+
$this->maxItems = $maxItems;
18+
}
19+
20+
public function getMaxItems(): ?int
21+
{
22+
return $this->maxItems;
23+
}
24+
25+
public function exists(): bool
26+
{
27+
return $this->maxItems !== null;
28+
}
29+
30+
public function getIfCondition(Variable $variable, CodeBuilder $builder): Expr
31+
{
32+
return $builder->compare(
33+
$builder->funcCall('count', [$variable]),
34+
'>',
35+
$builder->val($this->maxItems)
36+
);
37+
}
38+
39+
public function getExceptionMessage(): string
40+
{
41+
return sprintf('Expected max items: `%s`.', $this->maxItems);
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoclerLabs\ApiClientGenerator\Entity\Constraint;
6+
7+
use DoclerLabs\ApiClientGenerator\Ast\Builder\CodeBuilder;
8+
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\Variable;
10+
11+
class MaxLengthConstraint implements ConstraintInterface
12+
{
13+
private ?int $maxLength = null;
14+
15+
public function __construct(?int $maxLength)
16+
{
17+
$this->maxLength = $maxLength;
18+
}
19+
20+
public function getMaxLength(): ?int
21+
{
22+
return $this->maxLength;
23+
}
24+
25+
public function exists(): bool
26+
{
27+
return $this->maxLength !== null;
28+
}
29+
30+
public function getIfCondition(Variable $variable, CodeBuilder $builder): Expr
31+
{
32+
return $builder->compare(
33+
$builder->funcCall('strlen', [$variable]),
34+
'>',
35+
$builder->val($this->maxLength)
36+
);
37+
}
38+
39+
public function getExceptionMessage(): string
40+
{
41+
return sprintf('Length should be less than %s.', $this->maxLength);
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoclerLabs\ApiClientGenerator\Entity\Constraint;
6+
7+
use DoclerLabs\ApiClientGenerator\Ast\Builder\CodeBuilder;
8+
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\Variable;
10+
11+
class MaximumConstraint implements ConstraintInterface
12+
{
13+
private ?int $maximum = null;
14+
private ?bool $exclusiveMaximum = null;
15+
16+
public function __construct(?int $maximum, ?bool $exclusiveMaximum)
17+
{
18+
$this->maximum = $maximum;
19+
$this->exclusiveMaximum = $exclusiveMaximum;
20+
}
21+
22+
public function getMaximum(): ?int
23+
{
24+
return $this->maximum;
25+
}
26+
27+
public function isExclusiveMaximum(): ?bool
28+
{
29+
return $this->exclusiveMaximum;
30+
}
31+
32+
public function exists(): bool
33+
{
34+
return $this->maximum !== null;
35+
}
36+
37+
public function getIfCondition(Variable $variable, CodeBuilder $builder): Expr
38+
{
39+
return $builder->compare(
40+
$variable,
41+
$this->exclusiveMaximum === true ? '>=' : '>',
42+
$builder->val($this->maximum)
43+
);
44+
}
45+
46+
public function getExceptionMessage(): string
47+
{
48+
return sprintf(
49+
'Cannot be greater than %s%s.',
50+
$this->exclusiveMaximum === true ? 'or equal to ' : '',
51+
$this->maximum
52+
);
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoclerLabs\ApiClientGenerator\Entity\Constraint;
6+
7+
use DoclerLabs\ApiClientGenerator\Ast\Builder\CodeBuilder;
8+
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\Variable;
10+
11+
class MinItemsConstraint implements ConstraintInterface
12+
{
13+
private ?int $minItems = null;
14+
15+
public function __construct(?int $minItems)
16+
{
17+
$this->minItems = $minItems;
18+
}
19+
20+
public function getMinItems(): ?int
21+
{
22+
return $this->minItems;
23+
}
24+
25+
public function exists(): bool
26+
{
27+
return $this->minItems !== null;
28+
}
29+
30+
public function getIfCondition(Variable $variable, CodeBuilder $builder): Expr
31+
{
32+
return $builder->compare(
33+
$builder->funcCall('count', [$variable]),
34+
'<',
35+
$builder->val($this->minItems)
36+
);
37+
}
38+
39+
public function getExceptionMessage(): string
40+
{
41+
return sprintf('Expected min items: `%s`.', $this->minItems);
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoclerLabs\ApiClientGenerator\Entity\Constraint;
6+
7+
use DoclerLabs\ApiClientGenerator\Ast\Builder\CodeBuilder;
8+
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\Variable;
10+
11+
class MinLengthConstraint implements ConstraintInterface
12+
{
13+
private ?int $minLength = null;
14+
15+
public function __construct(?int $minLength)
16+
{
17+
$this->minLength = $minLength;
18+
}
19+
20+
public function getMinLength(): ?int
21+
{
22+
return $this->minLength;
23+
}
24+
25+
public function exists(): bool
26+
{
27+
return $this->minLength !== null;
28+
}
29+
30+
public function getIfCondition(Variable $variable, CodeBuilder $builder): Expr
31+
{
32+
return $builder->compare(
33+
$builder->funcCall('strlen', [$variable]),
34+
'<',
35+
$builder->val($this->minLength)
36+
);
37+
}
38+
39+
public function getExceptionMessage(): string
40+
{
41+
return sprintf('Length should be greater than %s.', $this->minLength);
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoclerLabs\ApiClientGenerator\Entity\Constraint;
6+
7+
use DoclerLabs\ApiClientGenerator\Ast\Builder\CodeBuilder;
8+
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\Variable;
10+
11+
class MinimumConstraint implements ConstraintInterface
12+
{
13+
private ?int $minimum = null;
14+
private ?bool $exclusiveMinimum = null;
15+
16+
public function __construct(?int $minimum, ?bool $exclusiveMinimum)
17+
{
18+
$this->minimum = $minimum;
19+
$this->exclusiveMinimum = $exclusiveMinimum;
20+
}
21+
22+
public function exists(): bool
23+
{
24+
return $this->minimum !== null;
25+
}
26+
27+
public function getIfCondition(Variable $variable, CodeBuilder $builder): Expr
28+
{
29+
return $builder->compare(
30+
$variable,
31+
$this->exclusiveMinimum === true ? '<=' : '<',
32+
$builder->val($this->minimum)
33+
);
34+
}
35+
36+
public function getExceptionMessage(): string
37+
{
38+
return sprintf(
39+
'Cannot be less than %s%s.',
40+
$this->exclusiveMinimum === true ? 'or equal to ' : '',
41+
$this->minimum
42+
);
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoclerLabs\ApiClientGenerator\Entity\Constraint;
6+
7+
use DoclerLabs\ApiClientGenerator\Ast\Builder\CodeBuilder;
8+
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\Variable;
10+
11+
class PatternConstraint implements ConstraintInterface
12+
{
13+
private ?string $pattern = null;
14+
15+
public function __construct(?string $pattern)
16+
{
17+
$this->pattern = $pattern;
18+
}
19+
20+
public function getPattern(): ?string
21+
{
22+
return $this->pattern;
23+
}
24+
25+
public function exists(): bool
26+
{
27+
return $this->pattern !== null;
28+
}
29+
30+
public function getIfCondition(Variable $variable, CodeBuilder $builder): Expr
31+
{
32+
return $builder->notEquals(
33+
$builder->funcCall('preg_match', [$builder->val($this->pattern), $variable]),
34+
$builder->val(1)
35+
);
36+
}
37+
38+
public function getExceptionMessage(): string
39+
{
40+
return sprintf('Pattern is %s.', $this->pattern);
41+
}
42+
}

0 commit comments

Comments
 (0)