Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mimic PHP strp_pad #24

Open
wants to merge 7 commits into
base: 8.x-4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
29 changes: 23 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,32 @@ 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.
- `ellipsis` string

```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.
Expand All @@ -411,22 +428,22 @@ The *coming soon* `bamboo_truncate_html` filter to truncates sentences html and
{{ "<p>This <b>is a very</b> long sentence.</p>"|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**
Expand Down
25 changes: 25 additions & 0 deletions bamboo_twig_extensions/src/TwigExtension/TwigText.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
];
}

Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion tests/src/Functional/BambooTwigExtensionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 59 additions & 0 deletions tests/src/Kernel/BambooTwigExtensionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Drupal\Tests\bamboo_twig\Kernel;

use Drupal\KernelTests\KernelTestBase;

/**
* Tests Extensions twig filters and functions.
*
* @group bamboo_twig
* @group bamboo_twig_extensions
*/
class BambooTwigExtensionsTest extends KernelTestBase {

/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'bamboo_twig',
'bamboo_twig_extensions',
];

/**
* @covers \Drupal\bamboo_twig_extensions\TwigExtension\TwigText::strpad
*
* @dataProvider providerTestTextStrpad
*/
public function testTextStrpad($input, $pad_length, $pad_string, $pad_type, $expected) {
/** @var \Drupal\bamboo_twig_extensions\TwigExtension\TwigText $extension */
$extension = \Drupal::service('bamboo_twig_extensions.twig.text');

$result = $extension->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'],
];
}

}