2019-11-27 06:25:33 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of RaspiSMS.
|
|
|
|
*
|
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the GPL-3.0 license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace controllers\internals;
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-27 06:25:33 +01:00
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
|
|
|
|
|
2020-07-03 03:48:42 +02:00
|
|
|
/**
|
|
|
|
* Class to analyse rules used by conditional groups.
|
|
|
|
*/
|
2019-11-27 06:25:33 +01:00
|
|
|
class Ruler extends \descartes\InternalController
|
|
|
|
{
|
|
|
|
private $expression_language;
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Constructor.
|
2019-11-27 06:25:33 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function __construct()
|
2019-11-27 06:25:33 +01:00
|
|
|
{
|
|
|
|
$this->expression_language = new ExpressionLanguage();
|
|
|
|
|
|
|
|
//Add custom functions
|
|
|
|
$this->expression_language->registerProvider(new ExpressionProvider());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify if a condition is valid. i.e we can evaluate it without error.
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
|
|
|
* @param string $condition : The condition to evaluate
|
2021-02-23 00:31:54 +01:00
|
|
|
* @param array $data : The data to made available to condition
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-27 06:25:33 +01:00
|
|
|
* @return bool : false if invalid, true else
|
|
|
|
*/
|
2021-01-17 03:16:57 +01:00
|
|
|
public function validate_condition(string $condition, array $data = []): bool
|
2019-11-27 06:25:33 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
try
|
2019-11-27 06:25:33 +01:00
|
|
|
{
|
2021-01-17 03:16:57 +01:00
|
|
|
$this->expression_language->parse($condition, array_keys($data));
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-27 06:25:33 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
|
|
|
return false;
|
2020-01-17 18:19:25 +01:00
|
|
|
}
|
2020-04-04 02:51:51 +02:00
|
|
|
catch (\Throwable $t)
|
|
|
|
{
|
|
|
|
//Just ignore non critical php warning and notice
|
|
|
|
}
|
2019-11-27 06:25:33 +01:00
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-27 06:25:33 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Evaluate a condition.
|
|
|
|
*
|
|
|
|
* @param string $condition : The condition to evaluate
|
2021-02-23 00:31:54 +01:00
|
|
|
* @param array $data : The data to made available to condition
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-27 06:25:33 +01:00
|
|
|
* @return ?bool : false if invalid, true else, null only on error
|
|
|
|
*/
|
2021-01-17 03:16:57 +01:00
|
|
|
public function evaluate_condition(string $condition, array $data = []): ?bool
|
2019-11-27 06:25:33 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
try
|
2019-11-27 06:25:33 +01:00
|
|
|
{
|
2021-11-26 19:40:26 +01:00
|
|
|
@$result = $this->expression_language->evaluate($condition, $data);
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-27 06:25:33 +01:00
|
|
|
return (bool) $result;
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
|
|
|
return null;
|
2020-01-17 18:19:25 +01:00
|
|
|
}
|
2019-11-27 06:25:33 +01:00
|
|
|
}
|
|
|
|
}
|