Skip to content

Commit

Permalink
Merge pull request #16 from alleyinteractive/fix/scalar
Browse files Browse the repository at this point in the history
Fix array-to-string conversion
  • Loading branch information
dlh01 authored Feb 14, 2024
2 parents b73688a + 31ffa3c commit 09fd68d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [ 8.2, 8.1, 8.0 ]
php: [ 8.3, 8.2, 8.1, 8.0 ]
can_fail: [ false ]
name: PHP ${{ matrix.php }}
steps:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ This library adheres to [Semantic Versioning](https://semver.org/) and [Keep a C

Nothing yet.

## 2.1.1

### Fixed

- Array-to-string conversion in `ContainsString`.

## 2.1.0

### Changed
Expand Down
20 changes: 12 additions & 8 deletions src/Alley/Validator/ContainsString.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,21 @@ final class ContainsString extends ExtendedAbstractValidator

protected function testValue($value): void
{
$haystack = (string) $value;
$needle = (string) $this->options['needle'];
if (\is_scalar($value)) {
$haystack = (string) $value;
$needle = (string) $this->options['needle'];

if ($this->options['ignoreCase']) {
$haystack = strtolower($haystack);
$needle = strtolower($needle);
}
if ($this->options['ignoreCase']) {
$haystack = strtolower($haystack);
$needle = strtolower($needle);
}

if (!str_contains($haystack, $needle)) {
$this->error(self::NOT_CONTAINS_STRING);
if (str_contains($haystack, $needle)) {
return;
}
}

$this->error(self::NOT_CONTAINS_STRING);
}

protected function setNeedle($needle)
Expand Down
10 changes: 10 additions & 0 deletions tests/Alley/Validator/ContainsStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,14 @@ public function testInvalidInput()
$validator->getMessages(),
);
}

public function testNonScalar()
{
$validator = new ContainsString(['needle' => 'baz']);
$this->assertFalse($validator->isValid([]));
$this->assertSame(
['notContainsString' => 'Must contain string "baz".'],
$validator->getMessages(),
);
}
}

0 comments on commit 09fd68d

Please sign in to comment.