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.
Install the library via composer:
composer require decodelabs/lucid
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
}
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.
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.
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.
Lucid is licensed under the MIT License. See LICENSE for the full license text.