Skip to content

Commit

Permalink
Prep for V2.2.0 release (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
phpfui authored Apr 26, 2022
1 parent ef82911 commit c396a88
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Conditional If logic
* Support for user defined operators
* Support for user defined functions
* Support for math on user defined objects
* Dynamic variable resolution (delayed computation)
* Unlimited variable name lengths
* String support, as function parameters or as evaluated as a number by PHP
Expand Down Expand Up @@ -138,6 +139,21 @@ $executor->setVar('var1', 0.15)->setVar('var2', 0.22);
echo $executor->execute("$var1 + var2");
```

By default, variables must be scalar values (int, float, bool or string). If you would like to support another type, use **setVarValidationHandler**

```php
$executor->setVarValidationHandler(function (string $name, $variable) {
// allow all scalars and null
if (is_scalar($variable) || $variable === null) {
return;
}
// Allow variables of type DateTime, but not others
if (! $variable instanceof \DateTime) {
throw new MathExecutorException("Invalid variable type");
}
});
```

You can dynamically define variables at run time. If a variable has a high computation cost, but might not be used, then you can define an undefined variable handler. It will only get called when the variable is used, rather than having to always set it initially.

```php
Expand Down
46 changes: 41 additions & 5 deletions tests/MathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,9 @@ public function testSetVarsAcceptsAllScalars()
$this->assertEquals(null, $calculator->getVar('null'));
$this->assertEquals(1.1, $calculator->getVar('float'));
$this->assertEquals('string', $calculator->getVar('string'));

$this->expectException(MathExecutorException::class);
$calculator->setVar('validVar', new \DateTime());
}

public function testSetVarsDoesNotAcceptObject()
Expand All @@ -592,16 +595,49 @@ public function testSetVarsDoesNotAcceptResource()
public function testSetCustomVarValidator()
{
$calculator = new MathExecutor();
$calculator->setVarValidationHandler(function ($name, $variable) {
if ($name === 'invalidVar' && $variable === 'invalid') {
throw new MathExecutorException("Invalid variable");
$calculator->setVarValidationHandler(function (string $name, $variable) {
// allow all scalars and null
if (is_scalar($variable) || $variable === null) {
return;
}
// Allow variables of type DateTime, but not others
if (! $variable instanceof \DateTime) {
throw new MathExecutorException("Invalid variable type");
}
});

$calculator->setVar('validFloat', 0.0);
$calculator->setVar('validInt', 0);
$calculator->setVar('validTrue', true);
$calculator->setVar('validFalse', false);
$calculator->setVar('validString', 'string');
$calculator->setVar('validNull', null);
$calculator->setVar('validDateTime', new \DateTime());

$this->expectException(MathExecutorException::class);
$calculator->setVar('validVar', $this);
}

public function testSetCustomVarNameValidator()
{
$calculator = new MathExecutor();
$calculator->setVarValidationHandler(function (string $name, $variable) {
// don't allow variable names with the word invalid in them
if (str_contains($name, 'invalid')) {
throw new MathExecutorException("Invalid variable name");
}
});

$calculator->setVar('valid', $this);
$calculator->setVar('validFloat', 0.0);
$calculator->setVar('validInt', 0);
$calculator->setVar('validTrue', true);
$calculator->setVar('validFalse', false);
$calculator->setVar('validString', 'string');
$calculator->setVar('validNull', null);
$calculator->setVar('validDateTime', new \DateTime());

$this->expectException(MathExecutorException::class);
$calculator->setVar('invalidVar', 'invalid');
$calculator->setVar('invalidVar', 12);
}

public function testVarExists()
Expand Down

0 comments on commit c396a88

Please sign in to comment.