Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions infection.json5
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
"ignore": [
"MichaelRubel\\ValueObjects\\Artisan\\ValueObjectMakeCommand::getDefaultNamespace"
]
},
"CastString": {
"ignore": [
"MichaelRubel\\ValueObjects\\Collection\\Primitive\\Boolean::handleNonBoolean"
]
},
"Ternary": {
"ignore": [
"MichaelRubel\\ValueObjects\\Collection\\Primitive\\Boolean::handleNonBoolean"
]
}
}
}
12 changes: 6 additions & 6 deletions src/Collection/Primitive/Boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace MichaelRubel\ValueObjects\Collection\Primitive;

use Illuminate\Support\Stringable;
use Illuminate\Support\Str;
use InvalidArgumentException;
use MichaelRubel\ValueObjects\ValueObject;

Expand Down Expand Up @@ -42,14 +42,14 @@ class Boolean extends ValueObject
*
* @var array
*/
protected array $trueValues = ['1', 'true', 1, 'on', 'yes'];
protected array $trueValues = ['1', 'true', 'on', 'yes'];

/**
* Values that represent `false` boolean.
*
* @var array
*/
protected array $falseValues = ['0', 'false', 0, 'off', 'no'];
protected array $falseValues = ['0', 'false', 'off', 'no'];

/**
* Create a new instance of the value object.
Expand Down Expand Up @@ -83,11 +83,11 @@ public function value(): bool
*/
protected function handleNonBoolean(int|string $value): void
{
$string = new Stringable($value);
$string = is_string($value) ? $value : (string) $value;

$this->value = match (true) {
$string->contains($this->trueValues, ignoreCase: true) => true,
$string->contains($this->falseValues, ignoreCase: true) => false,
Str::contains($string, $this->trueValues, ignoreCase: true) => true,
Str::contains($string, $this->falseValues, ignoreCase: true) => false,
default => throw new InvalidArgumentException('Invalid boolean.'),
};
}
Expand Down
10 changes: 8 additions & 2 deletions tests/Unit/Primitive/BooleanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
$this->assertTrue($valueObject->value());
});

test('throws exception on invalid integer', function () {
$this->expectException(InvalidArgumentException::class);

new Boolean(2);
});

test('boolean can accept integer as a string', function () {
$valueObject = new Boolean('0');
$this->assertFalse($valueObject->value());
Expand Down Expand Up @@ -105,10 +111,10 @@
Boolean::macro('getNegativeValues', fn () => $this->falseValues);
$valueObject = new Boolean(1);
$this->assertSame([
'1', 'true', 1, 'on', 'yes',
'1', 'true', 'on', 'yes',
], $valueObject->getPositiveValues());
$this->assertSame([
'0', 'false', 0, 'off', 'no',
'0', 'false', 'off', 'no',
], $valueObject->getNegativeValues());
});

Expand Down