Skip to content

Commit

Permalink
Documentation update + PHP 8.1 changes
Browse files Browse the repository at this point in the history
- Add documentation for string testers
- Avoid strlen null deprecation message because a variable
  was not propery type checked
- Upgrade quality tools
  • Loading branch information
iquito committed Nov 29, 2021
1 parent 3d4fc68 commit 845ead4
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 12 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ Squirrel Strings
Handles common string operations in PHP applications:

- [Filter a string](#filter-a-string) (remove newlines, remove excess spaces, wrap long words, etc.)
- [Test a string](#test-a-string) (whether a string is valid UTF8 or whether it is in a valid datetime format)
- [Generate a random string](#generate-a-random-string) with a set of characters
- [Condense a string into a number](#condense-a-string-into-a-number), and convert back from a number to a string
- [Condense a number into a string](#condense-a-string-into-a-number), and convert/expand a string into a number
- [Process an URL](#url) and modify it in a safe way (convert to relative URL, change parts of it, etc.)

Filter a string
Expand Down Expand Up @@ -259,6 +260,30 @@ Runs the following filters:

Basically the same as [StreamlineInputWithNewlines](#streamlineinputwithnewlines) but newlines are converted to spaces. This is good for common user input like names, emails addresses and any other fields where newlines make no sense.

Test a string
-------------

Tests a string and returns back true or false (whether the test was successful), using the `Squirrel\Strings\StringTesterInterface` interface:

```php
public function test(string $string): bool;
```

Additional testers can easily be defined by implementing `Squirrel\Strings\StringTesterInterface`. Possible ideas for custom testers in applications:

- Check the structure of external data (which can be highly application dependent)
- Check the string for allowed values or allowed characters

This library has two default testers. Each tester class ends with a `Tester` suffix, which is not mentioned in the following list in order to keep the titles and texts more readable.

#### ValidUTF8

Checks that only valid UTF8 characters are contained within a string. If your application wants to be strict about external data it can make sense to reject any data with non-UTF8 characters (a less strict way of dealing with non-UTF8 characters would be the [RemoveNonUTF8Characters](#removenonutf8characters) filter).

#### ValidDateTime

Checks the string according to a datetime format accepted by the `date` function given in the constructor of this class (default is `Y-m-d` for the ISO date format with dashes between year, month and day) and makes sure the given date exists (`2021-02-29` would return false, yet `2020-02-29` would return true). When validating input this makes it easy to ensure a date is in the format you expect and can be used for further processing.

Generate a random string
------------------------

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"captainhook/plugin-composer": "^5.0",
"phpunit/phpunit": "^9.0",
"twig/twig": "^3.0",
"symfony/form": "^5.0",
"symfony/http-foundation": "^5.0"
"symfony/form": "^5.0|^6.0",
"symfony/http-foundation": "^5.0|^6.0"
},
"suggest": {
"squirrelphp/strings-bundle": "Symfony integration of squirrelphp/strings"
Expand Down
4 changes: 2 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
parameters:
ignoreErrors:
-
message: "#^Method Squirrel\\\\Strings\\\\Attribute\\\\StringFilterProcessor\\:\\:getFromAttribute\\(\\) should return Squirrel\\\\Strings\\\\Attribute\\\\StringFilter\\|null but returns object\\.$#"
message: "#^Parameter \\#1 \\$objectOrClass of class ReflectionClass constructor expects class\\-string\\<T of object\\>\\|T of object, string given\\.$#"
count: 1
path: src/Attribute/StringFilterProcessor.php
path: src/Attribute/StringFilterExtension.php

-
message: "#^Property Squirrel\\\\Strings\\\\Condense\\\\ConverterUnicode\\:\\:\\$stringToNumber \\(array\\<string, int\\>\\) does not accept array\\<int\\|string, \\(int\\|string\\)\\>\\.$#"
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ parameters:
paths:
- src
checkMissingIterableValueType: false
excludes_analyse:
excludePaths:
- src/Filter/NormalizeLettersToAsciiFilter.php
8 changes: 7 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.9.3@4c262932602b9bbab5020863d1eb22d49de0dbf4"/>
<files psalm-version="4.13.1@5cf660f63b548ccd4a56f62d916ee4d6028e01a3">
<file src="src/Attribute/StringFilterExtension.php">
<ArgumentTypeCoercion occurrences="1">
<code>$options['data_class']</code>
</ArgumentTypeCoercion>
</file>
</files>
9 changes: 7 additions & 2 deletions src/Attribute/StringFilterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ public function __construct(private StringFilterProcessor $stringFiltersProcesso
public function buildForm(FormBuilderInterface $builder, array $options): void
{
// Modify data before it is submitted/validated
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($options) {
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($options): void {
// Retrieve submitted content
$data = $event->getData();
$form = $event->getForm();

// We only want form elements with a data class and an array of values
if (\is_array($data) && \strlen($options['data_class']) > 0) {
if (
\is_array($data)
&& isset($options['data_class'])
&& \is_string($options['data_class'])
&& \strlen($options['data_class']) > 0
) {
// Create instance of the form data object, either from empty_data or by instantiating it
if (isset($options['empty_data']) && $options['empty_data'] instanceof $options['data_class']) {
$model = clone $options['empty_data'];
Expand Down
3 changes: 3 additions & 0 deletions tests/StringTesterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ public function testValidDateTime(): void

$testCases = [
'2017-01-01' => true,
'2017-1-1' => false,
'17-01-01' => false,
'2017/01/01' => false,
'2017-02-30' => false,
'illegal' => false,
'' => false,
0 => false,
'2016-02-29' => true,
'2015-02-29' => false,
'2016-2-29' => false,
];

foreach ($testCases as $string => $result) {
Expand Down
2 changes: 1 addition & 1 deletion vendor-bin/phpstan/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": {
"phpstan/phpstan": "^0.12"
"phpstan/phpstan": "^1.0"
}
}
4 changes: 2 additions & 2 deletions vendor-bin/psalm/composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"require": {
"vimeo/psalm": "^4.0",
"psalm/plugin-phpunit": "^0.16",
"psalm/plugin-mockery": "^0.9"
"psalm/plugin-phpunit": "*",
"psalm/plugin-mockery": "*"
}
}

0 comments on commit 845ead4

Please sign in to comment.