diff --git a/CHANGELOG.md b/CHANGELOG.md index 8da8c10..c77217d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ CHANGELOG --------- +## NEXT RELEASE + - add a filter to mimic PHP `str_pad`: `bamboo_extensions_strpad`. + ## 8.x-3.3 (2018-05-16) - Fix date diff calcul error - Issue #2966556 diff --git a/README.md b/README.md index 441dd00..9c43373 100644 --- a/README.md +++ b/README.md @@ -389,7 +389,7 @@ has the requested permission. **Extensions** -The `truncate` filter from Twig-extensions [Text](http://twig-extensions.readthedocs.io/en/latest/text.html). +The `bamboo_extensions_truncate` filter from Twig-extensions [Text](http://twig-extensions.readthedocs.io/en/latest/text.html). - `sentence` string - `word` boolean - Truncat at the end of words. @@ -397,7 +397,24 @@ The `truncate` filter from Twig-extensions [Text](http://twig-extensions.readthe ```twig {# Truncate a sentence #} -{{ "This is a very long sentence."|truncate(2, false, '...') }} +{{ "This is a very long sentence."|bamboo_extensions_truncate(2, false, '...') }} +``` + +The `bamboo_extensions_strpad` filter provide a way to pad a string to a certain length with another string. + +- `input` string +- `pad_length` string - If the value of pad_length is negative, less than, + or equal to the length of the input string, no padding takes place. +- `pad_string` (optional) string - The pad_string may be truncated if the + required number of padding characters can't be evenly divided by the length. +- `pad_type` (optional) boolean - can be STR_PAD_RIGHT (1), STR_PAD_LEFT (0), or + STR_PAD_BOTH (2). When not specified it is assumed to be STR_PAD_RIGHT. + +```twig +{# Pad a string #} +{{ "Alien"|bamboo_extensions_strpad(10, ' ') }} +{{ "Alien"|bamboo_extensions_strpad(10, '-=', 0) }} +{{ "Alien"|bamboo_extensions_strpad(10, 'pp', 2) }} ``` The *coming soon* `bamboo_truncate_html` filter to truncates sentences html and preserves tags. @@ -411,22 +428,22 @@ The *coming soon* `bamboo_truncate_html` filter to truncates sentences html and {{ "
This is a very long sentence.
"|bamboo_truncate_html(2, false, '...') }} ``` -The `shuffle` filter from Twig-extensions [Array](http://twig-extensions.readthedocs.io/en/latest/array.html). +The `bamboo_extensions_shuffle` filter from Twig-extensions [Array](http://twig-extensions.readthedocs.io/en/latest/array.html). - `array` array ```twig {# Shuffle the given array #} -[1, 2, 3]|shuffle +[1, 2, 3]|bamboo_extensions_shuffle ``` -The `time_diff` filter from Twig-extensions [Date](http://twig-extensions.readthedocs.io/en/latest/date.html). +The `bamboo_extensions_time_diff` filter from Twig-extensions [Date](http://twig-extensions.readthedocs.io/en/latest/date.html). - `date` string - date, timestamp, DrupalDateTimePlus, DateTimePlus or DateTime ```twig {# Difference between two dates #} -{{ '24-07-2014 17:28:01'|time_diff('24-07-2014 17:28:06') }} +{{ '24-07-2014 17:28:01'|bamboo_extensions_time_diff('24-07-2014 17:28:06') }} ``` **Token** diff --git a/bamboo_twig_extensions/src/TwigExtension/TwigText.php b/bamboo_twig_extensions/src/TwigExtension/TwigText.php index f6d896b..b28c67c 100644 --- a/bamboo_twig_extensions/src/TwigExtension/TwigText.php +++ b/bamboo_twig_extensions/src/TwigExtension/TwigText.php @@ -17,6 +17,7 @@ class TwigText extends \Twig_Extension { public function getFilters() { return [ new \Twig_SimpleFilter('bamboo_extensions_truncate', [$this, 'truncate'], ['needs_environment' => TRUE]), + new \Twig_SimpleFilter('bamboo_extensions_strpad', [$this, 'strpad']), ]; } @@ -63,4 +64,28 @@ public function truncate(TwigEnvironment $env, $string, $length = 30, $preserve return FALSE; } + /** + * Pad a string to a certain length with another string. + * + * @see str_pad() + * + * @param string $input + * The input string. + * @param int $pad_length + * If the value of pad_length is negative, less than, or equal to + * the length of the input string, no padding takes place, + * and input will be returned. + * @param string $pad_string + * The pad_string may be truncated if the required number of padding + * characters can't be evenly divided by the pad_string's length. + * @param string $pad_type + * Optional argument pad_type can be STR_PAD_RIGHT, STR_PAD_LEFT, or + * STR_PAD_BOTH. When not specified it is assumed to be STR_PAD_RIGHT. + * + * @return string + * Returns the padded string. + */ + public function strpad($input, $pad_length, $pad_string = " ", $pad_type = STR_PAD_RIGHT) { + return str_pad($input, $pad_length, $pad_string, $pad_type); + } } diff --git a/tests/src/Functional/BambooTwigExtensionsTest.php b/tests/src/Functional/BambooTwigExtensionsTest.php index ab0006a..f517905 100644 --- a/tests/src/Functional/BambooTwigExtensionsTest.php +++ b/tests/src/Functional/BambooTwigExtensionsTest.php @@ -3,7 +3,7 @@ namespace Drupal\Tests\bamboo_twig\Functional; /** - * Tests Configs twig filters and functions. + * Tests Extensions twig filters and functions. * * @group bamboo_twig * @group bamboo_twig_extensions diff --git a/tests/src/Kernel/BambooTwigExtensionsTest.php b/tests/src/Kernel/BambooTwigExtensionsTest.php new file mode 100644 index 0000000..5986715 --- /dev/null +++ b/tests/src/Kernel/BambooTwigExtensionsTest.php @@ -0,0 +1,59 @@ +strpad($input, $pad_length, $pad_string, $pad_type); + $this->assertEquals($result, $expected); + } + + /** + * Data provider for testTextStrpad(). + * + * @return array + */ + public function providerTestTextStrpad() { + return [ + ['Alien', 10, " ", STR_PAD_RIGHT, 'Alien '], + ['Alien', 10, "-=", STR_PAD_LEFT, '-=-=-Alien'], + ['Alien', 6, "___", STR_PAD_RIGHT, 'Alien_'], + ['Alien', 3, "*", STR_PAD_RIGHT, 'Alien'], + + // Since the default pad_type is STR_PAD_RIGHT. using STR_PAD_BOTH + // were always favor in the right pad if the required number of padding + // characters can't be evenly divided. + ['Alien', 10, "pp", STR_PAD_BOTH, 'ppAlienppp'], + ['Alien', 6, "p", STR_PAD_BOTH, 'Alienp'], + ['Alien', 8, "p", STR_PAD_BOTH, 'pAlienpp'], + ]; + } + +}