diff --git a/features/parse.feature b/features/parse.feature index d447054..6194dea 100644 --- a/features/parse.feature +++ b/features/parse.feature @@ -35,16 +35,16 @@ Feature: Parse Method Implementation | 18 | 4 ** .5 | 1.000000000000000000 | | 0 | 4 ^ .5 | 2 | | 18 | 4 ^ .5 | 2.000000000000000000 | - | 0 | 1 = 5 | false | - | 18 | 1 = 5 | false | - | 0 | 1 == 5 | false | - | 18 | 1 == 5 | false | - | 0 | 1 > 5 | false | - | 18 | 1 > 5 | false | + | 0 | 1 = 5 | 0 | + | 18 | 1 = 5 | 0 | + | 0 | 1 == 5 | 0 | + | 18 | 1 == 5 | 0 | + | 0 | 1 > 5 | 0 | + | 18 | 1 > 5 | 0 | | 0 | 1 < 5 | true | | 18 | 1 < 5 | true | - | 0 | 1 >= 5 | false | - | 18 | 1 >= 5 | false | + | 0 | 1 >= 5 | 0 | + | 18 | 1 >= 5 | 0 | | 0 | 1 <= 5 | true | | 18 | 1 <= 5 | true | | 0 | 1 <> 5 | true | diff --git a/src/BC.php b/src/BC.php index 73a5b99..8bb72af 100644 --- a/src/BC.php +++ b/src/BC.php @@ -393,7 +393,7 @@ public static function round($val, $scale = null) * @param integer|null $scale The scale to pass to BC::math methods * @return string|integer|float|boolean */ - public static function parse($formula, $values = [], $scale = null) + public static function parse($formula, $values = [], $scale = null, $returnBool = false) { $scale = static::getScale($scale); @@ -433,17 +433,17 @@ public static function parse($formula, $values = [], $scale = null) case '**': $result = static::pow($opTrio[1], $opTrio[3], $scale); break; case '^': $result = static::powfrac($opTrio[1], $opTrio[3], $scale); break; case '==': - case '=': $result = static::comp($opTrio[1], $opTrio[3], $scale) == 0; break; - case '>': $result = static::comp($opTrio[1], $opTrio[3], $scale) > 0; break; - case '<': $result = static::comp($opTrio[1], $opTrio[3], $scale) < 0; break; - case '>=': $result = static::comp($opTrio[1], $opTrio[3], $scale) >= 0; break; - case '<=': $result = static::comp($opTrio[1], $opTrio[3], $scale) <= 0; break; + case '=': $result = static::comp($opTrio[1], $opTrio[3], $scale) == 0 ? 1:0; break; + case '>': $result = static::comp($opTrio[1], $opTrio[3], $scale) > 0 ? 1:0; break; + case '<': $result = static::comp($opTrio[1], $opTrio[3], $scale) < 0 ? 1:0; break; + case '>=': $result = static::comp($opTrio[1], $opTrio[3], $scale) >= 0 ? 1:0; break; + case '<=': $result = static::comp($opTrio[1], $opTrio[3], $scale) <= 0 ? 1:0; break; case '<>': - case '!=': $result = static::comp($opTrio[1], $opTrio[3], $scale) != 0; break; + case '!=': $result = static::comp($opTrio[1], $opTrio[3], $scale) != 0 ? 1:0; break; case '|': - case '||': $result = ($opTrio[1] || $opTrio[3]); break; + case '||': $result = ($opTrio[1] || $opTrio[3]) ? 1:0; break; case '&': - case '&&': $result = ($opTrio[1] && $opTrio[3]); break; + case '&&': $result = ($opTrio[1] && $opTrio[3]) ? 1:0; break; case '~': case '~~': $result = (bool) ((bool) $opTrio[1] ^ (bool) $opTrio[3]); break; } @@ -454,7 +454,9 @@ public static function parse($formula, $values = [], $scale = null) // error_log("Replacing {$parenthetical[0]} with {$parenthetical[1]} in {$formula}"); $formula = str_replace($parenthetical[0], $parenthetical[1], $formula); } - + if($returnBool) { + return (bool)$formula; + } return $formula; } diff --git a/tests/BCTest.php b/tests/BCTest.php index ef08e83..716343a 100644 --- a/tests/BCTest.php +++ b/tests/BCTest.php @@ -201,18 +201,18 @@ public function testParse() $this->assertEquals(4.5, BC::parse('5.5 -% 2')); $this->assertEquals(1, BC::parse('4 ** .5')); $this->assertEquals(2, BC::parse('4 ^ .5')); - $this->assertEquals(false, BC::parse('1 = 5')); - $this->assertEquals(false, BC::parse('1 == 5')); - $this->assertEquals(false, BC::parse('1 > 5')); - $this->assertEquals(true, BC::parse('1 < 5')); - $this->assertEquals(false, BC::parse('1 >= 5')); - $this->assertEquals(true, BC::parse('1 <= 5')); - $this->assertEquals(true, BC::parse('1 <> 5')); - $this->assertEquals(true, BC::parse('1 != 5')); - $this->assertEquals(true, BC::parse('1 & 5')); - $this->assertEquals(true, BC::parse('1 && 5')); - $this->assertEquals(true, BC::parse('1 | 5')); - $this->assertEquals(true, BC::parse('1 || 5')); + $this->assertEquals(0, BC::parse('1 = 5')); + $this->assertEquals(0, BC::parse('1 == 5')); + $this->assertEquals(0, BC::parse('1 > 5')); + $this->assertEquals(1, BC::parse('1 < 5')); + $this->assertEquals(0, BC::parse('1 >= 5')); + $this->assertEquals(1, BC::parse('1 <= 5')); + $this->assertEquals(1, BC::parse('1 <> 5')); + $this->assertEquals(1, BC::parse('1 != 5')); + $this->assertEquals(1, BC::parse('1 & 5')); + $this->assertEquals(1, BC::parse('1 && 5')); + $this->assertEquals(1, BC::parse('1 | 5')); + $this->assertEquals(1, BC::parse('1 || 5')); $this->assertEquals(false, BC::parse('1 ~ 5')); $this->assertEquals(false, BC::parse('1 ~~ 5')); $this->assertEquals('2.25', BC::parse('5 / 4 + 1')); @@ -225,6 +225,7 @@ public function testParse() $this->assertEquals('1', BC::parse('(1 + 5) / 4', null, 0)); $this->assertEquals('1.5', BC::parse('({a} + {b}) / {c}', ['a' => 1, 'b' => 5, 'c' => 4])); $this->assertEquals('1', BC::parse('({a} + {b}) / {c}', ['a' => 1, 'b' => 5, 'c' => 4], 0)); + $this->assertTrue(BC::parse('1', [], 0, true)); } /**