PHPackages                             habemus/vacuum-cleaner - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. habemus/vacuum-cleaner

Abandoned → [apdev/lawn-mower](/?search=apdev%2Flawn-mower)Library[Validation &amp; Sanitization](/categories/validation)

habemus/vacuum-cleaner
======================

Stand-alone Class for data filtering

v1.4.0(2y ago)046[1 issues](https://github.com/habemus-agency/vacuum-cleaner/issues)MITPHP

Since Jan 24Pushed 2y ago1 watchersCompare

[ Source](https://github.com/habemus-agency/vacuum-cleaner)[ Packagist](https://packagist.org/packages/habemus/vacuum-cleaner)[ RSS](/packages/habemus-vacuum-cleaner/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (3)Versions (14)Used By (0)

Vacuum: Data validation library
===============================

[](#vacuum-data-validation-library)

Vacuum is a light-weight standalone library for data validation, heavily based on Laravel Validation.

Installation
------------

[](#installation)

```

$ composer require habemus/vacuum-cleaner

```

Basic Usage: The Cleaner Object
-------------------------------

[](#basic-usage-the-cleaner-object)

Instantiate Cleaner::class with input data and validate with built in filters. Cleaner::class accepts only arrays of data as input. Data can be of any native data type and objects. Native PHP file arrays will be converted to FileUpload::class objects.

```
use Habemus\Vacuum\Cleaner;

$validator = new Cleaner(array_merge($_POST,$_FILES));

$validated = $validator->validate([
    'name' => 'required|string|max:25',
    'email' => 'required|email',
    'phone' => 'nullable|digits:11',
    'privacy' => 'required|boolean',
    'quantity' => 'required|integer|gt:1',
    'upload' => 'present|file|mimes:png,jpg|max:2000000',
]);
```

### Results

[](#results)

To check if input has validated use isValid() method. The `validate()` method returns data that has validated succesfully, even if not all of the input data has not been validated.

```
$validated = $validator->validate([
    ...
]);

if($validator->isValid()){

    //handle $validated data
    ...

}else{

    //display errors
    $errors = $validator->errors();

}
```

Filters
-------

[](#filters)

Filters must be enclosed in an array or separated by `|`. Filter parameters must be specified after `:` and separated by commas `,`.

```
$validator->validate([
    'field1' => 'filter1|filter2|filter3|...',
    'field2' => 'filter1_with_params:param1,param2,param3|filter2',
    'field3' => [
        'filter1',
        'filter2',
        'filter3_with_params:param1,param2',
    ],
]);
```

### Available filters

[](#available-filters)

- **required** - the field must be present and not null, empty string, empty array or empty file.
- **nullable** - the field can be omitted.
- **present** - the field must be present, but it's content is not checked.
- **bail** - after this filter, stop executing checks as soon as a filter fails.
- **email** - field must be a valid e-mail.
- **url** - field must be a valid url.
- **alpha\_num** - field must contain only letters and numbers.
- **alpha** - field must contain only letters.
- **numeric** - field must contain only numbers.
- **digits:*size*** - field must contain only numbers characters and must be exact *size* long.
- **mimes:*extension1,extension2,...*** - field must be a valid FileUpload and match the specified *extensions*. Extension is guessed by file Mime Type and it's binary data.
- **file** - field must be valid file upload (uploaded from user).
- **size:*value*** - field size must be of exact *value*. Works with arrays, strings and files.
- **in:*value1,value2,...*** - field must be one of the specified values.
- **gte:*value*** - field must be greater than or equal to *value*. Works with numbers.
- **gt:*value*** - field must be greater than *value*. Works with numbers.
- **lt:*value*** - field must be less than *value*. Works with numbers.
- **lte:*value*** - field must be less than or equal to *value*. Works with numbers.
- **min:*value*** - field must be greater than or equal to *value* in size. Works with arrays and files.
- **max:*value*** - field must be smaller than or equal to *value* in size. Works with arrays and files.
- **integer** - field must be a valid integer.
- **boolean** - field must be a valid boolean. Values accepted are `true`,`false`,`0`,`1`,`'true'`,`'false'`.
- **string** - field must be a string.
- **date** - field must be a valid date.
- **slug** - field must contain only lowercase letters, numbers and `-`.

### Custom Filter

[](#custom-filter)

It's possible to write your own filter using CustomFilter::class. The class constructor accepts a `Closure` as parameter. The Closure must have one $value parameter and must return either `true` on validation or `false` on failure.

```
use Habemus\Vacuum\Cleaner;
use Habemus\Vacuum\Filters\CustomFilter;

$input = [
    'murphy' => "Anything that can go wrong will go wrong",
];

$validator = new Cleaner($input);

$subject = 'wrong';

$data = $validator->validate([
    'murphy' => [
        'required',
        'string',
        new CustomFilter(function($value) use ($subject) {
            if(strpos($value,$subject) !== false){
                return true;
            }

            return false;
        }),
    ],
]);
```

Custom Filters also has the capability of running sanitize function on data being evaluated. Just pass the sanitizing function (`Closure`) as second parameter to the constructor.

```
$mySpaceRemoverFilter = new CustomFilter(
    function($value){
        if(is_string($value)){
            return true;
        }

        return false;
    },
    function($value){
        return str_replace(' ','',$value);
    },
    true
);
```

The third optional parameter (`true` or `false`) is to instruct Cleaner::class instance to execute the sanitizing function after filter validation. Otherwise sanitizing functions are always executed first.

The Request Object
------------------

[](#the-request-object)

The `Request::class` is a small wrapper around the `Cleaner::class` validator instance that speeds up the process of validating HTTP requests data from forms.

```
use Habemus\Vacuum\Request;

$request = new Request(); //init from $_GET,$_POST,$_FILES global vars
$request = new Request($custom_data); //init with custom data

$validated_data = $request->validate($filters, $custom_validation_msgs = null, $sanitizer_fn = null); //validate data from request

$request->isValid(); // true or false
$request->getErrors(); //populated after validate()
$request->getValidated(); //populated after validate()
$request->getData(); // retrieve input data
```

FileUpload
----------

[](#fileupload)

This class has some handy methods to help you deal with files uploaded via forms. When FileUpload::class constructor is called, it can raise RuntimeException when attempting to guess file extension or while renaming it. When a FileUpload instance is succesfully created, the uploaded file is automatically renamed with it's upload name, avoiding duplications and overwriting. If you intend to keep the uploaded file it's mandatory to call the method store($destination\_path), because the class destructor will delete the file automatically on exit.

### class methods

[](#class-methods)

- **isEmpty()** - checks if file is empty
- **rename($new\_path)** - moves file to new path
- **store($path)** - saves file to $path
- **getPath()** - return current file fullpath
- **getFilename()** - returns *filename.ext* without path
- **getExtension()** - returns file extension
- **getMime()** - returns file mime type

Now File and FileUpload are separate entities. tmp\_path no longer exists, use getPath() instead.

Error Messages
--------------

[](#error-messages)

To retrieve messages use `errors()`.

```
$errors = $validator->errors();

/*
//output
$errors = [
    'field1_name' => [
        'filter1_name' => 'Error Message',
        'filter2_name' => 'Error Message',
    ],
    'field2_name' => [
        'filter1_name' => 'Error Message',
        'filter2_name' => 'Error Message',
    ],
];

*/
```

This way you can check if field is present and which error has occurred. It's also possibile to specify a custom error message for a specific field by passing an `array` with custom messages as third parameter to Cleaner::class constructor.

```
$custom_messages = [
    'field1_name' => 'Il campo non è valido!',
    'field2_name' => 'You can use ###field### placeholder.',
];

$validator = new Cleaner($input_array,$custom_messages_array);
```

### Other options

[](#other-options)

It's possible to sanitize all fields by passing a `Closure` as third parameter on Cleaner init.

```
$sanitize_all = function($value){

    if(is_string($value)){
        return trim($value);
    }

    return $value;
};

$validator = new Cleaner($input_array, $custom_messages_array, $sanitize_all);
```

Package developed by Habemus Agency in 2021.

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~82 days

Recently: every ~187 days

Total

12

Last Release

1073d ago

Major Versions

v0.1 → v1.02021-01-24

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/68127276?v=4)[habemus-agency](/maintainers/habemus-agency)[@habemus-agency](https://github.com/habemus-agency)

---

Top Contributors

[![habemus-agency](https://avatars.githubusercontent.com/u/68127276?v=4)](https://github.com/habemus-agency "habemus-agency (25 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/habemus-vacuum-cleaner/health.svg)

```
[![Health](https://phpackages.com/badges/habemus-vacuum-cleaner/health.svg)](https://phpackages.com/packages/habemus-vacuum-cleaner)
```

###  Alternatives

[laravel/framework

The Laravel Framework.

34.8k532.1M19.4k](/packages/laravel-framework)[symfony/mailer

Helps sending emails

1.6k394.6M1.3k](/packages/symfony-mailer)[illuminate/validation

The Illuminate Validation package.

18837.7M1.6k](/packages/illuminate-validation)[spatie/flare-client-php

Send PHP errors to Flare

177156.9M21](/packages/spatie-flare-client-php)[drupal/core

Drupal is an open source content management platform powering millions of websites and applications.

19564.8M1.6k](/packages/drupal-core)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.4M196](/packages/sulu-sulu)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
