Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/decodelabs/lucid
Flexible and expansive sanitisation and validation framework for PHP
https://github.com/decodelabs/lucid
php sanitization validation
Last synced: 17 days ago
JSON representation
Flexible and expansive sanitisation and validation framework for PHP
- Host: GitHub
- URL: https://github.com/decodelabs/lucid
- Owner: decodelabs
- License: mit
- Created: 2022-09-01T14:37:51.000Z (over 2 years ago)
- Default Branch: develop
- Last Pushed: 2024-09-04T21:26:16.000Z (5 months ago)
- Last Synced: 2024-12-08T04:13:06.516Z (about 2 months ago)
- Topics: php, sanitization, validation
- Language: PHP
- Homepage:
- Size: 106 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Lucid
[![PHP from Packagist](https://img.shields.io/packagist/php-v/decodelabs/lucid?style=flat)](https://packagist.org/packages/decodelabs/lucid)
[![Latest Version](https://img.shields.io/packagist/v/decodelabs/lucid.svg?style=flat)](https://packagist.org/packages/decodelabs/lucid)
[![Total Downloads](https://img.shields.io/packagist/dt/decodelabs/lucid.svg?style=flat)](https://packagist.org/packages/decodelabs/lucid)
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/decodelabs/lucid/integrate.yml?branch=develop)](https://github.com/decodelabs/lucid/actions/workflows/integrate.yml)
[![PHPStan](https://img.shields.io/badge/PHPStan-enabled-44CC11.svg?longCache=true&style=flat)](https://github.com/phpstan/phpstan)
[![License](https://img.shields.io/packagist/l/decodelabs/lucid?style=flat)](https://packagist.org/packages/decodelabs/lucid)### 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](https://blog.decodelabs.com)._
---
## Installation
Install the library via composer:
```bash
composer require decodelabs/lucid
```## Usage
Direct value sanitisation can be achieved quickly and painlessly:
```php
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()
:```php
$result = Lucid::validate('int', 'potato', [
'min' => 4
]);if(!$result->isValid()) {
// Do something with the potatoforeach($result->getErrors() as $error) {
echo $error->getMessage();
}
}
```Or conversely if you just need a yes or no answer, use
is()
:```php
if(!Lucid::is('float', 'not a number')) {
// do something
}
```### Importing
Lucid uses [Veneer](https://github.com/decodelabs/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](https://github.com/decodelabs/archetype) to load both
Processors
andConstraints
- implement your own custom classes withinDecodeLabs\Lucid\Processor
orDecodeLabs\Lucid\Constraint
namespaces, or create your own ArchetypeResolver
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](https://github.com/decodelabs/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](https://github.com/decodelabs/lucid-support) for integrating Lucid into your own libraries.
## Licensing
Lucid is licensed under the MIT License. See [LICENSE](./LICENSE) for the full license text.