Skip to content

Commit

Permalink
Add rmna function
Browse files Browse the repository at this point in the history
  • Loading branch information
neveldo committed Mar 30, 2017
1 parent c6bcec2 commit 471d8f1
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down
4 changes: 2 additions & 2 deletions src/TextGenerator/TextFunction/FilterFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand Down
47 changes: 47 additions & 0 deletions src/TextGenerator/TextFunction/RmnaFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Neveldo\TextGenerator\TextFunction;

use Neveldo\TextGenerator\Tag\TagReplacerInterface;

/**
* Class c
* 'rmna' function : return the argument only if it does not contain any empty values
* Examples :
* #rmna{one @possible_not_available_tag two}
*
* @package Neveldo\TextGenerator\TextFunction
*/
class RmnaFunction implements FunctionInterface
{
/**
* @var TagReplacerInterface Tag Replacer service
*/
private $tagReplacer;

/**
* RandomFunction constructor.
* @param TagReplacerInterface $tagReplacer
*/
public function __construct(TagReplacerInterface $tagReplacer)
{
$this->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];
}

}
2 changes: 2 additions & 0 deletions src/TextGenerator/TextGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
;
}

Expand Down

0 comments on commit 471d8f1

Please sign in to comment.