Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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 potato

foreach($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 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](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.