AlisQI's expression engine allows users to configure
calculated form fields. For example, the following expression calculates the sum of two fields' values: a + b
.
The expression engine is powered by Twig. AlisQI wraps the user's expression in a barebones Twig template, which is then rendered with the form field values provided as context:
$twig->render(
'{{ a + b }}',
['a' => 1000, 'b' => 337]
);
Consider what happens if the form field for b
has no value (i.e., the user left the form field empty).
Internally, this leads to $b = null
(not $b = ''
).
Twig will compile the template {{ a + b }}
to PHP code which boils down to
return $a + $b;
However, $a + null === $a
for any int|float $a
, since null
is cast to 0
:
var_export(1337 + null); // outputs '1337'
var_export(+null); // outputs '0'
Your assignment is to change the Twig sandbox in such a way a + b
evaluates to an empty string if either a
or b
is empty (null
).
The script calculator.php
runs a suite of tests and outputs the failing ones (see "How to run" below).
Therefore, it should produce no output with a valid solution in place.
May the force be with you!
For bonus points, make the sandbox do the same trick for subtraction, multiplication and division.
You may use any PHP runtime you want and run php calculator.php
. Don't forget to composer install
first.
Alternatively, you can use the Lando config (in .lando.yml
)
to spin up a PHP 8.2 container that's ready to go.
lando start # to start the container
lando calculate # to run `php calculator.php` inside the container
Don't publish your code to a public repo and/or send a pull request. Your solution needs to remain private!
You can either send us the code over email or invite this repo's owner to your cloned repo.