accentinteractive / disallowlister
Test a string against a disallowlist. Useful for user forms and such.
Requires
- php: ^7.3|^7.4|^8.0
Requires (Dev)
- orchestra/testbench: 4.*|5.*|6.*
- phpunit/phpunit: ^8.4|^9.0
README
This little package tests a string against a disallowlist.
If you are looking for a Laravel-specific implementation, see https://github.com/accentinteractive/laravel-disallowlister
The isDisallowed()
method can use wildcards, like *.
Under the hood, accentinteractive/disallowtester
uses fnmatch()
, so you can use the same wildcards as in that php function (the globbing wildcard patterns):
-
*sex*
disallows sex, sexuality and bisexual. -
cycle*
disallows cycle and cycles, but not bicycle. -
m[o,u]m
disallows mom and mum, but allows mam. -
m?n
disallows man and men, but allows moon.
Installation
You can install the package via composer:
composer require accentinteractive/disallowlister
Usage
Setting the disallowlist
You can pass the disallowlist in the constructor or via other methods.
// Pass the disallowlist in the constructor $disallowLister = new DisallowLister(['foo']); // ['foo'] // Set the disallowlist in the setter method $disallowLister->setDisallowList(['bar']); // ['bar'] // Add an item to the disallowlist $disallowLister->add('baz'); // ['bar', 'baz'] // Add multiple items to the disallowlist $disallowLister->add(['bat', 'fiz']); // ['bar', 'baz', 'bat', 'fiz'] // Remove an item from the disallowlist $disallowLister->remove('fiz'); // ['bar', 'baz', 'bat'] // Remove multiple items from the disallowlist $disallowLister->remove(['baz', 'bat']); // ['bar']
Checking data against the disallowlist
## Literal string $disallowLister = new DisallowLister(['bar', 'foo']); $disallowLister->isDisallowed('bar'); // Returns true $disallowLister->isDisallowed('bars'); // Returns false ## Wildcards // Under the hood, `accentinteractive/disallowtester` // uses `fnmatch()`, so you can use the same // wildcards as in that php function (the // globbing wildcard patterns): (new DisallowLister(['b?r']))->isDisallowed('bar'); // Returns true (new DisallowLister(['m[o,u]m']))->isDisallowed('mom'); // Returns true (new DisallowLister(['*bar*']))->isDisallowed('I like crowbars'); // Returns true
Case sensitivity
// By default, matching is not case sensitive (new DisallowLister(['bar']))->isDisallowed('BAR'); // Returns true // To set case sensitive matching (new DisallowLister(['bar']))->caseSensitive(true)->isDisallowed('BAR'); // Returns false
Whole word checking
// By default the entire string is checked. (new DisallowLister())->setDisallowList(['bar'])->isDisallowed('My favorite bar'); // Returns false // Check word for word. (new DisallowLister())->setDisallowList(['bar'])->setWordForWord(true)->isDisallowed('My favorite bar'); // Returns true
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email joost@accentinteractive.nl instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.