Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Dec 28, 2023
1 parent e342b12 commit 5e72602
Show file tree
Hide file tree
Showing 4 changed files with 498 additions and 89 deletions.
22 changes: 16 additions & 6 deletions src/Interfaces/StringParameterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,27 @@ public function default(): ?string;

public function assertCompatible(self $parameter): void;

public function withStarts(string $string): self;
public function withStartsWith(string $string): self;

public function starts(): ?string;
public function startsWith(): ?string;

public function withEnds(string $string): self;
public function withEndsWith(string $string): self;

public function ends(): ?string;
public function endsWith(): ?string;

public function withContains(string $string): self;
public function withContains(string ...$strings): self;

public function contains(): ?string;
/**
* @return array<string>
*/
public function contains(): array;

public function withReject(string ...$strings): self;

/**
* @return array<string>
*/
public function reject(): array;

public function withMinLength(int $int): self;

Expand Down
224 changes: 182 additions & 42 deletions src/StringParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,98 @@ final class StringParameter implements StringParameterInterface

private ?string $endsWith = null;

private ?string $contains = null;

private ?int $minLength = null;

private ?int $maxLength = null;

private ?int $length = null;

/**
* @var array<string>
*/
private array $contains = [];

/**
* @var array<string>
*/
private array $reject = [];

public function __invoke(string|Stringable $value): string
{
// $value = strval($value);
// if ($this->regex->match($value) !== []) {
// return $value;
// }

// throw new InvalidArgumentException(
// (string) message(
// "Argument value provided `%provided%` doesn't match the regex `%regex%`",
// provided: $value,
// regex: strval($this->regex),
// )
// );
$value = (string) $value;

Check warning on line 53 in src/StringParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 test on ubuntu-latest

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ private array $reject = []; public function __invoke(string|Stringable $value) : string { - $value = (string) $value; + $value = $value; if ($this->startsWith && !str_starts_with($value, $this->startsWith)) { throw new InvalidArgumentException((string) message("Argument `%value%` doesn't start with `%startsWith%`", value: $value, startsWith: $this->startsWith)); }

Check warning on line 53 in src/StringParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 test on ubuntu-latest

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ private array $reject = []; public function __invoke(string|Stringable $value) : string { - $value = (string) $value; + $value = $value; if ($this->startsWith && !str_starts_with($value, $this->startsWith)) { throw new InvalidArgumentException((string) message("Argument `%value%` doesn't start with `%startsWith%`", value: $value, startsWith: $this->startsWith)); }

Check warning on line 53 in src/StringParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 test on ubuntu-latest

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ private array $reject = []; public function __invoke(string|Stringable $value) : string { - $value = (string) $value; + $value = $value; if ($this->startsWith && !str_starts_with($value, $this->startsWith)) { throw new InvalidArgumentException((string) message("Argument `%value%` doesn't start with `%startsWith%`", value: $value, startsWith: $this->startsWith)); }
if ($this->startsWith && ! str_starts_with($value, $this->startsWith)) {
throw new InvalidArgumentException(
(string) message(
"Argument `%value%` doesn't start with `%startsWith%`",
value: $value,
startsWith: $this->startsWith,
)
);
}
if ($this->endsWith && ! str_ends_with($value, $this->endsWith)) {
throw new InvalidArgumentException(
(string) message(
"Argument `%value%` doesn't ends with `%endsWith%`",
value: $value,
endsWith: $this->endsWith,
)
);
}
$strlen = mb_strlen($value);
if ($this->minLength !== null && $strlen < $this->minLength) {
throw new InvalidArgumentException(
(string) message(
'Argument `%value%` length (%strlen%) is less than %minLength%',
value: $value,
strlen: $strlen,
minLength: $this->minLength,
)
);
}
if ($this->maxLength !== null && $strlen > $this->maxLength) {
throw new InvalidArgumentException(
(string) message(
'Argument `%value%` length (%strlen%) is greater than %maxLength%',
value: $value,
strlen: $strlen,
maxLength: $this->maxLength,
)
);
}
if ($this->length !== null && $strlen !== $this->length) {
throw new InvalidArgumentException(
(string) message(
'Argument `%value%` length (%strlen%) is different from %length%',
value: $value,
strlen: $strlen,
length: $this->length,
)
);
}
foreach ($this->contains as $string) {
if (! str_contains($value, $string)) {
throw new InvalidArgumentException(
(string) message(
"Argument `%value%` doesn't contain `%contains%`",
value: $value,
contains: $string,
)
);
}
}
foreach ($this->reject as $string) {
if (str_contains($value, $string)) {
throw new InvalidArgumentException(
(string) message(
'Argument `%value%` contains rejected value `%reject%`',
value: $value,
reject: $string,
)
);
}
}

return (string) $value;
return $value;
}

public function schema(): array
Expand All @@ -64,6 +132,13 @@ public function schema(): array
'type' => $this->type()->primitive(),
'description' => $this->description(),
'default' => $this->default(),
'startsWith' => $this->startsWith(),
'endsWith' => $this->endsWith(),
'contains' => $this->contains(),
'reject' => $this->reject(),
'minLength' => $this->minLength(),
'maxLength' => $this->maxLength(),
'length' => $this->length(),
];
}

Expand All @@ -84,7 +159,7 @@ public function default(): ?string
return $this->default;
}

public function withStarts(string $string): StringParameterInterface
public function withStartsWith(string $string): StringParameterInterface
{
$strlen = mb_strlen($string);
$this->assertRuleLength('length', $strlen);
Expand All @@ -95,12 +170,12 @@ public function withStarts(string $string): StringParameterInterface
return $new;
}

public function starts(): ?string
public function startsWith(): ?string
{
return $this->startsWith;
}

public function withEnds(string $string): StringParameterInterface
public function withEndsWith(string $string): StringParameterInterface
{
$strlen = mb_strlen($string);
$this->assertRuleLength('length', $strlen);
Expand All @@ -111,23 +186,25 @@ public function withEnds(string $string): StringParameterInterface
return $new;
}

public function ends(): ?string
public function endsWith(): ?string
{
return $this->endsWith;
}

public function withContains(string $string): StringParameterInterface
public function withContains(string ...$strings): StringParameterInterface
{
$strlen = mb_strlen($string);
$this->assertRuleLength('length', $strlen);
$this->assertRuleLength('maxLength', $strlen);
$new = clone $this;
$new->contains = $string;
foreach ($strings as $string) {
$strlen = mb_strlen($string);
$this->assertRuleLength('length', $strlen);
$this->assertRuleLength('maxLength', $strlen);
}
$new->contains = $strings;

return $new;
}

public function contains(): ?string
public function contains(): array
{
return $this->contains;
}
Expand Down Expand Up @@ -168,9 +245,26 @@ public function withMaxLength(int $int): StringParameterInterface
)
);
}
$this->assertRuleLength('startsWith', $int);
$this->assertRuleLength('endsWith', $int);
$this->assertRuleLength('contains', $int);
$this->assertRuleLength(
rule: 'startsWith',
try: $int,
operand: '<'
);
$this->assertRuleLength(
rule: 'endsWith',
try: $int,
operand: '<'
);
$this->assertRuleLength(
rule: 'contains',
try: $int,
operand: '<'
);
$this->assertRuleLength(
rule: 'reject',
try: $int,
operand: '<'
);
$new = clone $this;
$new->maxLength = $int;

Expand All @@ -194,9 +288,26 @@ public function withLength(int $int): StringParameterInterface
)
);
}
$this->assertRuleLength('startsWith', $int, '<');
$this->assertRuleLength('endsWith', $int, '<');
$this->assertRuleLength('contains', $int, '<');
$this->assertRuleLength(
rule: 'startsWith',
try: $int,
operand: '<'
);
$this->assertRuleLength(
rule: 'endsWith',
try: $int,
operand: '<'
);
$this->assertRuleLength(
rule: 'contains',
try: $int,
operand: '<'
);
$this->assertRuleLength(
rule: 'reject',
try: $int,
operand: '<'
);
$new = clone $this;
$new->length = $int;

Expand All @@ -208,30 +319,59 @@ public function length(): ?int
return $this->length;
}

public function withReject(string ...$strings): StringParameterInterface
{
foreach ($strings as $string) {
$strlen = mb_strlen($string);
$this->assertRuleLength('length', $strlen);
$this->assertRuleLength('maxLength', $strlen);
}
$new = clone $this;
$new->reject = $strings;

return $new;
}

public function reject(): array
{
return $this->reject;
}

// @phpstan-ignore-next-line
private function assertRuleLength(
string $rule,
int $length,
int $try,
null|array|int|string $length = null,
string $operand = '>'
): void {
if ($this->{$rule} === null) {
return;
}
$ruleLength = match (is_int($this->{$rule})) {
true => $this->{$rule},
default => mb_strlen($this->{$rule}),
};
$length = $length ?? $this->{$rule};
if (is_array($length)) {
foreach ($length as $string) {
$this->assertRuleLength($rule, $try, $string, $operand);
}

return;
}
if (is_string($length)) {
$length = mb_strlen($length);
}
// @infection-ignore-all
$result = match (true) {
$operand === '>' => $length > $ruleLength,
$operand === '>=' => $length >= $ruleLength,
$operand === '<' => $length < $ruleLength,
$operand === '<=' => $length <= $ruleLength,
default => $length > $ruleLength,
$operand === '>' => $try > $length,
$operand === '>=' => $try >= $length,
$operand === '<' => $try < $length,
$operand === '<=' => $try <= $length,
default => $try > $length,
};
if ($result) {
throw new LogicException(
(string) message(
'Argument value provided conflicts with `%rule%` rule',
'Argument value provided conflicts with `%rule%` rule length `%ruleLength%`',
rule: $rule,
ruleLength: $length,
)
);
}
Expand Down
Loading

0 comments on commit 5e72602

Please sign in to comment.