PHP Classes

File: src/functions-string.php

Recommend this page to a friend!
  Classes of Rodolfo Berrios Arce   Parameter   src/functions-string.php   Download  
File: src/functions-string.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Parameter
Validate function parameters with PHP attributes
Author: By
Last change:
Date: 15 days ago
Size: 2,615 bytes
 

Contents

Class file image Download
<?php

/*
 * This file is part of Chevere.
 *
 * (c) Rodolfo Berrios <[email protected]>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace
Chevere\Parameter;

use
BackedEnum;
use
Chevere\Parameter\Interfaces\StringParameterInterface;
use
Chevere\Regex\Regex;
use
InvalidArgumentException;

function
string(
   
string|BackedEnum $regex = '',
   
string $description = '',
    ?
string $default = null,
   
bool $sensitive = false
): StringParameterInterface {
    if (
$regex instanceof BackedEnum) {
        if (!
is_string($regex->value)) {
            throw new
InvalidArgumentException('Non-string BackedEnum provided');
        }
       
$regex = $regex->value;
    }
   
$parameter = new StringParameter($description, $sensitive);
    if (
$regex !== '') {
       
$parameter = $parameter
           
->withRegex(
                new
Regex($regex)
            );
    }
    if (
$default !== null) {
       
$parameter = $parameter->withDefault($default);
    }

    return
$parameter;
}

function
intString(
   
string $description = '',
    ?
string $default = null,
   
bool $sensitive = false
): StringParameterInterface {
    return
string(
       
regex: '/^\d+$/',
       
description: $description,
        default:
$default,
       
sensitive: $sensitive
   
);
}

function
enum(string $string, string ...$strings): StringParameterInterface
{
   
array_unshift($strings, $string);
   
$cases = implode('|', $strings);
   
$regex = "/\b{$cases}\b/";

    return
string($regex);
}

/**
 * Parameter for `YYYY-MM-DD` strings.
 */
function date(
   
string $description = 'YYYY-MM-DD',
    ?
string $default = null,
   
bool $sensitive = false
): StringParameterInterface {
   
$regex = '/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/';

    return
string($regex, $description, $default, $sensitive);
}

/**
 * Parameter for `hh:mm:ss` strings.
 */
function time(
   
string $description = 'hh:mm:ss',
    ?
string $default = null,
   
bool $sensitive = false
): StringParameterInterface {
   
$regex = '/^\d{2,3}:[0-5][0-9]:[0-5][0-9]$/';

    return
string($regex, $description, $default, $sensitive);
}

/**
 * Parameter for `YYYY-MM-DD hh:mm:ss` strings.
 */
function datetime(
   
string $description = 'YYYY-MM-DD hh:mm:ss',
    ?
string $default = null,
   
bool $sensitive = false
): StringParameterInterface {
   
$regex = '/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])\s{1}\d{2,3}:[0-5][0-9]:[0-5][0-9]$/';

    return
string($regex, $description, $default, $sensitive);
}