Skip to content

Commit

Permalink
toBool modifier (#6159)
Browse files Browse the repository at this point in the history
  • Loading branch information
edalzell authored Jun 9, 2022
1 parent 8208c5a commit a66ac9c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Modifiers/CoreModifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2460,6 +2460,22 @@ public function title($value)
return Stringy::titleize($value, $ignore);
}

/**
* Convert value to a boolean.
*
* @param $params
* @param $value
* @return bool
*/
public function toBool($value, $params)
{
if (is_string($value)) {
return Str::toBool($value);
}

return boolval($value);
}

/**
* Converts the data to json.
*
Expand Down
26 changes: 26 additions & 0 deletions tests/Modifiers/ToBoolTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Tests\Modifiers;

use Statamic\Modifiers\Modify;
use Tests\TestCase;

class ToBoolTest extends TestCase
{
/** @test */
public function it_bools(): void
{
$this->assertTrue($this->modify(1));
$this->assertFalse($this->modify(0));
$this->assertTrue($this->modify('foo'));
$this->assertFalse($this->modify('false'));
$this->assertFalse($this->modify('FALSE'));
$this->assertFalse($this->modify('False'));
$this->assertTrue($this->modify(new \stdClass));
}

private function modify($value)
{
return Modify::value($value)->toBool()->fetch();
}
}

0 comments on commit a66ac9c

Please sign in to comment.