diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cd5347..a62fdb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # ChangeLog Change log for TextGenerator +## 1.1.0 - March 31, 2017 +- Add rmna function +- Prevent filters to be called on empty values +- Prevent 'number' filter to be called on non-numeric values + ## 1.0.2 - March 14, 2017 - Add u modifier to preg_replace_callback() call diff --git a/README.md b/README.md index 07b85f1..df4da9c 100644 --- a/README.md +++ b/README.md @@ -210,6 +210,28 @@ Output example : Hello +### 'rmna' + +Return the argument only if it does not contain any empty values. It allows to prevent the display of sentences with missing values. + +Data : + + [ + [ + 'tag1' => '', // or null + 'tag2' => 'ok', + ] + ] + +Template example : + + #rmna{test 1 : @tag1};; + #rmna{test 2 : @tag2} + +Output : + + test 2 : ok + ## Complete example : Template : diff --git a/src/TextGenerator/TextFunction/FilterFunction.php b/src/TextGenerator/TextFunction/FilterFunction.php index 87fc87d..84a9f16 100644 --- a/src/TextGenerator/TextFunction/FilterFunction.php +++ b/src/TextGenerator/TextFunction/FilterFunction.php @@ -174,8 +174,8 @@ public function execute(array $arguments, array $originalArguments) ); } - if ($arguments[0] === $this->tagReplacer->getEmptyTag()) { - return ''; + if ($arguments[0] === $this->tagReplacer->getEmptyTag() || $arguments[0] === '') { + return $arguments[0]; } if (isset($filter['maxArgs']) && count($arguments) > $filter['maxArgs']) { diff --git a/src/TextGenerator/TextFunction/RmnaFunction.php b/src/TextGenerator/TextFunction/RmnaFunction.php new file mode 100644 index 0000000..b6400b8 --- /dev/null +++ b/src/TextGenerator/TextFunction/RmnaFunction.php @@ -0,0 +1,47 @@ +tagReplacer = $tagReplacer; + } + + /** + * Handle rmna function + * @param array $arguments list of arguments where tags have been replaced by their values + * @param array $originalArguments list of original arguments + */ + public function execute(array $arguments, array $originalArguments) + { + if (count($arguments) === 0 + || mb_strpos($arguments[0], $this->tagReplacer->getEmptyTag()) !== false + ) { + return ''; + } + + return $arguments[0]; + } + +} \ No newline at end of file diff --git a/src/TextGenerator/TextGenerator.php b/src/TextGenerator/TextGenerator.php index bac5213..576bf7a 100644 --- a/src/TextGenerator/TextGenerator.php +++ b/src/TextGenerator/TextGenerator.php @@ -11,6 +11,7 @@ use Neveldo\TextGenerator\TextFunction\LoopFunction; use Neveldo\TextGenerator\TextFunction\ProbabilityRandomFunction; use Neveldo\TextGenerator\TextFunction\RandomFunction; +use Neveldo\TextGenerator\TextFunction\RmnaFunction; use Neveldo\TextGenerator\TextFunction\SetFunction; use Neveldo\TextGenerator\TextFunction\ShuffleFunction; use Neveldo\TextGenerator\Tag\TagReplacer; @@ -83,6 +84,7 @@ public function __construct(TagReplacerInterface $tagReplacer = null) ->registerFunction('expr', new ExprFunction($this->tagReplacer)) ->registerFunction('filter', new FilterFunction($this->tagReplacer)) ->registerFunction('coalesce', new CoalesceFunction($this->tagReplacer)) + ->registerFunction('rmna', new RmnaFunction($this->tagReplacer)) ; }