Skip to content
Commits on Source (3)
<?php
/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/
/*
================================================================================
EvalMath - PHP Class to safely evaluate math expressions
Copyright (C) 2005 Miles Kaufmann <http://www.twmagic.com/>
================================================================================
NAME
EvalMath - safely evaluate math expressions
SYNOPSIS
<?
include('evalmath.class.php');
$m = new EvalMath;
// basic evaluation:
$result = $m->evaluate('2+2');
// supports: order of operation; parentheses; negation; built-in functions
$result = $m->evaluate('-8(5/2)^2*(1-sqrt(4))-8');
// create your own variables
$m->evaluate('a = e^(ln(pi))');
// or functions
$m->evaluate('f(x,y) = x^2 + y^2 - 2x*y + 1');
// and then use them
$result = $m->evaluate('3*f(42,a)');
?>
DESCRIPTION
Use the EvalMath class when you want to evaluate mathematical expressions
from untrusted sources. You can define your own variables and functions,
which are stored in the object. Try it, it's fun!
METHODS
$m->evalute($expr)
Evaluates the expression and returns the result. If an error occurs,
prints a warning and returns false. If $expr is a function assignment,
returns true on success.
$m->e($expr)
A synonym for $m->evaluate().
$m->vars()
Returns an associative array of all user-defined variables and values.
$m->funcs()
Returns an array of all user-defined functions.
PARAMETERS
$m->suppress_errors
Set to true to turn off warnings when evaluating expressions
$m->last_error
If the last evaluation failed, contains a string describing the error.
(Useful when suppress_errors is on).
AUTHOR INFORMATION
Copyright 2005, Miles Kaufmann.
LICENSE
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1 Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
class EvalMath
{
public bool $suppress_errors = false;
......
<?php
/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/
*********************************************************************/
class ilMath
{
/**
......
<?php
/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/
*********************************************************************/
/**
* Class ilMathBCMathAdapter
* @author Michael Jansen <mjansen@databay.de>
......
<?php
/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/
*********************************************************************/
/**
* Class ilMathBaseAdapter
* @author Michael Jansen <mjansen@databay.de>
......
<?php
/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/
*********************************************************************/
/**
* Class ilMathPhpAdapter
* @author Michael Jansen <mjansen@databay.de>
......
<?php
/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
<?php declare(strict_types=1);
/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/
*********************************************************************/
/**
* Class ilMathException
* @author Michael Jansen <mjansen@@databay.de>
* @author Michael Jansen <mjansen@databay.de>
*/
class ilMathDivisionByZeroException extends ilMathException
{
......
<?php
/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
<?php declare(strict_types=1);
/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/
*********************************************************************/
/**
* Class ilMathException
* @author Michael Jansen <mjansen@@databay.de>
* @author Michael Jansen <mjansen@databay.de>
*/
class ilMathException extends ilException
{
......
<?php
/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/
*********************************************************************/
/**
* Interface ilMathAdapter
* @author Michael Jansen <mjansen@databay.de>
......
<?php
require_once 'Services/Math/test/ilMathBaseAdapterTest.php';
/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/
*********************************************************************/
require_once 'Services/Math/test/ilMathBaseAdapterTest.php';
class ilMathBCAdapterTest extends ilMathBaseAdapterTest
{
/**
* @inheritDoc
*/
public function setUp() : void
protected function setUp() : void
{
if (!extension_loaded('bcmath')) {
$this->markTestSkipped('Could not execute test due to missing bcmath extension!');
......@@ -29,7 +34,7 @@ class ilMathBCAdapterTest extends ilMathBaseAdapterTest
}
/**
* @return mixed[]
* @return array
*/
public function powData() : array
{
......
<?php
use PHPUnit\Framework\TestCase;
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/
*********************************************************************/
use PHPUnit\Framework\TestCase;
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
/**
* @author Michael Jansen <mjansen@databay.de>
* @version $Id$
* @author Michael Jansen <mjansen@databay.de>
*/
abstract class ilMathBaseAdapterTest extends TestCase
{
const DEFAULT_SCALE = 50;
protected const DEFAULT_SCALE = 50;
/**
* @var ilMathAdapter
*/
protected $mathAdapter;
/**
* @var EvalMath
*/
protected $evalMath;
protected ilMathAdapter $mathAdapter;
protected EvalMath $evalMath;
/**
* @inheritDoc
......@@ -52,17 +48,18 @@ abstract class ilMathBaseAdapterTest extends TestCase
* @param string $actual
* @param string $expected
*/
private function assertEqualNumbers(string $actual, string $expected)
private function assertEqualNumbers(string $actual, string $expected) : void
{
$differ = new Differ(new UnifiedDiffOutputBuilder("\n--- Expected\n+++ Actual\n"));
/** @noinspection PhpUnitTestsInspection */
$this->assertTrue($actual == $expected, $differ->diff($actual, $expected));
}
/**
* @dataProvider addData
*/
public function testAdd(string $a, string $b, string $result, int $scale)
public function testAdd(string $a, string $b, string $result, int $scale) : void
{
$this->assertEqualNumbers($result, $this->mathAdapter->add($a, $b, $scale));
}
......@@ -70,7 +67,7 @@ abstract class ilMathBaseAdapterTest extends TestCase
/**
* @dataProvider subData
*/
public function testSub(string $a, string $b, string $result, int $scale)
public function testSub(string $a, string $b, string $result, int $scale) : void
{
$this->assertEqualNumbers($result, $this->mathAdapter->sub($a, $b, $scale));
}
......@@ -78,7 +75,7 @@ abstract class ilMathBaseAdapterTest extends TestCase
/**
* @dataProvider mulData
*/
public function testMul(string $a, string $b, string $result, int $scale)
public function testMul(string $a, string $b, string $result, int $scale) : void
{
$this->assertEqualNumbers($result, $this->mathAdapter->mul($a, $b, $scale));
}
......@@ -86,7 +83,7 @@ abstract class ilMathBaseAdapterTest extends TestCase
/**
* @dataProvider divData
*/
public function testDiv(string $a, string $b, string $result, int $scale)
public function testDiv(string $a, string $b, string $result, int $scale) : void
{
$this->assertEqualNumbers($result, $this->mathAdapter->div($a, $b, $scale));
}
......@@ -94,7 +91,7 @@ abstract class ilMathBaseAdapterTest extends TestCase
/**
* @dataProvider sqrtData
*/
public function testSqrt(string $a, string $result, ?int $scale)
public function testSqrt(string $a, string $result, ?int $scale) : void
{
$this->assertEqualNumbers($result, $this->mathAdapter->sqrt($a, $scale));
}
......@@ -102,7 +99,7 @@ abstract class ilMathBaseAdapterTest extends TestCase
/**
* @dataProvider powData
*/
public function testPow(string $a, string $b, string $result, ?int $scale)
public function testPow(string $a, string $b, string $result, ?int $scale) : void
{
$this->assertEqualNumbers($result, $this->mathAdapter->pow($a, $b, $scale));
}
......@@ -111,7 +108,7 @@ abstract class ilMathBaseAdapterTest extends TestCase
* @dataProvider modData
* @throws ilMathDivisionByZeroException
*/
public function testMod(string $a, string $b, string $result)
public function testMod(string $a, string $b, string $result) : void
{
$this->assertEqualNumbers($result, $this->mathAdapter->mod($a, $b));
}
......@@ -119,7 +116,7 @@ abstract class ilMathBaseAdapterTest extends TestCase
/**
* @dataProvider equalsData
*/
public function testEquals(string $a, string $b, bool $result, ?int $scale)
public function testEquals(string $a, string $b, bool $result, ?int $scale) : void
{
$this->assertEqualNumbers($result, $this->mathAdapter->equals($a, $b, $scale));
}
......@@ -127,7 +124,7 @@ abstract class ilMathBaseAdapterTest extends TestCase
/**
* @dataProvider calcData
*/
public function testCalculation(string $formula, string $result, int $scale)
public function testCalculation(string $formula, string $result, int $scale) : void
{
$this->assertEqualNumbers($result, ilMath::_applyScale($this->evalMath->evaluate($formula), $scale));
}
......@@ -135,7 +132,7 @@ abstract class ilMathBaseAdapterTest extends TestCase
/**
*
*/
public function testDivisionsByZero()
public function testDivisionsByZero() : void
{
$this->expectException(ilMathDivisionByZeroException::class);
......@@ -145,7 +142,7 @@ abstract class ilMathBaseAdapterTest extends TestCase
/**
*
*/
public function testModuloByZero()
public function testModuloByZero() : void
{
$this->expectException(ilMathDivisionByZeroException::class);
......@@ -155,7 +152,7 @@ abstract class ilMathBaseAdapterTest extends TestCase
/**
* @return array
*/
public function addData()
public function addData() : array
{
return [
['1', '2', '3', self::DEFAULT_SCALE]
......@@ -165,7 +162,7 @@ abstract class ilMathBaseAdapterTest extends TestCase
/**
* @return array
*/
public function subData()
public function subData() : array
{
return [
['1', '2', '-1', self::DEFAULT_SCALE]
......@@ -175,7 +172,7 @@ abstract class ilMathBaseAdapterTest extends TestCase
/**
* @return array
*/
public function mulData()
public function mulData() : array
{
return [
'Multiplication with integer operands' => ['1', '2', '2', self::DEFAULT_SCALE],
......@@ -187,7 +184,7 @@ abstract class ilMathBaseAdapterTest extends TestCase
/**
* @return array
*/
public function divData()
public function divData() : array
{
return [
'Division with integer operands' => ['1', '2', '0.5', self::DEFAULT_SCALE],
......@@ -199,7 +196,7 @@ abstract class ilMathBaseAdapterTest extends TestCase
/**
* @return array
*/
public function modData()
public function modData() : array
{
return [
['1', '2', '1']
......@@ -209,7 +206,7 @@ abstract class ilMathBaseAdapterTest extends TestCase
/**
* @return array
*/
public function sqrtData()
public function sqrtData() : array
{
return [
['9', '3', self::DEFAULT_SCALE],
......@@ -222,7 +219,7 @@ abstract class ilMathBaseAdapterTest extends TestCase
/**
* @return array
*/
public function powData()
public function powData() : array
{
return [
['3', '2', '9', self::DEFAULT_SCALE]
......@@ -232,7 +229,7 @@ abstract class ilMathBaseAdapterTest extends TestCase
/**
* @return array
*/
public function equalsData()
public function equalsData() : array
{
return [
['3', '3', true, null],
......@@ -243,7 +240,7 @@ abstract class ilMathBaseAdapterTest extends TestCase
/**
*
*/
public function calcData()
public function calcData() : array
{
return [
['3+5', '8', self::DEFAULT_SCALE],
......
<?php
require_once 'Services/Math/test/ilMathBaseAdapterTest.php';
/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/
*********************************************************************/
require_once 'Services/Math/test/ilMathBaseAdapterTest.php';
class ilMathPhpAdapterTest extends ilMathBaseAdapterTest
{
/**
......
<?php
use PHPUnit\Framework\TestCase;
/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/
*********************************************************************/
use PHPUnit\Framework\TestCase;
/**
* @author Michael Jansen <mjansen@databay.de>
* @version $Id$
* @author Michael Jansen <mjansen@databay.de>
*/
class ilMathTest extends TestCase
{
/**
* @var EvalMath
*/
protected $eval_math;
protected EvalMath $eval_math;
/**
*
* @inheritDoc
*/
protected function setUp() : void
{
......
<?php
use PHPUnit\Framework\TestSuite;
<?php declare(strict_types=1);
/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/
*********************************************************************/
use PHPUnit\Framework\TestSuite;
/**
* @author Michael Jansen <mjansen@databay.de>
* @version $Id$
* @author Michael Jansen <mjansen@databay.de>
*/
class ilServicesMathSuite extends TestSuite
{
......