Skip to content

Commit

Permalink
added min/max rules support for strings
Browse files Browse the repository at this point in the history
  • Loading branch information
romalytvynenko committed Jun 28, 2024
1 parent 90f7661 commit f993cd4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Support/Generator/Types/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class ArrayType extends Type
/** @var Type|Schema */
public $prefixItems = [];

private $minItems = null;
public $minItems = null;

private $maxItems = null;
public $maxItems = null;

private $additionalItems = null;
public $additionalItems = null;

public function __construct()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Support/Generator/Types/NumberType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

class NumberType extends Type
{
private $min = null;
public $min = null;

private $max = null;
public $max = null;

public function __construct($type = 'number')
{
Expand Down
26 changes: 26 additions & 0 deletions src/Support/Generator/Types/StringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,34 @@

class StringType extends Type
{
public $min = null;

public $max = null;

public function __construct()
{
parent::__construct('string');
}

public function setMin($min)
{
$this->min = $min;

return $this;
}

public function setMax($max)
{
$this->max = $max;

return $this;
}

public function toArray()
{
return array_merge(parent::toArray(), array_filter([
'minLength' => $this->min,
'maxLength' => $this->max,
], fn ($v) => $v !== null));
}
}
12 changes: 10 additions & 2 deletions src/Support/OperationExtensions/RulesExtractor/RulesMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ public function required(Type $type)

public function min(Type $type, $params)
{
if ($type instanceof NumberType || $type instanceof ArrayType) {
if (
$type instanceof NumberType
|| $type instanceof ArrayType
|| $type instanceof StringType
) {
$type->setMin((float) $params[0]);
}

Expand All @@ -124,7 +128,11 @@ public function min(Type $type, $params)

public function max(Type $type, $params)
{
if ($type instanceof NumberType || $type instanceof ArrayType) {
if (
$type instanceof NumberType
|| $type instanceof ArrayType
|| $type instanceof StringType
) {
$type->setMax((float) $params[0]);
}

Expand Down

0 comments on commit f993cd4

Please sign in to comment.