Skip to content

Commit 33a69ed

Browse files
committed
#3 ✨ feat: Add StringHelper::toBoolean() method
- [x] Implements `toBoolean()` method in `StringHelper` to convert string values to boolean. - [x] Includes comprehensive test cases for various boolean representations (string, numeric, aliases) and default fallback behavior. - [x] Ensures method chaining works correctly with `set()` and `toBoolean()`.
1 parent 33391d7 commit 33a69ed

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

src/String/StringHelper.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,62 @@ public function getResult()
235235
{
236236
return $this->string;
237237
}
238+
239+
/**
240+
* Convert current value to boolean.
241+
*
242+
* Mendukung berbagai representasi:
243+
* - Boolean string: "true", "false" (case-insensitive)
244+
* - Truthy/Falsy string umum: "yes/no", "y/n", "on/off", "t/f"
245+
* - Numerik: "1"/"0" atau angka lain (non-zero -> true, zero -> false)
246+
* - JSON boolean: 'true' / 'false'
247+
*
248+
* Jika konversi gagal dan $default disediakan, kembalikan $default.
249+
* Jika $default tidak disediakan dan konversi gagal, kembalikan false.
250+
*
251+
* Contoh:
252+
* $helper->set('true')->toBoolean(); // true
253+
* $helper->set('maybe')->toBoolean(true); // true (fallback)
254+
* $helper->set('0')->toBoolean(); // false
255+
*
256+
* @param bool|null $default Nilai pengganti jika konversi gagal
257+
* @return bool
258+
*/
259+
public function toBoolean(?bool $default = null): bool
260+
{
261+
$value = $this->string;
262+
263+
// Normalisasi string
264+
$trimmed = trim((string)$value);
265+
266+
// 1) Coba JSON boolean murni
267+
if ($trimmed === 'true' || $trimmed === 'false') {
268+
return $trimmed === 'true';
269+
}
270+
271+
// 2) FILTER_VALIDATE_BOOLEAN mendukung: true/false, on/off, yes/no, 1/0 (case-insensitive)
272+
$filtered = filter_var($trimmed, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
273+
if ($filtered !== null) {
274+
return (bool)$filtered;
275+
}
276+
277+
// 3) Numerik umum: non-zero -> true, zero -> false
278+
if (is_numeric($trimmed)) {
279+
return ((float)$trimmed) != 0.0;
280+
}
281+
282+
// 4) Alias tambahan: t/f, y/n
283+
$lower = strtolower($trimmed);
284+
$aliasesTrue = ['t', 'y'];
285+
$aliasesFalse = ['f', 'n'];
286+
if (in_array($lower, $aliasesTrue, true)) {
287+
return true;
288+
}
289+
if (in_array($lower, $aliasesFalse, true)) {
290+
return false;
291+
}
292+
293+
// Gagal konversi: gunakan default bila ada, jika tidak false
294+
return $default ?? false;
295+
}
238296
}

tests/String/StringHelperTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,52 @@ public function testGetResult()
132132
$this->stringHelper->set($string);
133133
$this->assertEquals($string, $this->stringHelper->getResult());
134134
}
135+
136+
public function testToBoolean_BooleanStrings()
137+
{
138+
$this->assertTrue($this->stringHelper->set('true')->toBoolean());
139+
$this->assertFalse($this->stringHelper->set('false')->toBoolean());
140+
$this->assertTrue($this->stringHelper->set('TRUE')->toBoolean());
141+
$this->assertFalse($this->stringHelper->set('FALSE')->toBoolean());
142+
}
143+
144+
public function testToBoolean_YesNo_OnOff_TF_YN()
145+
{
146+
$this->assertTrue($this->stringHelper->set('yes')->toBoolean());
147+
$this->assertFalse($this->stringHelper->set('no')->toBoolean());
148+
$this->assertTrue($this->stringHelper->set('on')->toBoolean());
149+
$this->assertFalse($this->stringHelper->set('off')->toBoolean());
150+
151+
$this->assertTrue($this->stringHelper->set('t')->toBoolean());
152+
$this->assertFalse($this->stringHelper->set('f')->toBoolean());
153+
$this->assertTrue($this->stringHelper->set('y')->toBoolean());
154+
$this->assertFalse($this->stringHelper->set('n')->toBoolean());
155+
}
156+
157+
public function testToBoolean_Numeric()
158+
{
159+
$this->assertTrue($this->stringHelper->set('1')->toBoolean());
160+
$this->assertFalse($this->stringHelper->set('0')->toBoolean());
161+
$this->assertTrue($this->stringHelper->set('42')->toBoolean());
162+
$this->assertTrue($this->stringHelper->set('-1')->toBoolean());
163+
$this->assertFalse($this->stringHelper->set('0.0')->toBoolean());
164+
$this->assertTrue($this->stringHelper->set('0.1')->toBoolean());
165+
}
166+
167+
public function testToBoolean_DefaultFallback()
168+
{
169+
$this->assertTrue($this->stringHelper->set('maybe')->toBoolean(true));
170+
$this->assertFalse($this->stringHelper->set('maybe')->toBoolean(false));
171+
// Tanpa default -> fallback false
172+
$this->assertFalse($this->stringHelper->set('maybe')->toBoolean());
173+
}
174+
175+
public function testToBoolean_MethodChaining()
176+
{
177+
// Pastikan set()->toBoolean() bekerja berurutan untuk nilai berbeda
178+
$this->assertTrue($this->stringHelper->set('true')->toBoolean());
179+
$this->assertFalse($this->stringHelper->set('false')->toBoolean());
180+
$this->assertTrue($this->stringHelper->set('yes')->toBoolean());
181+
$this->assertFalse($this->stringHelper->set('no')->toBoolean());
182+
}
135183
}

0 commit comments

Comments
 (0)