decodelabs / lucid
Flexible and expansive sanitisation and validation framework
Installs: 10 592
Dependents: 6
Suggesters: 1
Security: 0
Stars: 3
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: ^8.1
- decodelabs/archetype: ^0.3
- decodelabs/coercion: ^0.2.2
- decodelabs/dictum: ^0.6
- decodelabs/exceptional: ^0.4
- decodelabs/lucid-support: ^0.4
- decodelabs/veneer: ^0.11.6
- nesbot/carbon: ^2|^3
Requires (Dev)
- decodelabs/compass: ^0.2
- decodelabs/phpstan-decodelabs: ^0.6
- decodelabs/spectrum: ^0.2.1
Suggests
- decodelabs/compass: IP address validation support
- decodelabs/spectrum: Color validation support
README
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.