Description
Symfony version(s) affected
^6.0
Description
I'm using Expression Language Package with Laravel Framework
and I'm sending the rules object and data object to the package as mentioned in the documentation.
The issue is if the rules object checks property which does not exist in the data object the package displays an error.
"message": "Undefined property: App\\RulesEngine\\Conditions\\DataConditions::$scores", "exception": "ErrorException",
The idea is that I'm receiving the data object from an API and the object has different properties which not of it is required but I need to evaluate the properties in case of its existence.
I checked the package's code and I found that the code is calling the property directly without checking the existence of the property.
As example,
Rules object =>
data.scores = 20 || data.name = "abeer"
data object
data['name'] = "abeer";
and error will appear " Undefined property data.scores"
How to reproduce
My rules engine class
`
class RulesEngine implements RulesEngineInterface
{
/** @var array<string> */
private array $rules = [];
public function __construct(public ExpressionLanguage $expressionLanguage)
{
}
/**
* @inheritdoc
*/
public function addRules(array $expressions, bool $reset = false): void
{
$this->rules = $reset ? [...$expressions] : [...$this->rules, ...$expressions];
}
/**
* @inheritdoc
*/
public function validateData(array $data): mixed
{
foreach ($this->rules as $rule) {
$evaluated = $this->expressionLanguage->evaluate($rule, $data);
if (! $evaluated) {
return false;
}
}
return true;
}
}
`
Calling the class to handle rules evaluation
`
public function evaluateRules(RulesEngineInterface $rulesEngine, Game $game, array $data): bool
{
$rulesEngine->addRules([$game->conditions], true);
return $rulesEngine->validateData($data);
}
`
Possible Solution
instead of return direct access to the property in the object return line in Evaluate function in call GetAttrNode
check the existence of the property object_get($obj, $property) !== null
and return false to drop only one rules without stoping the code and keep evaluating the rest of the rules.
Additional Context
No response