mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-04-20 16:37:48 +02:00
Add first valid expression ruler. Still not linked to scheduleds.
This commit is contained in:
parent
f59f7bd757
commit
f4bbfa0152
16 changed files with 1049 additions and 18 deletions
|
@ -41,7 +41,9 @@ namespace controllers\internals;
|
|||
'condition' => $condition,
|
||||
];
|
||||
|
||||
if (!$this->validate_condition($condition))
|
||||
$internal_ruler = new Ruler();
|
||||
$valid_condition = $internal_ruler->validate_condition($condition, ['contact' => (object) ['datas' => (object) null]]);
|
||||
if (!$valid_condition)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -73,8 +75,10 @@ namespace controllers\internals;
|
|||
'name' => $name,
|
||||
'condition' => $condition,
|
||||
];
|
||||
|
||||
if (!$this->validate_condition($condition))
|
||||
|
||||
$internal_ruler = new Ruler();
|
||||
$valid_condition = $internal_ruler->validate_condition($condition, ['contact' => (object) ['datas' => (object) null]]);
|
||||
if (!$valid_condition)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -102,11 +106,39 @@ namespace controllers\internals;
|
|||
|
||||
|
||||
/**
|
||||
* Verify if a condition string is valid (i.e we can parse it without error)
|
||||
* Gets the user's contacts that respects a condition
|
||||
* @param int $id_user : User id
|
||||
* @param string $condition : Condition string to verify
|
||||
* @return bool
|
||||
*/
|
||||
public function validate_condition (string $condition) : bool
|
||||
public function get_contacts_for_condition_and_user (int $id_user, string $condition) : bool
|
||||
{
|
||||
$internal_contacts = new Contacts($this->bdd);
|
||||
$contacts = $internal_contacts->gets_for_user($id_user);
|
||||
|
||||
$ruler = new Ruler();
|
||||
|
||||
foreach ($contacts as $key => $contact)
|
||||
{
|
||||
if ($contact['datas'] != null)
|
||||
{
|
||||
$contact['datas'] = json_decode($contact['datas']);
|
||||
}
|
||||
else
|
||||
{
|
||||
$contact['datas'] = new \stdClass();
|
||||
}
|
||||
|
||||
$contact = (object) $contact;
|
||||
|
||||
$datas = ['contact' => $contact];
|
||||
$is_valid = $ruler->evaluate_condition($condition, $datas);
|
||||
if (!$is_valid)
|
||||
{
|
||||
unset($contacts[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
return $contacts;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -68,7 +68,7 @@ namespace controllers\internals;
|
|||
* @param string $datas : Contact datas
|
||||
* @return mixed bool|int : False if cannot create contact, id of the new contact else
|
||||
*/
|
||||
public function create($id_user, $number, $name, $datas)
|
||||
public function create($id_user, $number, $name, ?string $datas = null)
|
||||
{
|
||||
$contact = [
|
||||
'id_user' => $id_user,
|
||||
|
|
21
controllers/internals/ExpressionProvider.php
Normal file
21
controllers/internals/ExpressionProvider.php
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace controllers\internals;
|
||||
|
||||
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
|
||||
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
|
||||
|
||||
class ExpressionProvider implements ExpressionFunctionProviderInterface
|
||||
{
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
ExpressionFunction::fromPhp('is_null', 'exists'),
|
||||
ExpressionFunction::fromPhp('mb_strtolower', 'lower'),
|
||||
ExpressionFunction::fromPhp('mb_strtoupper', 'upper'),
|
||||
ExpressionFunction::fromPhp('mb_substr', 'substr'),
|
||||
ExpressionFunction::fromPhp('abs', 'abs'),
|
||||
ExpressionFunction::fromPhp('strtotime', 'date'),
|
||||
];
|
||||
}
|
||||
}
|
75
controllers/internals/Ruler.php
Executable file
75
controllers/internals/Ruler.php
Executable file
|
@ -0,0 +1,75 @@
|
|||
<?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;
|
||||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
|
||||
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
|
||||
|
||||
/**
|
||||
* Class to analyse rules used by conditional groups
|
||||
*/
|
||||
class Ruler extends \descartes\InternalController
|
||||
{
|
||||
private $expression_language;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct ()
|
||||
{
|
||||
$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.
|
||||
* @param string $condition : The condition to evaluate.
|
||||
* @param array $datas : The datas to made available to condition
|
||||
* @return bool : false if invalid, true else
|
||||
*/
|
||||
public function validate_condition (string $condition, array $datas = []) : bool
|
||||
{
|
||||
try
|
||||
{
|
||||
$this->expression_language->evaluate($condition, $datas);
|
||||
return true;
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
echo "Error : ";
|
||||
echo $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Evaluate a condition
|
||||
* @param string $condition : The condition to evaluate.
|
||||
* @param array $datas : The datas to made available to condition
|
||||
* @return ?bool : false if invalid, true else, null only on error
|
||||
*/
|
||||
public function evaluate_condition (string $condition, array $datas = []) : ?bool
|
||||
{
|
||||
try
|
||||
{
|
||||
$result = $this->expression_language->evaluate($condition, $datas);
|
||||
return (bool) $result;
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue