Skip to content

Commit b633f6e

Browse files
committed
Convert int to string before contains check
1 parent 1aaa71d commit b633f6e

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/Collection/Primitive/Boolean.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ class Boolean extends ValueObject
4242
*
4343
* @var array
4444
*/
45-
protected array $trueValues = ['1', 'true', 1, 'on', 'yes'];
45+
protected array $trueValues = ['1', 'true', 'on', 'yes'];
4646

4747
/**
4848
* Values that represent `false` boolean.
4949
*
5050
* @var array
5151
*/
52-
protected array $falseValues = ['0', 'false', 0, 'off', 'no'];
52+
protected array $falseValues = ['0', 'false', 'off', 'no'];
5353

5454
/**
5555
* Create a new instance of the value object.
@@ -83,9 +83,11 @@ public function value(): bool
8383
*/
8484
protected function handleNonBoolean(int|string $value): void
8585
{
86+
$string = is_string($value) ? $value : (string) $value;
87+
8688
$this->value = match (true) {
87-
Str::contains($value, $this->trueValues, ignoreCase: true) => true,
88-
Str::contains($value, $this->falseValues, ignoreCase: true) => false,
89+
Str::contains($string, $this->trueValues, ignoreCase: true) => true,
90+
Str::contains($string, $this->falseValues, ignoreCase: true) => false,
8991
default => throw new InvalidArgumentException('Invalid boolean.'),
9092
};
9193
}

tests/Unit/Primitive/BooleanTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
$this->assertTrue($valueObject->value());
1212
});
1313

14+
test('throws exception on invalid integer', function () {
15+
$this->expectException(InvalidArgumentException::class);
16+
17+
new Boolean(2);
18+
});
19+
1420
test('boolean can accept integer as a string', function () {
1521
$valueObject = new Boolean('0');
1622
$this->assertFalse($valueObject->value());
@@ -105,10 +111,10 @@
105111
Boolean::macro('getNegativeValues', fn () => $this->falseValues);
106112
$valueObject = new Boolean(1);
107113
$this->assertSame([
108-
'1', 'true', 1, 'on', 'yes',
114+
'1', 'true', 'on', 'yes',
109115
], $valueObject->getPositiveValues());
110116
$this->assertSame([
111-
'0', 'false', 0, 'off', 'no',
117+
'0', 'false', 'off', 'no',
112118
], $valueObject->getNegativeValues());
113119
});
114120

0 commit comments

Comments
 (0)