squirrelphp / strings-bundle
squirrelphp/strings 的 Symfony 集成 - 使 squirrel 字符串功能在 Symfony 中易于使用
Requires
- php: >=8.1
- squirrelphp/debug: ^2.0
- squirrelphp/strings: ^1.0
- symfony/dependency-injection: ^5.0|^6.0|^7.0
Requires (Dev)
- bamarni/composer-bin-plugin: ^1.3
- captainhook/plugin-composer: ^5.0
- phpunit/phpunit: ^10.0
- symfony/form: ^5.0|^6.0|^7.0
- symfony/twig-bundle: ^5.0|^6.0|^7.0
Suggests
- squirrelphp/strings-bundle: Symfony integration of squirrelphp/strings
README
通过服务标签和包配置将 squirrelphp/strings 集成到 Symfony 项目中。
安装
composer require squirrelphp/strings-bundle
配置
通过将 Squirrel\StringsBundle\SquirrelStringsBundle
添加到你的使用包列表中来启用此包。该包将自动进行配置。
使用
字符串过滤器
默认过滤器分为六个类别(链接到 squirrelphp/strings
文档)
换行符、制表符和空格
- NormalizeNewlinesToUnixStyle
- ReplaceUnicodeWhitespaces
- RemoveExcessSpaces
- LimitConsecutiveUnixNewlinesToTwo
- RemoveZeroWidthSpaces
- ReplaceNewlinesWithSpaces
- Trim
- ReplaceTabsWithSpaces
- WrapLongWordsNoHTML20Chars
- WrapLongWordsWithHTML20Chars
大小写:小写、大写、驼峰式、蛇形
- Lowercase
- Uppercase
- UppercaseFirstCharacter
- UppercaseWordsFirstCharacter
- CamelCaseToSnakeCase
- SnakeCaseToCamelCase
HTML
- RemoveHTMLTags
- RemoveHTMLTagCharacters
- ReplaceUnixStyleNewlinesWithParagraphs
- EncodeBasicHTMLEntities
- DecodeBasicHTMLEntities
- DecodeAllHTMLEntities
移除/限制字符和内容
- RemoveNonUTF8Characters
- RemoveNonAlphanumeric
- RemoveNonAlphabetic
- RemoveNonNumeric
- RemoveNonAsciiAndControlCharacters
- RemoveEmails
- RemoveURLs
Normalize to ASCII
- NormalizeLettersToAscii
- NormalizeToAlphanumeric
- NormalizeToAlphanumericLowercase
- ReplaceNonAlphanumericWithDash
Streamline input
您可以类型提示 Squirrel\Strings\StringFilterSelectInterface
以获取一个服务,其中所有过滤器都可通过 getFilter 方法访问
function (\Squirrel\Strings\StringFilterSelectInterface $selector) { $string = "hello\n\nthanks a lot!\nbye"; $string = $selector->getFilter('ReplaceNewlinesWithSpaces') ->filter($string); // Outputs "hello thanks a lot! bye" echo $string; }
您也可以直接类型提示一个过滤器类,例如 Squirrel\Strings\Filter\NormalizeToAlphanumeric
- 所有类都已在 Symfony 中注册为服务,其名称为类名。所有过滤器类也可以在您的应用程序中实例化。
表单字符串过滤器
此包会自动为您的表单值配置字符串过滤器,您可以通过属性使用它们,例如
<?php use Squirrel\Strings\Annotation\StringFilter; class NewsletterChangeAction { #[StringFilter("StreamlineInputNoNewlines","RemoveHTMLTags")] public string $firstName = ''; #[StringFilter("RemoveNonAlphanumeric")] public string $confirmToken = ''; }
您可以运行一个或多个字符串过滤器,并使用任何默认列表中的过滤器或任何您添加的过滤器。过滤器作为早于 PRE_SUBMIT 表单事件的运行。
注意:仅在将类属性(例如本例中的 $firstName
和 $lastName
)映射到表单且没有自定义属性路径时才有效,因此表单中使用的名称必须与类中的属性名称相同。只要您在表单中不指定 property_path
,就不用担心。
添加新过滤器
创建一个类,实现 Squirrel\Strings\StringFilterInterface
并在 Symfony 配置文件中标记服务,如下所示
services: MyCustomStringFilter: tags: - { name: squirrel.strings.filter, filter: MyCustomStringFilter }
过滤器将在 Squirrel\Strings\StringFilterSelectInterface
下以名称 MyCustomStringFilter
(您为标记定义的 filter
值)以及 StringFilter
属性下可用。
随机字符串生成器
根据可能的字符列表生成随机字符串。以下服务默认配置,并可注入到您的服务中(它们实现了 Squirrel\Strings\RandomStringGeneratorInterface
)
squirrel.strings.random.62_characters
生成包含 62 个字母数字字符(A-Z、a-z 和 0-9)的随机字符串squirrel.strings.random.36_characters
生成一个包含36个字母数字小写字符(a-z和0-9)的随机字符串squirrel.strings.readfriendly_uppercase
生成一个包含27个易于区分的大写字母(234579ACDEFGHKMNPQRSTUVWXYZ
)的随机字符串,如果需要人工查看和输入包含此类字符的代码,则非常理想squirrel.strings.readfriendly_lowercase
生成一个包含27个易于区分的小写字母(234579acdefghkmnpqrstuvwxyz
)的随机字符串,与上述大写变体相同,只是为小写
您可以通过创建一个实现 Squirrel\Strings\RandomStringGeneratorInterface
的类并使用 squirrel.strings.random
标签来添加您自己的随机字符串生成器
services: MyCustomRandomGenerator: tags: - { name: squirrel.strings.random, generator: MyGenerator }
当通过 Squirrel\Strings\RandomStringGeneratorSelectInterface
服务中的 getGenerator
方法获取生成器时,使用驼峰命名法的生成器名称(因此为 62Characters
而不是 62_characters
,以及 ReadfriendlyLowercase
而不是 readfriendly_lowercase
),或者如果您想直接注入随机字符串生成器,则将生成器名称转换为蛇形命名,因此在这个例子中,您可以注入服务为 @squirrel.strings.random.my_generator
。
Squirrel\Strings\Random\GeneratorAscii
和 Squirrel\Strings\Random\GeneratorUnicode
类是您自己需求的良好基类,其中您只需在构造函数中定义允许的字符
services: MyCustomRandomGenerator: class: Squirrel\Strings\Random\GeneratorAscii arguments: - '6789' tags: - { name: squirrel.strings.random, generator: MyGenerator } MyOtherCustomRandomGenerator: class: Squirrel\Strings\Random\GeneratorUnicode arguments: - 'öéèä' tags: - { name: squirrel.strings.random, generator: MyUnicodeGenerator }
第一个将创建一个只生成字符6、7、8和9的生成器,第二个将只生成unicode字符ö、é、è和ä的生成器。请确保不要重复使用字符,否则类将抛出异常。
将字符串压缩为数字
将整数转换为具有给定“字符集”的字符串 - 这样我们可以将整数编码以压缩它(因此具有8个数字的整数现在仅是一个4个字符的字符串),并在需要时将其转换回。
更多信息可在 squirrelphp/strings
库中找到,此包目前不提供任何额外的服务定义来处理压缩。
URL
URL类接受一个URL作为构造函数参数,然后允许您获取或更改URL的某些部分以执行以下操作
- 获取方案、主机、路径、查询字符串和特定的查询字符串变量
- 将绝对URL更改为相对URL
- 更改方案、主机、路径和查询字符串
- 替换查询字符串变量,或添加/删除它们
这可以用来轻松构建或更改URL,或清理给定URL的某些部分,例如在重定向时:使用相对URL而不是绝对URL,以避免恶意重定向到您无法控制的地方。
此功能来自 squirrelphp/strings
,此包不提供任何额外的URL处理功能。
正则表达式包装器
使用内置的 preg_match
、preg_match_all
、preg_replace
和 preg_replace_callback
PHP函数通常会使得代码更难以阅读和理解,因为它们使用引用($matches
)并且有多个可能的返回值。Squirrel\Strings\Regex
包装了这些preg函数的基本功能,创建更容易理解的返回值,并在发生错误时抛出 Squirrel\Strings\Exception\RegexException
。以下是Regex类可用的静态方法
(此功能来自 squirrelphp/strings
,此包不提供任何额外的正则表达式处理功能)
Regex::isMatch(string $pattern, string $subject, int $offset): bool
包装 preg_match
以检查 $pattern
是否存在于 $subject
中。
Regex::getMatches(string $pattern, string $subject, int $flags, int $offset): ?array
将 preg_match_all
封装成函数,用于检索 $subject
中所有 $pattern
的出现,始终设置 PREG_UNMATCHED_AS_NULL
标志并允许添加其他标志。如果没有找到匹配项,则返回 null,否则返回 preg_match_all
为 $matches
设置的结果数组。
Regex::replace(string|array $pattern, string|array $replacement, string $subject, int $limit): string
将 preg_replace
封装成函数,用 $replacement
替换 $pattern
的出现,并且只接受字符串作为 $subject
。
Regex::replaceArray(string|array $pattern, string|array $replacement, array $subject, int $limit): array
将 preg_replace
封装成函数,用 $replacement
替换 $pattern
的出现,并且只接受数组作为 $subject
。
Regex::replaceWithCallback(string|array $pattern, callback $callback, string $subject, int $limit, int $flags): string
将 preg_replace_callback
封装成函数,为 $subject
中每个 $pattern
的出现调用一个回调函数,该回调函数的签名是 function(array $matches): string
,并且只接受字符串作为 $subject
。
Regex::replaceArrayWithCallback(string|array $pattern, callback $callback, array $subject, int $limit, int $flags): array
将 preg_replace_callback
封装成函数,为 $subject
中每个 $pattern
的出现调用一个回调函数,该回调函数的签名是 function(array $matches): string
,并且只接受数组作为 $subject
。