Skip to content
Snippets Groups Projects
Commit da319d72 authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

hotline #83526 : fix RT

parent 9300eac3
Branches
Tags
4 merge requests!3297WIP: Master,!2936Master,!2935Hotline,!2931hotline #83526 : better Nanook standard handling
Pipeline #5669 passed with stage
in 31 minutes and 38 seconds
<?php
/**
* Copyright (c) 2012-2018, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_Codification_Rule {
const VALUE_SEPARATOR = ';';
protected
$_zone,
$_operator,
$_values = [];
public static function newFrom($rule) {
$zone = substr($rule, 0, 5);
$operator = substr($rule, 5, 1);
$values = array_filter(array_map(
function($value) {
return trim(str_replace(' ', '', $value));
},
explode(static::VALUE_SEPARATOR, substr($rule, 6))));
return $values
? new static($zone, $operator, $values)
: null;
}
public function __construct($zone, $operator, $values) {
$this->_zone = $zone;
$this->_operator = $operator;
$this->_values = $values;
}
public function isSameFieldAndOperator($other) {
return ($other
&& $this->_zone === $other->_zone
&& $this->_operator === $other->_operator);
}
public function isAlreadyHandledByOtherRule($other) {
return $this->isSameFieldAndOperator($other)
&& !array_diff($this->_values, $other->_values);
}
public function isZone($zone) {
return $this->_zone === $zone;
}
public function addValuesOf($other) {
foreach($other->_values as $value)
if (!in_array($value, $this->_values))
$this->_values[] = $value;
return $this;
}
public function eachValueDo($closure) {
foreach($this->_values as $value)
$closure($value);
return $this;
}
public function asString() {
return $this->_zone
. $this->_operator
. implode(static::VALUE_SEPARATOR, array_unique($this->_values));
}
}
\ No newline at end of file
......@@ -23,6 +23,19 @@
class Class_Codification_Rules {
protected $_rules;
public static function newFromString($rules) {
$model = (new Class_Entity(['Regles' => $rules]))
->whenCalledDo(
'hasRegles',
function() use($rules) {
return $rules;
});
return new static($model);
}
protected static function _rulesFrom($model) {
if (!$model || !$model->hasRegles())
return new Storm_Collection([]);
......@@ -47,7 +60,7 @@ class Class_Codification_Rules {
$existing = $this->_rules
->detect(
function($other) use($rule) {
return $rule->isSameAs($other);
return $rule->isSameFieldAndOperator($other);
});
$existing
......@@ -58,15 +71,24 @@ class Class_Codification_Rules {
}
public function match($rule_string) {
if (!$rule = Class_Codification_Rule::newFrom($rule_string))
public function alreadyExistsIn($other_rules) {
if (!$other_rules || $other_rules->isEmpty())
return false;
return $this->_rules
->detect(
function($other) use($rule) {
return $rule->match($other);
});
foreach($this->_rules as $rule)
if (!$this->_ruleIsAlreadyHandledByOtherRules($rule, $other_rules))
return false;
return true;
}
protected function _ruleIsAlreadyHandledByOtherRules($rule, $other_rules) {
foreach($other_rules->_rules as $other)
if ($rule->isAlreadyHandledByOtherRule($other))
return true;
return false;
}
......@@ -76,8 +98,13 @@ class Class_Codification_Rules {
}
public function isEmpty() {
return $this->_rules->isEmpty();
}
public function asString() {
return $this->_rules->isEmpty()
return $this->isEmpty()
? ''
: implode("\n", $this->_rulesAsString()->getArrayCopy());
}
......@@ -87,78 +114,4 @@ class Class_Codification_Rules {
return $this->_rules
->collect(function($rule) { return $rule->asString(); });
}
}
class Class_Codification_Rule {
const VALUE_SEPARATOR = ';';
protected
$_zone,
$_operator,
$_values = [];
public static function newFrom($rule) {
$zone = substr($rule, 0, 5);
$operator = substr($rule, 5, 1);
$values = array_filter(array_map(
function($value) {
return trim(str_replace(' ', '', $value));
},
explode(static::VALUE_SEPARATOR, substr($rule, 6))));
return $values
? new static($zone, $operator, $values)
: null;
}
public function __construct($zone, $operator, $values) {
$this->_zone = $zone;
$this->_operator = $operator;
$this->_values = $values;
}
public function isSameAs($other) {
return ($other
&& $this->_zone === $other->_zone
&& $this->_operator === $other->_operator);
}
public function match($other) {
return $this->isSameAs($other) && !array_diff($this->_values, $other->_values);
}
public function isZone($zone) {
return $this->_zone === $zone;
}
public function addValuesOf($other) {
foreach($other->_values as $value)
if (!in_array($value, $this->_values))
$this->_values[] = $value;
return $this;
}
public function eachValueDo($closure) {
foreach($this->_values as $value)
$closure($value);
return $this;
}
public function asString() {
return $this->_zone
. $this->_operator
. implode(static::VALUE_SEPARATOR, array_unique($this->_values));
}
}
\ No newline at end of file
......@@ -108,9 +108,13 @@ abstract class Class_Cosmogramme_Generator_AbstractTask {
if ($model = $model_class::findFirstBy(['libelle' => $label]))
return $model;
foreach($model_class::findAll() as $model)
if ((new Class_Codification_Rules($model))->match($this->_getRuleFor($code)))
foreach($model_class::findAll() as $model) {
if (!$rules = Class_Codification_Rules::newFromString($this->_getRuleFor($code)))
continue;
if ($rules->alreadyExistsIn(new Class_Codification_Rules($model)))
return $model->setLibelle($label);
}
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment