PHP code example of limoncello-php / validation

1. Go to this page and download the library: Download limoncello-php/validation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

limoncello-php / validation example snippets


$validator = v::validator([
    'sku'           => r:: 'delivery_date' => r::nullable(r::deliveryDate()),
    'email'         => r::email(),
    'address1'      => r::
];

if ($validator->validate($input)) {
    // use validated/converted/sanitized inputs
    $validated = $validator->getCaptures();
} else {
    // print validation errors
    $errors = $validator->getErrors();
}

class Rules extends \Limoncello\Validation\Rules
{
    public static function sku(): RuleInterface
    {
        return static::stringToInt(new IsSkuRule());
    }

    public static function amount(int $max): RuleInterface
    {
        return static::stringToInt(static::between(1, $max));
    }

    public static function deliveryDate(): RuleInterface
    {
        return static::stringToDateTime(DateTime::ISO8601, new IsDeliveryDateRule());
    }

    public static function email(): RuleInterface
    {
        return static::isString(
            static::filter(FILTER_VALIDATE_EMAIL, null, Errors::IS_EMAIL, static::stringLengthMax(255))
        );
    }

    public static function address1(): RuleInterface
    {
        return static::isString(static::stringLengthBetween(1, 255));
    }

    public static function address2(): RuleInterface
    {
        return static::nullable(static::isString(static::stringLengthMax(255)));
    }

    public static function areTermsAccepted(): RuleInterface
    {
        return static::stringToBool(static::equals(true));
    }
}

class IsSkuRule extends ExecuteRule
{
    public static function execute($value, ContextInterface $context): array
    {
        $pdo   = $context->getContainer()->get(PDO::class);
        $isSku = ...;

        return $isSku === true ?
            self::createSuccessReply($value) :
            self::createErrorReply($context, $value, Errors::IS_VALID_SKU);
    }
}
bash
$ composer