PHP code example of tienvx / assignments-evaluator
1. Go to this page and download the library: Download tienvx/assignments-evaluator library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tienvx / assignments-evaluator example snippets
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Tienvx\AssignmentsEvaluator\AssignmentsEvaluator;
class Robot
{
public function sayHi(string $name): string
{
return sprintf('Hi %s!', $name);
}
}
$assignmentsEvaluator = new AssignmentsEvaluator(new ExpressionLanguage());
var_dump($assignmentsEvaluator->evaluate(
'fullName = firstName~" "~lastName; hello = robot.sayHi(fullName)',
[
'firstName' => 'Madonna',
'lastName' => 'Jenkins',
'robot' => new Robot(),
]
));
/* displays
array(5) {
["firstName"]=>
string(7) "Madonna"
["lastName"]=>
string(7) "Jenkins"
["robot"]=>
object(Robot)#8 (0) {
}
["fullName"]=>
string(15) "Madonna Jenkins"
["hello"]=>
string(19) "Hi Madonna Jenkins!"
}
*/
$assignmentsEvaluator->lint('title = "Dr."; firstName = "zane"; lastName = "stroman"; name = title~ucfirst(firstName)~" "~ucfirst(lastName)');
/* throw
PHP Fatal error: Uncaught Tienvx\AssignmentsEvaluator\SyntaxError: Expression "title~ucfirst(firstName)~" "~ucfirst(lastName)" is invalid: The function "ucfirst" does not exist around position 7 for expression `title~ucfirst(firstName)~" "~ucfirst(lastName)`..
*/