use Ruler\RuleBuilder;
/**
* Class InsertRulesObjectDB
*
* @category Treatment_Standard_Data
* @package WS\RESTBundle\Services\jsonBuilder
* @author deploiement <
[email protected]>
* @license copyright SellSecure
* @version Release: <2.0>
* @link www.sellsecure.com
*/
class InsertRulesObjectDB
{
private $_container;
private $_connexion;
private $_tableTransaction;
private $_arrayDecisionRuler;
private $_arrayRulesScoreRuler;
/**
* Method constructor
*
* @param connexion $connexion :connection to database
* @param Container $container :container object
*/
public function __construct($connexion, Container $container)
{
$this->_container = $container;
$this->_connexion = $connexion;
$this->_tableTransaction
= $this->_container->getParameter('ISB_TRANSACTIONS');
}
/**
* Insert rules object into DB
*
* @return void
*/
public function insertObjectDB()
{
$rules = $this->getRulesFromDB();
$this->rulesTreatment($rules);
$this->insertRuleObjectDB($this->_arrayDecisionRuler);
$this->insertRuleObjectDB($this->_arrayRulesScoreRuler);
}
/**
* Get all rules from the DB
*
* @return Array
*/
public function getRulesFromDB()
{
$sql = 'Select *
FROM ' . $this->_tableTransaction . '.REGLES
WHERE version = :version
AND actif = :actif';
$queryFlux = $this->_connexion->prepare($sql);
$queryFlux->bindValue("version", 0);
$queryFlux->bindValue("actif", 1);
$queryFlux->execute();
$rules = $queryFlux->fetchAll();
return $rules;
}
/**
* Separate the rules in 2 categories, for simple rules & decision
*
* @param Array $rules :the Array of the rules
*
* @return void
*/
public function rulesTreatment($rules)
{
$arrayJsonRuler = array();
$arrayDecisionRuler = array();
foreach ($rules as $rule) {
if ($rule['UTILISATION_SCORE'] == 1) {
$arrayJsonRuler[]
= $this->returnArrayRuler(
$rule['JSON'],
$rule['EMPLACEMENT'],
$rule['NOM'],
$rule['ID'],
$rule['MOTIF'],
$rule['SCORE']
);
} elseif ($rule['UTILISATION_SCORE'] == 2) {
$arrayDecisionRuler[]
= $this->returnArrayRuler(
$rule['JSON'],
$rule['EMPLACEMENT'],
$rule['NOM'],
$rule['ID'],
$rule['MOTIF'],
$rule['SCORE']
);
}
}
$this->_arrayRulesScoreRuler = $arrayJsonRuler;
$this->_arrayDecisionRuler = $arrayDecisionRuler;
}
/**
* Insert the object of rules into DB
*
* @param Array $rules :the json of rules to insert the object
*
* @return void;
*/
public function insertRuleObjectDB($rules)
{
$jsonToRuleBuilder
= $this->_container->get('JsonToRulerBuilder');
foreach ($rules as $rule) {
$ruleBuilder = new RuleBuilder();
$parameters
= $jsonToRuleBuilder->jsonToRuleBuilder(
$rule,
$ruleBuilder
);
$serializeRules = serialize($rule);
$encodeRules = base64_encode($serializeRules);
$serializeParameters = serialize($parameters);
$encodeParameters = (base64_encode($serializeParameters));
$sql = "DECLARE
vClobVal varchar2(32767) := '$encodeParameters';
vClobVal1 varchar2(32767) := '$encodeRules';
begin ";
$sql .= "UPDATE "
. $this->_tableTransaction
. ".REGLES SET PARAMETERS = vClobVal,
RULES = vClobVal1 WHERE ID = '"
. utf8_decode($rule[0]['id']) . "'; end;";
$query = $this->_connexion->prepare($sql);
$query->execute();
}
}
/**
* Convert the rules into an array
*
* @param string $json_ruler :the rule to convert
* @param string $emplacement :the emplacement of the rule
* @param string $name :the name of the rule
* @param string $id :the name of the rule
* @param string $motif :the motif of the rule
* @param string $score :the score of the rule
*
* @return Array
*/
public function returnArrayRuler(
$json_ruler,
$emplacement = '',
$name = '',
$id = '',
$motif = '',
$score = ''
) {
$jsonInfosRule
= ',"emplacement":"'
. $emplacement
. '","nom":"'
. $name
. '","id":"'
. $id
. '","motif":"'
. $motif
. '","score":"'
. $score . '"}]';
$jsonRuler = substr($json_ruler, 0, -2) . $jsonInfosRule;
$objectRuler = json_decode($jsonRuler);
$arrayRuler = json_decode(json_encode($objectRuler), true);
return $arrayRuler;
}
}
Plus de 450 lignes de codes 15 lignes de code Le code : public function rulesTreatment($rules, $context, $isbref)
{
foreach ($rules as $expression) {
try {
$return
= $this->expressionLanguage->evaluate(
$expression,
$context
); if ($return === true) { //On déclenche la règle }
} catch (SyntaxError $e) {
}
}
}