You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Integration of [squirrelphp/strings](https://github.com/squirrelphp/strings) into Symfony through service tags and bundle configuration.
7
7
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
+
------------
12
10
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
+
```
14
14
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):
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:
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:
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`:
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:
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.
0 commit comments