Skip to content

Commit 56be00e

Browse files
committed
Adjust to changes in strings component
Also start with a proper documentation, although it is still incomplete.
1 parent 461ef69 commit 56be00e

File tree

5 files changed

+166
-19
lines changed

5 files changed

+166
-19
lines changed

README.md

Lines changed: 150 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,156 @@ Squirrel Strings Integration for Symfony
33

44
[![Build Status](https://img.shields.io/travis/com/squirrelphp/strings-bundle.svg)](https://travis-ci.com/squirrelphp/strings-bundle) [![Test Coverage](https://api.codeclimate.com/v1/badges/f6e0f7b91f266787ce0c/test_coverage)](https://codeclimate.com/github/squirrelphp/strings-bundle/test_coverage) ![PHPStan](https://img.shields.io/badge/style-level%207-success.svg?style=flat-round&label=phpstan) [![Packagist Version](https://img.shields.io/packagist/v/squirrelphp/strings-bundle.svg?style=flat-round)](https://packagist.org/packages/squirrelphp/strings-bundle) [![PHP Version](https://img.shields.io/packagist/php-v/squirrelphp/strings-bundle.svg)](https://packagist.org/packages/squirrelphp/strings-bundle) [![Software License](https://img.shields.io/badge/license-MIT-success.svg?style=flat-round)](LICENSE)
55

6-
Handles common string operations in applications:
6+
Integration of [squirrelphp/strings](https://github.com/squirrelphp/strings) into Symfony through service tags and bundle configuration.
77

8-
- Filter a string (remove newlines, remove excess spaces, wrap long words, etc.)
9-
- Generate a random string with a set of characters
10-
- Condense a number into a string, and convert back from a string to a number
11-
- Process an URL and modify it in a safe way (convert to relative URL, change parts of it, etc.)
8+
Installation
9+
------------
1210

13-
This component integrates all this into Symfony, so you can use it in Twig, in the Form component, use type hints in your services and easily add new string filters for your application.
11+
```
12+
composer require squirrelphp/strings-bundle
13+
```
1414

15-
More documentation coming soon!
15+
Configuration
16+
-------------
17+
18+
Enable the bundle by adding `Squirrel\StringsBundle\SquirrelStringsBundle` to the list of your used bundles. The bundle then configures itself automatically.
19+
20+
Usage
21+
-----
22+
23+
### String filters
24+
25+
The default ones are (links go to `squirrelphp/strings` documentation):
26+
27+
- [NormalizeNewlinesToUnixStyle](https://github.com/squirrelphp/strings#normalizenewlinestounixstyle)
28+
- [ReplaceUnicodeWhitespaces](https://github.com/squirrelphp/strings#replaceunicodewhitespaces)
29+
- [RemoveExcessSpaces](https://github.com/squirrelphp/strings#removeexcessspaces)
30+
- [LimitConsecutiveUnixNewlinesToTwo](https://github.com/squirrelphp/strings#limitconsecutiveunixnewlines)
31+
- [RemoveZeroWidthSpaces](https://github.com/squirrelphp/strings#removezerowidthspaces)
32+
- [ReplaceNewlinesWithSpaces](https://github.com/squirrelphp/strings#replacenewlineswithspaces)
33+
- [Trim](https://github.com/squirrelphp/strings#trim)
34+
- [ReplaceTabsWithSpaces](https://github.com/squirrelphp/strings#replacetabswithspaces)
35+
- [WrapLongWordsNoHTML20Chars](https://github.com/squirrelphp/strings#wraplongwordsnohtml)
36+
- [WrapLongWordsWithHTML20Chars](https://github.com/squirrelphp/strings#wraplongwordswithhtml)
37+
- [Lowercase](https://github.com/squirrelphp/strings#lowercase)
38+
- [Uppercase](https://github.com/squirrelphp/strings#uppercase)
39+
- [UppercaseFirstCharacter](https://github.com/squirrelphp/strings#uppercasefirstcharacter)
40+
- [UppercaseWordsFirstCharacter](https://github.com/squirrelphp/strings#uppercasewordsfirstcharacter)
41+
- [CamelCaseToSnakeCase](https://github.com/squirrelphp/strings#camelcasetosnakecase)
42+
- [SnakeCaseToCamelCase](https://github.com/squirrelphp/strings#snakecasetocamelcase)
43+
- [RemoveHTMLTags](https://github.com/squirrelphp/strings#removehtmltags)
44+
- [ReplaceUnixStyleNewlinesWithParagraphs](https://github.com/squirrelphp/strings#replaceunixstylenewlineswithparagraphs)
45+
- [EncodeBasicHTMLEntities](https://github.com/squirrelphp/strings#encodebasichtmlentities)
46+
- [DecodeBasicHTMLEntities](https://github.com/squirrelphp/strings#decodebasichtmlentities)
47+
- [DecodeAllHTMLEntities](https://github.com/squirrelphp/strings#decodeallhtmlentities)
48+
- [RemoveNonUTF8Characters](https://github.com/squirrelphp/strings#removenonutf8characters)
49+
- [RemoveNonAlphanumeric](https://github.com/squirrelphp/strings#removenonalphanumeric)
50+
- [RemoveNonNumeric](https://github.com/squirrelphp/strings#removenonnumeric)
51+
- [RemoveNonAsciiAndControlCharacters](https://github.com/squirrelphp/strings#removenonasciiandcontrolcharacters)
52+
- [RemoveEmails](https://github.com/squirrelphp/strings#removeemails)
53+
- [RemoveURLs](https://github.com/squirrelphp/strings#removeurls)
54+
- [NormalizeLettersToAscii](https://github.com/squirrelphp/strings#normalizeletterstoascii)
55+
- [NormalizeToAlphanumeric](https://github.com/squirrelphp/strings#normalizetoalphanumeric)
56+
- [NormalizeToAlphanumericLowercase](https://github.com/squirrelphp/strings#normalizetoalphanumericlowercase)
57+
- [ReplaceNonAlphanumericWithDash](https://github.com/squirrelphp/strings#replacenonalphanumeric)
58+
- [StreamlineInputWithNewlines](https://github.com/squirrelphp/strings#streamlineinputwithnewlines)
59+
- [StreamlineInputNoNewlines](https://github.com/squirrelphp/strings#streamlineinputnonewlines)
60+
61+
You can typehint `Squirrel\Strings\StringFilterSelectInterface` to get a service through which all filters are accessible by the getFilter method:
62+
63+
```php
64+
function (\Squirrel\Strings\StringFilterSelectInterface $selector) {
65+
$string = "hello\n\nthanks a lot!\nbye";
66+
67+
$string = $selector->getFilter('ReplaceNewlinesWithSpaces')
68+
->filter($string);
69+
70+
// Outputs "hello thanks a lot! bye"
71+
echo $string;
72+
}
73+
```
74+
75+
You can also directly typehint a filter class, like `Squirrel\Strings\Filter\NormalizeToAlphanumeric` - all classes are registered as services in Symfony with their class names. All filter classes can also be instantiated in your application.
76+
77+
#### Form string filtering
78+
79+
This bundle automatically configures string filters for your form values that you can use via annotations, example:
80+
81+
```php
82+
<?php
83+
84+
use Squirrel\Strings\Annotation\StringFilter;
85+
86+
class NewsletterChangeAction
87+
{
88+
/**
89+
* @StringFilter({"StreamlineInputNoNewlines","RemoveHTMLTags"})
90+
*/
91+
public $firstName = '';
92+
93+
/**
94+
* @StringFilter("RemoveNonAlphanumeric")
95+
*/
96+
public $confirmToken = '';
97+
}
98+
```
99+
100+
You can run one or more string filters and use any of the default list of filters or any of [your own filters which you added](#adding-new-filters). The filters are run as an early PRE_SUBMIT form event.
101+
102+
#### Adding new filters
103+
104+
Create a class, implement `Squirrel\Strings\StringFilterInterface` and tag the service with `squirrel.strings.filter` in a Symfony config file like this:
105+
106+
```yaml
107+
services:
108+
MyCustomStringFilter:
109+
tags:
110+
- { name: squirrel.strings.filter, filter: MyCustomStringFilter }
111+
```
112+
113+
The filter will be available in `Squirrel\Strings\StringFilterSelectInterface` under the name `MyCustomStringFilter` (the `filter` value you defined for the tag) as well as in StringFilter annotations.
114+
115+
### Random string generators
116+
117+
Generates random strings according to a list of possible characters. The following services are configured by default and can be injected into your services (they are implementing `Squirrel\Strings\RandomStringGeneratorInterface`):
118+
119+
- `squirrel.strings.random.62_characters` generates a random string with the 62 alphanumeric characters (A-Z, a-z and 0-9)
120+
- `squirrel.strings.random.36_characters` generates a random string with the 36 alphanumeric lowercase characters (a-z and 0-9)
121+
- `squirrel.strings.readfriendly_uppercase` generates a random string with 27 easily distinguishable uppercase characters (`234579ACDEFGHKMNPQRSTUVWXYZ`), ideal if a human has to view and enter a code with such characters
122+
- `squirrel.strings.readfriendly_lowercase` generates a random string with 27 easily distinguishable lowercase characters (`234579acdefghkmnpqrstuvwxyz`), the same as the above uppercase variant, just in lowercase
123+
124+
You can add your own random string generator by creating a class implementing `Squirrel\Strings\RandomStringGeneratorInterface` and tagging it with `quirrel.strings.random`:
125+
126+
```yaml
127+
services:
128+
MyCustomRandomGenerator:
129+
tags:
130+
- { name: squirrel.strings.random, generator: MyGenerator }
131+
```
132+
133+
The generator name is used when getting a generator via the `getGenerator` method from the service `Squirrel\Strings\RandomStringGeneratorSelectInterface`, or if you want to inject the random string generator directly just convert the generator name to snake case, so in this example you could inject the service with `@squirrel.strings.random.my_generator`.
134+
135+
The classes `Squirrel\Strings\Random\GeneratorAscii` and `Squirrel\Strings\Random\GeneratorUnicode` are good base classes to use for your own needs, where you only need to define the allowed characters in the constructor:
136+
137+
```yaml
138+
services:
139+
MyCustomRandomGenerator:
140+
class: Squirrel\Strings\Random\GeneratorAscii
141+
arguments:
142+
- '6789'
143+
tags:
144+
- { name: squirrel.strings.random, generator: MyGenerator }
145+
146+
MyOtherCustomRandomGenerator:
147+
class: Squirrel\Strings\Random\GeneratorUnicode
148+
arguments:
149+
- 'öéèä'
150+
tags:
151+
- { name: squirrel.strings.random, generator: MyUnicodeGenerator }
152+
```
153+
154+
The first one would create a generator where only the characters 6, 7, 8 and 9 are generated, the second one where only the unicode characters ö, é, è and ä are generated. Just make sure to not use a character twice, otherwise the class will throw an exception.
155+
156+
### String to number condensing
157+
158+
Todo!

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"require": {
2323
"php": "^7.2",
2424
"symfony/dependency-injection": "^4.0",
25-
"squirrelphp/strings": "^0.7.1"
25+
"squirrelphp/strings": "^0.8"
2626
},
2727
"require-dev": {
2828
"captainhook/plugin-composer": "^4.0",

src/DependencyInjection/Compiler/StringFilterPass.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Squirrel\Strings\Filter\NormalizeLettersToAsciiFilter;
1414
use Squirrel\Strings\Filter\NormalizeNewlinesToUnixStyleFilter;
1515
use Squirrel\Strings\Filter\NormalizeToAlphanumericFilter;
16+
use Squirrel\Strings\Filter\NormalizeToAlphanumericLowercaseFilter;
1617
use Squirrel\Strings\Filter\RemoveEmailsFilter;
1718
use Squirrel\Strings\Filter\RemoveExcessSpacesFilter;
1819
use Squirrel\Strings\Filter\RemoveHTMLTagsFilter;
@@ -21,11 +22,12 @@
2122
use Squirrel\Strings\Filter\RemoveNonNumericFilter;
2223
use Squirrel\Strings\Filter\RemoveNonUTF8CharactersFilter;
2324
use Squirrel\Strings\Filter\RemoveURLsFilter;
24-
use Squirrel\Strings\Filter\ReplaceNewlinesWithParagraphsAndBreaksFilter;
25+
use Squirrel\Strings\Filter\RemoveZeroWidthSpacesFilter;
2526
use Squirrel\Strings\Filter\ReplaceNewlinesWithSpacesFilter;
2627
use Squirrel\Strings\Filter\ReplaceNonAlphanumericFilter;
2728
use Squirrel\Strings\Filter\ReplaceTabsWithSpacesFilter;
2829
use Squirrel\Strings\Filter\ReplaceUnicodeWhitespacesFilter;
30+
use Squirrel\Strings\Filter\ReplaceUnixStyleNewlinesWithParagraphsAndBreaksFilter;
2931
use Squirrel\Strings\Filter\SnakeCaseToCamelCaseFilter;
3032
use Squirrel\Strings\Filter\StreamlineInputNoNewlinesFilter;
3133
use Squirrel\Strings\Filter\StreamlineInputWithNewlinesFilter;
@@ -60,6 +62,7 @@ class StringFilterPass implements CompilerPassInterface
6062
NormalizeLettersToAsciiFilter::class,
6163
NormalizeNewlinesToUnixStyleFilter::class,
6264
NormalizeToAlphanumericFilter::class,
65+
NormalizeToAlphanumericLowercaseFilter::class,
6366
RemoveExcessSpacesFilter::class,
6467
RemoveEmailsFilter::class,
6568
RemoveHTMLTagsFilter::class,
@@ -68,7 +71,8 @@ class StringFilterPass implements CompilerPassInterface
6871
RemoveNonNumericFilter::class,
6972
RemoveNonUTF8CharactersFilter::class,
7073
RemoveURLsFilter::class,
71-
ReplaceNewlinesWithParagraphsAndBreaksFilter::class,
74+
RemoveZeroWidthSpacesFilter::class,
75+
ReplaceUnixStyleNewlinesWithParagraphsAndBreaksFilter::class,
7276
ReplaceNewlinesWithSpacesFilter::class,
7377
'ReplaceNonAlphanumericWithDash' => ReplaceNonAlphanumericFilter::class,
7478
ReplaceTabsWithSpacesFilter::class,
@@ -80,8 +84,8 @@ class StringFilterPass implements CompilerPassInterface
8084
UppercaseFilter::class,
8185
UppercaseFirstCharacterFilter::class,
8286
UppercaseWordsFirstCharacterFilter::class,
83-
'WrapLongWordsNoHTML80Chars' => WrapLongWordsNoHTMLFilter::class,
84-
'WrapLongWordsWithHTML80Chars' => WrapLongWordsWithHTMLFilter::class,
87+
'WrapLongWordsNoHTML20Chars' => WrapLongWordsNoHTMLFilter::class,
88+
'WrapLongWordsWithHTML20Chars' => WrapLongWordsWithHTMLFilter::class,
8589
];
8690

8791
public function process(ContainerBuilder $container): void

tests/ExtensionPassTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public function testNoFormNoTwig()
2121

2222
$this->processCompilerPass($container);
2323

24-
// service container + filter selector + 31 default filters + random selector + 4 generators
24+
// service container + filter selector + 33 default filters + random selector + 4 generators
2525
// + annotation processor
26-
$this->assertEquals(39, \count($container->getDefinitions()));
26+
$this->assertEquals(41, \count($container->getDefinitions()));
2727

2828
// Make sure all definitions exist that we expect
2929
$this->assertTrue($container->hasDefinition(StringFilterSelectInterface::class));
@@ -41,9 +41,9 @@ public function testFormAndTwig()
4141

4242
$this->processCompilerPass($container);
4343

44-
// service container + filter selector + 31 default filters + random selector + 4 generators
44+
// service container + filter selector + 33 default filters + random selector + 4 generators
4545
// + annotation processor + form type extension + twig extension + form.factory + twig
46-
$this->assertEquals(43, \count($container->getDefinitions()));
46+
$this->assertEquals(45, \count($container->getDefinitions()));
4747

4848
// Make sure all definitions exist that we expect
4949
$this->assertTrue($container->hasDefinition(StringFilterSelectInterface::class));

tests/StringFilterPassTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public function testDefault()
1919

2020
$this->processCompilerPass($container);
2121

22-
// service container + selector + 28 default filters
23-
$this->assertEquals(33, \count($container->getDefinitions()));
22+
// service container + selector + 33 default filters
23+
$this->assertEquals(35, \count($container->getDefinitions()));
2424

2525
// Make sure all definitions exist that we expect
2626
$this->assertTrue($container->hasDefinition(StringFilterSelectInterface::class));
@@ -31,7 +31,7 @@ public function testDefault()
3131

3232
$argument = $definition->getArgument(0);
3333

34-
$this->assertEquals(31, \count($argument));
34+
$this->assertEquals(33, \count($argument));
3535

3636
$container->compile();
3737
}

0 commit comments

Comments
 (0)