From 9bb8fb33ba55a759af24bee4a3ee44deefb0ef3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come@opensides.be> Date: Wed, 7 Nov 2018 17:05:12 +0100 Subject: [PATCH] :sparkles: feat(template) Adds a number modifier This allows to use %n|% to add a number if needed for unicity. Args are mandatory, start, step. Default values for these are 0, 1, 1. issue #5882 --- include/class_templateHandling.inc | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/include/class_templateHandling.inc b/include/class_templateHandling.inc index d1072a28b..4b2084d3e 100644 --- a/include/class_templateHandling.inc +++ b/include/class_templateHandling.inc @@ -431,6 +431,28 @@ class templateHandling return array($dateObject->format($args[1])); } + private static function modifierNumber(array $args) + { + if (count($args) < 1) { + $args[] = FALSE; + } + if (count($args) < 2) { + $args[] = 1; + } + if (count($args) < 3) { + $args[] = 1; + } + $numberGenerator = function ($mandatory, $start, $step) { + $i = $start; + while (TRUE) { + yield $i; + $i += $step; + } + }; + + return $numberGenerator($args[0], $args[1], $args[2]); + } + /*! \brief Apply a modifier * * \param string $m the modifier @@ -533,6 +555,10 @@ class templateHandling // date $result = array(static::modifierDate($args)); break; + case 'n': + // number + $result = static::modifierNumber($args); + break; default: trigger_error("Unkown modifier '$m'"); $result = array($str); -- GitLab