Просто ухожу от @deceze ответа и предложения использовать компонент Symfony ExpressionLanguage.
Я установил его в свой проект через Composer и подумал, что для всех, кто наткнулся на пост, было бы полезно увидеть, как оно работает (и в связи с моим вопросом):
<code># build array for testing rows against rules
$test = [];
# foreach csv row
foreach ($csv as $keey => $row)
{
# 10000s of rows, just for simplicity - break after 3
if ($keey == 0) {continue;}
if ($keey >= 3) {continue;}
# get array keys for
$keys = array_keys($row);
foreach ($keys as $key)
{
# if row key is in the $conditions array, add to $test array for testing
if (in_array($key, array_map('strtolower', array_keys($conditions)))) {
$conditionType = array_keys($conditions[$key]);
$conditionType = $conditionType[0];
if ($conditionType === 'condition_suffix') {
$brokenCondition = explode(' ', $conditions[$key][$conditionType]);
# build array to pass into ->evaluate()
$test[$key]['evaluate'] = 'field '. $brokenCondition[0] .' required'; # expression to actually test
$test[$key]['pass'] = [ # works like pdo, pass in the names and give them a value
'field' => strlen($row[$key]),
'required' => $brokenCondition[1]
];
} else {
$test[$key]['evaluate'] = 'field == required';
$test[$key]['pass'] = [
'field' => is_numeric($row[$key]),
'required' => true
];
}
}
}
}
echo '#----------------------------------------------------------------------------#';
# show test arr for reference
echo '<pre>';
print_r($test);
echo '
';
# foreach тестовый ряд, проверка на соответствие условию
foreach ($ test as $ key => $ item)
{
echo '
';
var_dump($key. ': ' .$expressionLanguage->evaluate(
$item['evaluate'],
$item['pass']
));
echo '
';
echo '+ ----------------------------------------------- ----------------------------- + ';
}
Это теперь оценивает мои собственные строки php-запроса через компонент ExpressionLanguage Symfony. Спасибо @ deceze
рефов:
https://symfony.com/doc/current/components/expression_language/syntax.html
https://symfony.com/doc/current/components/expression_language.html