Skip to content

Commit fa09fa8

Browse files
Merge pull request #30 from michael-rubel/update/boolean
Update `Boolean` object 🔧
2 parents b6e59f3 + b66beea commit fa09fa8

File tree

5 files changed

+33
-47
lines changed

5 files changed

+33
-47
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ A bunch of general-purpose value objects you can use in your Laravel application
1212

1313
---
1414

15-
The package requires PHP `^8.0` and Laravel `^9.7`.
15+
The package requires PHP `^8.0.2` and Laravel `^9.33`.
1616

1717
## #StandWithUkraine
1818
[![SWUbanner](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg)](https://github.com/vshymanskyy/StandWithUkraine/blob/main/docs/README.md)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^8.0",
20-
"illuminate/contracts": "^9.7",
20+
"illuminate/contracts": "^9.33",
2121
"michael-rubel/laravel-formatters": "^7.0.4",
2222
"phpmath/bignumber": "^1.2",
2323
"spatie/laravel-package-tools": "^1.12"

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ parameters:
2020
- '#Method MichaelRubel\\ValueObjects\\Collection\\Complex\\Email\:\:username\(\) should return string but returns string\|null\.#'
2121
- '#Method MichaelRubel\\ValueObjects\\Collection\\Complex\\FullName\:\:firstName\(\) should return string but returns string\|null\.#'
2222
- '#Method MichaelRubel\\ValueObjects\\Collection\\Complex\\FullName\:\:lastName\(\) should return string but returns string\|null\.#'
23+
- '#Parameter \#1 \$value of class Illuminate\\Support\\Stringable constructor expects string, int\|string given\.#'
2324

2425
checkMissingIterableValueType: false
2526

src/Collection/Primitive/Boolean.php

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace MichaelRubel\ValueObjects\Collection\Primitive;
1414

15+
use Illuminate\Support\Stringable;
1516
use InvalidArgumentException;
1617
use MichaelRubel\ValueObjects\ValueObject;
1718

@@ -32,23 +33,23 @@
3233
class Boolean extends ValueObject
3334
{
3435
/**
35-
* @var bool|int|string
36+
* @var bool
3637
*/
37-
protected bool|int|string $value;
38+
protected bool $value;
3839

3940
/**
40-
* Allowed values that are treated as `true`.
41+
* Values that represent `true` boolean.
4142
*
4243
* @var array
4344
*/
44-
protected array $trueValues = ['1', 'true', 'True', 'TRUE', 1, true];
45+
protected array $trueValues = ['1', 'true', 1, 'on', 'yes'];
4546

4647
/**
47-
* Allowed values that are treated as `false`.
48+
* Values that represent `false` boolean.
4849
*
4950
* @var array
5051
*/
51-
protected array $falseValues = ['0', 'false', 'False', 'FALSE', 0, false];
52+
protected array $falseValues = ['0', 'false', 0, 'off', 'no'];
5253

5354
/**
5455
* Create a new instance of the value object.
@@ -61,13 +62,7 @@ public function __construct(bool|int|string $value)
6162
throw new InvalidArgumentException(static::IMMUTABLE_MESSAGE);
6263
}
6364

64-
$this->value = $value;
65-
66-
$this->value = match (true) {
67-
$this->isInTrueValues() => true,
68-
$this->isInFalseValues() => false,
69-
default => throw new InvalidArgumentException('Invalid boolean.'),
70-
};
65+
! is_bool($value) ? $this->handleNonBoolean($value) : $this->value = $value;
7166
}
7267

7368
/**
@@ -77,27 +72,24 @@ public function __construct(bool|int|string $value)
7772
*/
7873
public function value(): bool
7974
{
80-
return (bool) $this->value;
75+
return $this->value;
8176
}
8277

8378
/**
8479
* Determine if the passed boolean is true.
8580
*
86-
* @return bool
81+
* @param int|string $value
82+
* @return void
8783
*/
88-
protected function isInTrueValues(): bool
84+
protected function handleNonBoolean(int|string $value): void
8985
{
90-
return in_array($this->value, $this->trueValues, strict: true);
91-
}
86+
$string = new Stringable($value);
9287

93-
/**
94-
* Determine if the passed boolean is false.
95-
*
96-
* @return bool
97-
*/
98-
protected function isInFalseValues(): bool
99-
{
100-
return in_array($this->value, $this->falseValues, strict: true);
88+
$this->value = match (true) {
89+
$string->contains($this->trueValues, ignoreCase: true) => true,
90+
$string->contains($this->falseValues, ignoreCase: true) => false,
91+
default => throw new InvalidArgumentException('Invalid boolean.'),
92+
};
10193
}
10294

10395
/**
@@ -107,7 +99,7 @@ protected function isInFalseValues(): bool
10799
*/
108100
public function toString(): string
109101
{
110-
return ! empty($this->value()) ? 'true' : 'false';
102+
return $this->value() ? 'true' : 'false';
111103
}
112104

113105
/**

tests/Unit/Primitive/BooleanTest.php

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
$this->assertFalse($valueObject->value());
2424
$valueObject = new Boolean('False');
2525
$this->assertFalse($valueObject->value());
26+
$valueObject = new Boolean('off');
27+
$this->assertFalse($valueObject->value());
28+
$valueObject = new Boolean('no');
29+
$this->assertFalse($valueObject->value());
2630
$valueObject = new Boolean('FALSE');
2731
$this->assertFalse($valueObject->value());
2832
$valueObject = new Boolean('true');
@@ -31,6 +35,10 @@
3135
$this->assertTrue($valueObject->value());
3236
$valueObject = new Boolean('TRUE');
3337
$this->assertTrue($valueObject->value());
38+
$valueObject = new Boolean('on');
39+
$this->assertTrue($valueObject->value());
40+
$valueObject = new Boolean('yes');
41+
$this->assertTrue($valueObject->value());
3442
});
3543

3644
test('boolean can accept native booleans', function () {
@@ -97,10 +105,10 @@
97105
Boolean::macro('getNegativeValues', fn () => $this->falseValues);
98106
$valueObject = new Boolean(1);
99107
$this->assertSame([
100-
'1', 'true', 'True', 'TRUE', 1, true,
108+
'1', 'true', 1, 'on', 'yes',
101109
], $valueObject->getPositiveValues());
102110
$this->assertSame([
103-
'0', 'false', 'False', 'FALSE', 0, false,
111+
'0', 'false', 0, 'off', 'no',
104112
], $valueObject->getNegativeValues());
105113
});
106114

@@ -155,28 +163,13 @@
155163

156164
test('can extend protected methods in boolean', function () {
157165
$bool = new TestBoolean('true');
158-
assertTrue($bool->isInTrueValues());
159-
assertIsBool($bool->value());
160-
161-
$bool = new TestBoolean('false');
162-
assertTrue($bool->isInFalseValues());
163166
assertIsBool($bool->value());
164167
});
165168

166169
class TestBoolean extends Boolean
167170
{
168171
public function __construct(bool|int|string $value)
169172
{
170-
$this->value = $value;
171-
}
172-
173-
public function isInTrueValues(): bool
174-
{
175-
return parent::isInTrueValues();
176-
}
177-
178-
public function isInFalseValues(): bool
179-
{
180-
return parent::isInFalseValues();
173+
! is_bool($value) ? $this->handleNonBoolean($value) : $this->value = $value;
181174
}
182175
}

0 commit comments

Comments
 (0)