diff --git a/src/Plugin/token_modifier/TitleCase.php b/src/Plugin/token_modifier/TitleCase.php new file mode 100644 index 0000000000000000000000000000000000000000..38ab33947eb4a637cad90a58486206a245a42c46 --- /dev/null +++ b/src/Plugin/token_modifier/TitleCase.php @@ -0,0 +1,25 @@ +<?php + +namespace Drupal\token_modifier\Plugin\token_modifier; + +use Drupal\token_modifier\Plugin\TokenModifierPluginBase; + +/** + * Makes the first characters of each word uppercase. + * + * @TokenModifier( + * id = "title-case", + * name = @Translation("Title case"), + * description = @Translation("Uppercases the first letter of each word.") + * ) + */ +class TitleCase extends TokenModifierPluginBase { + + /** + * {@inheritdoc} + */ + public function transform(string $text, array $data = [], array $options = []) { + return ucwords($this->token->replace($text, $data, $options)); + } + +} diff --git a/src/Plugin/token_modifier/UpperCase.php b/src/Plugin/token_modifier/UpperCase.php new file mode 100644 index 0000000000000000000000000000000000000000..48f03219f74d8e3f914660d5b985bfc1c1201c86 --- /dev/null +++ b/src/Plugin/token_modifier/UpperCase.php @@ -0,0 +1,25 @@ +<?php + +namespace Drupal\token_modifier\Plugin\token_modifier; + +use Drupal\token_modifier\Plugin\TokenModifierPluginBase; + +/** + * Makes all characters of the string uppercase. + * + * @TokenModifier( + * id = "uppercase", + * name = @Translation("Upper case"), + * description = @Translation("Uppercases all characters.") + * ) + */ +class UpperCase extends TokenModifierPluginBase { + + /** + * {@inheritdoc} + */ + public function transform(string $text, array $data = [], array $options = []) { + return strtoupper($this->token->replace($text, $data, $options)); + } + +} diff --git a/tests/src/Unit/TitleCaseTest.php b/tests/src/Unit/TitleCaseTest.php new file mode 100644 index 0000000000000000000000000000000000000000..645f13338d350e83c778832a28cc3112f8a2ffbc --- /dev/null +++ b/tests/src/Unit/TitleCaseTest.php @@ -0,0 +1,37 @@ +<?php + +namespace Drupal\Tests\token_modifier\Unit; + +use Drupal\token_modifier\Plugin\token_modifier\TitleCase; + +class TitleCaseTest extends TokenModifierTestCase { + + /** + * Tests the Title Case token modifier. + * + * @param string $value + * The source value for the modifier. + * @param string $expected + * The expected result. + * + * @covers ::transform + * + * @dataProvider providerTestTransform + */ + public function testTransform(string $value, string $expected) { + $plugin = new TitleCase([], 'title-case', [], $this->token); + $actual = $plugin->transform($value); + $this->assertSame($expected, $actual); + } + + /** + * Provides data for testTransform. + */ + public function providerTestTransform(): array { + $data['basic'] = ['hello world!', 'Hello World!']; + $data['already capitalised'] = ['It\'s Working?', 'It\'s Working?']; + $data['everything else capitalised'] = ['cAPITALISE eVERYTHING', 'CAPITALISE EVERYTHING']; + return $data; + } + +} diff --git a/tests/src/Unit/UpperCaseTest.php b/tests/src/Unit/UpperCaseTest.php new file mode 100644 index 0000000000000000000000000000000000000000..0afff0cd78e6d5bf95fb003cbf84748c91c9c604 --- /dev/null +++ b/tests/src/Unit/UpperCaseTest.php @@ -0,0 +1,37 @@ +<?php + +namespace Drupal\Tests\token_modifier\Unit; + +use Drupal\token_modifier\Plugin\token_modifier\UpperCase; + +class UpperCaseTest extends TokenModifierTestCase { + + /** + * Tests the Upper Case token modifier. + * + * @param string $value + * The source value for the modifier. + * @param string $expected + * The expected result. + * + * @covers ::transform + * + * @dataProvider providerTestTransform + */ + public function testTransform(string $value, string $expected) { + $plugin = new UpperCase([], 'uppercase', [], $this->token); + $actual = $plugin->transform($value); + $this->assertSame($expected, $actual); + } + + /** + * Provides data for testTransform. + */ + public function providerTestTransform(): array { + $data['basic'] = ['hello world!', 'HELLO WORLD!']; + $data['already capitalised'] = ['WORKING?', 'WORKING?']; + $data['camel case'] = ['cApItAlIsE', 'CAPITALISE']; + return $data; + } + +}