Skip to content

Flexible and expansive sanitisation and validation framework for PHP

License

Notifications You must be signed in to change notification settings

decodelabs/lucid

Repository files navigation

Lucid

PHP from Packagist Latest Version Total Downloads GitHub Workflow Status PHPStan License

Flexible and expansive sanitisation and validation framework for PHP

Lucid provides a unified single-value sanitisation and validation structure for making sure your input makes sense.

Get news and updates on the DecodeLabs blog.


Installation

Install the library via composer:

composer require decodelabs/lucid

Usage

Direct value sanitisation can be achieved quickly and painlessly:

use DecodeLabs\Lucid;

// This ensures the value is a string
$myString = Lucid::cast('string', 'This is a string');

// This is nullable
$notAString = Lucid::cast('?string', null);

// These are constraints - throws an exception
$myString = Lucid::cast('string', 'My very long piece of text', [
    'maxLength' => 10,
    'maxWords' => 4
]);

// Creates an instance of Carbon (DateTime)
$myDate = Lucid::cast('date','tomorrow', [
    'min' => 'yesterday',
    'max' => '+3 days'
]);

If you need more fine grained control of the responses to constraints, use validate():

$result = Lucid::validate('int', 'potato', [
    'min' => 4
]);

if(!$result->isValid()) {
    // Do something with the potato

    foreach($result->getErrors() as $error) {
        echo $error->getMessage();
    }
}

Or conversely if you just need a yes or no answer, use is():

if(!Lucid::is('float', 'not a number')) {
    // do something
}

Importing

Lucid uses Veneer to provide a unified frontage under DecodeLabs\Lucid. You can access all the primary functionality via this static frontage without compromising testing and dependency injection.

Custom processors

Lucid uses Archetype to load both Processors and Constraints - implement your own custom classes within DecodeLabs\Lucid\Processor or DecodeLabs\Lucid\Constraint namespaces, or create your own Archetype Resolver to load them from elsewhere.

Please see the selection of existing implementations for details on how to build your own custom classes.

Provider interfaces

Lucid builds on a sub-package, Lucid Support which makes available a set of Provider interfaces to enable embedded implementations of the Sanitizer structure.

Please see the readme in Lucid Support for integrating Lucid into your own libraries.

Licensing

Lucid is licensed under the MIT License. See LICENSE for the full license text.

About

Flexible and expansive sanitisation and validation framework for PHP

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages