Boolean formatter.
This commit is contained in:
parent
7ba116e5a0
commit
c6c5cc9251
33
src/Formatters/Boolean.php
Normal file
33
src/Formatters/Boolean.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of web3.php package.
|
||||
*
|
||||
* (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
|
||||
*
|
||||
* @author Peter Lai <alk03073135@gmail.com>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
namespace Web3\Formatters;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Web3\Utils;
|
||||
use Web3\Formatters\IFormatter;
|
||||
|
||||
class Boolean implements IFormatter
|
||||
{
|
||||
/**
|
||||
* format
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return int
|
||||
*/
|
||||
public static function format($value)
|
||||
{
|
||||
if (!is_bool($value)) {
|
||||
throw new InvalidArgumentException('The value to inputFormat function must be boolean.');
|
||||
}
|
||||
return (int) $value;
|
||||
}
|
||||
}
|
45
test/unit/BooleanFormatterTest.php
Normal file
45
test/unit/BooleanFormatterTest.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Test\Unit;
|
||||
|
||||
use Test\TestCase;
|
||||
use Web3\Formatters\Boolean;
|
||||
|
||||
class BooleanFormatterTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* formatter
|
||||
*
|
||||
* @var \Web3\Formatters\Boolean
|
||||
*/
|
||||
protected $formatter;
|
||||
|
||||
/**
|
||||
* setUp
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->formatter = new Boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* testFormat
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testFormat()
|
||||
{
|
||||
$formatter = $this->formatter;
|
||||
|
||||
$boolean = $formatter->format(true);
|
||||
|
||||
$this->assertEquals($boolean, 1);
|
||||
|
||||
$boolean = $formatter->format(false);
|
||||
|
||||
$this->assertEquals($boolean, 0);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user