From 269bb9eda51f423ef6409e527df51dbc89758976 Mon Sep 17 00:00:00 2001 From: Kevin Wenger Date: Thu, 17 May 2018 16:00:00 +0200 Subject: [PATCH 1/7] fix readme bamboo_extensions filters --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 441dd00..5dbd19e 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. @@ -411,22 +411,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** From e441dbeaae07e436d6ff2a5179a3fd2bde544d8d Mon Sep 17 00:00:00 2001 From: Kevin Wenger Date: Thu, 17 May 2018 16:01:01 +0200 Subject: [PATCH 2/7] add new filter extension --- .../src/TwigExtension/TwigText.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) 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); + } } From bfdac7551e05e3de7db91ebe9f558fcbb4fec60d Mon Sep 17 00:00:00 2001 From: Kevin Wenger Date: Thu, 17 May 2018 16:01:12 +0200 Subject: [PATCH 3/7] fix typo --- tests/src/Functional/BambooTwigExtensionsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 22bbf3256a9bd680ed69563df10e70d2b21ac4a1 Mon Sep 17 00:00:00 2001 From: Kevin Wenger Date: Thu, 17 May 2018 16:01:40 +0200 Subject: [PATCH 4/7] add kernel coverage for the new filter --- tests/src/Kernel/BambooTwigExtensionsTest.php | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 tests/src/Kernel/BambooTwigExtensionsTest.php 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'], + ]; + } + +} From e9424753d93165c734910cef02782055a23d8d48 Mon Sep 17 00:00:00 2001 From: Kevin Wenger Date: Thu, 17 May 2018 16:02:30 +0200 Subject: [PATCH 5/7] prepare changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) 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 From b5643f292df96d1ee3928107ff034f6f00719023 Mon Sep 17 00:00:00 2001 From: Kevin Wenger Date: Thu, 17 May 2018 16:03:36 +0200 Subject: [PATCH 6/7] add doc about new filter --- README.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5dbd19e..f255bcd 100644 --- a/README.md +++ b/README.md @@ -397,7 +397,22 @@ The `bamboo_extensions_truncate` filter from Twig-extensions [Text](http://twig- ```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, STR_PAD_LEFT, or + STR_PAD_BOTH. When not specified it is assumed to be STR_PAD_RIGHT. + +```twig +{# Pad a string #} +{{ "Alien"|bamboo_extensions_strpad(10, ' ') }} ``` The *coming soon* `bamboo_truncate_html` filter to truncates sentences html and preserves tags. From faffff37ff8fb42f08f13dfa479d79afc922fcb6 Mon Sep 17 00:00:00 2001 From: Kevin Wenger Date: Thu, 17 May 2018 16:07:59 +0200 Subject: [PATCH 7/7] improve doc new feature --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f255bcd..9c43373 100644 --- a/README.md +++ b/README.md @@ -407,12 +407,14 @@ The `bamboo_extensions_strpad` filter provide a way to pad a string to a certain 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, STR_PAD_LEFT, or - STR_PAD_BOTH. When not specified it is assumed to be STR_PAD_RIGHT. +- `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.