Download the PHP package tobento/service-sanitizer without Composer
On this page you can find all versions of the php package tobento/service-sanitizer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package service-sanitizer
Sanitizer Service
The Sanitizer Service provides an easy way to sanitize user input.
Table of Contents
- Getting started
- Requirements
- Highlights
- Simple Example
- Documentation
- Sanitizing
- Single value
- Multiple values
- Nested values
- Using array for filters
- Strict sanitation
- Sanitized data only
- Filtering
- Default filters
- Custom Default Filters
- Adding filters
- A note on FilterIf
- Parsing filters
- Sanitizing
- Credits
Getting started
Add the latest version of the sanitizer service running this command.
Requirements
- PHP 8.0 or greater
Highlights
- Framework-agnostic, will work with any project
- Decoupled design
- Extendable
- Nested value support
- Customize filters parsing
Simple Example
Here is a simple example of how to use the Sanitizer Service.
Documentation
Sanitizing
Single value
Easily sanitize a single value.
Multiple values
Sanitize multiple values.
Nested values
If the incoming values contains "nested" data, you may specify these attributes in your filters using "dot" syntax:
Using array for filters
Depending on the FiltersParsers implementation you may need to set the filters by an array as a parameter might need the parsing notation such as ":".
Strict sanitation
If strict sanitation is used, filters will be applied even if the data does not exist.
Sanitized data only
Sometimes it might be useful to get only the sanitized data:
Filtering
Default filters
The following filters are available out of the box:
Filter | Parameters | Description |
---|---|---|
cast:int:12 | int, float, string, bool, array | Casts a value into the given type. You might define a default value as the second parameter. This works only on strict sanitation though. |
date:Y-m-d:d.m.Y | Any DateTime formats | Formats the date from given to the target format. |
remove:foo:bar | As many as you want | Removes the parameters set from an array. |
trim | Trims a string. | |
digit | Get only digit characters. | |
alphaStrict | Get only alpha characters [a-zA-Z]. | |
stripTags | Strips any tags. | |
ucwords | Uppercase the first character of each word in a string. | |
lcfirst | Make a string's first character uppercase. | |
lowercase | Make a string lowercase. | |
uppercase | Make a string uppercase. | |
array | ['cast:string', 'cast:int'] | Filters each array data. You must use array syntax for filters. The second parameter is optional and would be the filters for the array keys. |
filterIf:attribute:value | Applies filters if an attribute exactly matches the value. |
:warning: Some filters like stripTags return the original value if the type is not a string. So you might add the cast:string filter in addition.
Custom Default Filters
If you want to set your own default filters you can do it by the following way:
Adding filters
You can add your own filters by the following way. If the same filter key already exists it will overwrite the filter.
A note on FilterIf
The "filterIf" filter applies filters only if a value matches the given condition.
You can easily add more FilterIf conditions by extending FilterIf class:
Parsing filters
You may change the behaviour of parsing the filters for sanitizing.
Credits
- Tobias Strub
- All Contributors
All versions of service-sanitizer with dependencies
tobento/service-collection Version ^1.0
tobento/service-dater Version ^1.0