PHP Classes

PHP Sanitize Object: Process class variables with rules from comments

Recommend this page to a friend!
  Info   View files Documentation   View files View files (32)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2023-07-06 (10 months ago) RSS 2.0 feedNot enough user ratingsTotal: 100 All time: 9,796 This week: 82Up
Version License PHP version Categories
jawira-sanitizer 1.0Custom (specified...7Text processing, Validation, PHP 7
Description 

Author

This package can process class variables with rules from comments.

It can take an object of a given class and parses the class file to extract sanitization rules defined in the class code comments.

This package can process the class variables and change their values according to the sanitization rules.

Currently, it supports the following sanitization rules:

- Contain only ASCII characters

- Capitalize the letters of sentence strings

- Numbers above zero

- String only with integer characters

- Numbers below zero

- String with lowercase letters

- String with padding characters

- String without HTML tags

- Trimmed string

- Upper case letters

Innovation Award
PHP Programming Innovation award winner
July 2023
Winner
Secure PHP applications need to be careful with the values passed to the PHP code from external sources that they do not control, such as the parameters passed to PHP via HTTP requests.

When the user enters the values, for instance, in a form, it may be necessary to tell the user to fix invalid values submitted via forms so that the values can comply with applicable rules.

For simple user mistakes, such as incorrect use of the case of words of a person's name, it may be more user-friendly to make the PHP code fix the issue of the letters rather than making the user select the case of the person's name.

This package can do that kind of automatic fixing of values passed to the PHP application by sanitizing the importance of data object classes according to the rules defined in the comments of those classes.

This way, the user will not be bothered to fix the values entered in the forms, and those values will be compliant with the PHP application rules.

Manuel Lemos
Picture of jawira
  Performance   Level  
Name: jawira <contact>
Classes: 11 packages by
Country: Belgium Belgium
Age: ???
All time rank: 302716 in Belgium Belgium
Week rank: 305 Up2 in Belgium Belgium Up
Innovation award
Innovation award
Nominee: 9x

Winner: 2x

Documentation

? jawira/sanitizer

Sanitize your classes using attributes.

Usage

Add sanitizer attributes to your class:

use Jawira\Sanitizer\Filters as Sanitizer;

class User {
    #[Sanitizer\Trim]
    #[Sanitizer\Capitalize]
    public string $name;
}

Call SanitizerService::sanitize method to apply sanitizers:

use Jawira\Sanitizer\SanitizerService;

$sanitizer = new SanitizerService();
$user = new User();
$user->name = ' BOB ';

$sanitizer->sanitize($user);
echo $user->name; // After: 'Bob'

Available sanitizers

| Sanitizer | Works with | Description | |------------------|----------------|-----------------------------------------------------------------------------------------| | Ascii | _string_ | Remove all characters except ascii characters. | | Capitalize | _string_ | Converts the first letter of each word to uppercase and leaves the others as lowercase. | | GteZero | _int_, _float_ | Ensures number is greater than or equal to zero. | | IntegerChars | _string_ | Remove all characters except digits, plus and minus sign. | | Lowercase | _string_ | Make a string lowercase. | | LteZero | _int_, _float_ | Ensures number is lower than or equal to zero. | | Pad | _string_ | Pad a string to a certain length with another string. | | StripTags | _string_ | Strip HTML and PHP tags from a string. | | Trim | _string_ | Strip whitespace (or other characters) from the beginning and end of a string. | | Uppercase | _string_ | Make a string uppercase. |

Install

composer require jawira/sanitizer

Security

You must not solely rely on sanitization, you must implement a proper data validation mechanism.


  Files folder image Files  
File Role Description
Files folder imagedocs (1 file)
Files folder imagesrc (2 files, 1 directory)
Files folder imagetests (1 directory)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .php-cs-fixer.php Example Example script
Accessible without login Plain text file build.xml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE.md Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file psalm.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  docs  
File Role Description
  Accessible without login Plain text file Trim.md Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageFilters (11 files)
  Plain text file SanitizerInterface.php Class Class source
  Plain text file SanitizerService.php Class Class source

  Files folder image Files  /  src  /  Filters  
File Role Description
  Plain text file Ascii.php Class Class source
  Plain text file Capitalize.php Class Class source
  Plain text file FilterInterface.php Class Class source
  Plain text file GteZero.php Class Class source
  Plain text file IntegerChars.php Class Class source
  Plain text file Lowercase.php Class Class source
  Plain text file LteZero.php Class Class source
  Plain text file Pad.php Class Class source
  Plain text file StripTags.php Class Class source
  Plain text file Trim.php Class Class source
  Plain text file Uppercase.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageUnitTests (10 files)

  Files folder image Files  /  tests  /  UnitTests  
File Role Description
  Plain text file AsciiTest.php Class Class source
  Plain text file CapitalizeTest.php Class Class source
  Plain text file GteZeroTest.php Class Class source
  Plain text file IntegerTest.php Class Class source
  Plain text file LowercaseTest.php Class Class source
  Plain text file LteZeroTest.php Class Class source
  Plain text file PadTest.php Class Class source
  Plain text file StripTagsTest.php Class Class source
  Plain text file TrimTest.php Class Class source
  Plain text file UppercaseTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:100
This week:0
All time:9,796
This week:82Up