bayfrontmedia/php-validator

Simple class used to validate a variety of data.

v3.0.0 2023-05-14 14:07 UTC

This package is auto-updated.

Last update: 2024-04-14 15:59:28 UTC


README

Simple class used to validate types and values.

License

This project is open source and available under the MIT License.

Author

Bayfront Media

Requirements

  • PHP ^8.0

Installation

composer require bayfrontmedia/php-validator

Usage

Example:

use Bayfront\Validator\Validate;

$string = 'This is a string.';

if (!Validate::empty($string)) {
    // Do something
}

Strings:

Numbers:

Types:

Other:

empty

Description:

Checks if string is empty.

Parameters:

  • $string (string)

Returns:

  • (bool)

matches

Description:

Checks if two strings match.

Parameters:

  • $string (string)
  • $matches (string)

Returns:

  • (bool)

minLength

Description:

Checks if string length is greater than or equal to a given length.

Parameters:

  • $string (string)
  • $length (int)

Returns:

  • (bool)

maxLength

Description:

Checks if string length is less than or equal to a given length.

Parameters:

  • $string (string)
  • $length (int)

Returns:

  • (bool)

lengthEquals

Description:

Checks if string length is equal to a given length.

Parameters:

  • $string (string)
  • $length (int)

Returns:

  • (bool)

lengthBetween

Description:

Checks if string length is between min and max length.

Parameters:

  • $string (string)
  • $min (int)
  • $max (int)

Returns:

  • (bool)

contains

Description:

Checks if string contains case-sensitive needle(s).

Parameters:

  • $string (string)
  • $needles (string|array)

Returns:

  • (bool)

Example:

use Bayfront\Validator\Validate;

$string = 'This is a string.';

if (!Validate::contains($string, [
    'This',
    'is a'
])) {
    // Do something
}

startsWith

Description:

Checks if string begins with a given needle.

Parameters:

  • $string (string)
  • $needle (string)

Returns:

  • (bool)

endsWith

Description:

Checks if string ends with a given needle.

Parameters:

  • $string (string)
  • $needle (string)

Returns:

  • (bool)

email

Description:

Checks if string validates as email.

Parameters:

  • $string (string)

Returns:

  • (bool)

url

Description:

Checks if string validates as a URL.

Parameters:

  • $string (string)

Returns:

  • (bool)

ip

Description:

Checks if string validates as an IP address.

Parameters:

  • $string (string)

Returns:

  • (bool)

ipv4

Description:

Checks if string validates as an IPv4 address.

Parameters:

  • $string (string)

Returns:

  • (bool)

ipv6

Description:

Checks if string validates as an IPv6 address.

Parameters:

  • $string (string)

Returns:

  • (bool)

alpha

Description:

Checks if string only contains alpha characters.

Parameters:

  • $string (string)

Returns:

  • (bool)

numeric

Description:

Checks if string is numeric.

Parameters:

  • $string (string)

Returns:

  • (bool)

alphaNumeric

Description:

Checks if string only contains alphanumeric characters.

Parameters:

  • $string (string)

Returns:

  • (bool)

date

Description:

Checks if string is a valid date according to a given format.

See: https://www.php.net/manual/en/datetime.format.php

Parameters:

  • $string (string)
  • $format = 'Y-m-d H:i:s' (string)

Returns:

  • (bool)

uuid

Description:

Checks if string is a valid UUID.

Parameters:

  • $string (string)

Returns:

  • (bool)

uuidv4

Description:

Checks if string is a valid UUIDv4.

Parameters:

  • $string (string)

Returns:

  • (bool)

lessThan

Description:

Checks if value is less than a given number.

Parameters:

  • $value (int)
  • $compare (int)

Returns:

  • (bool)

greaterThan

Description:

Checks if value is greater than a given number.

Parameters:

  • $value (int)
  • $compare (int)

Returns:

  • (bool)

lessThanOrEqual

Description:

Checks if value is less than or equal to a given number.

Parameters:

  • $value (int)
  • $compare (int)

Returns:

  • (bool)

greaterThanOrEqual

Description:

Checks if value is greater than or equal to a given number.

Parameters:

  • $value (int)
  • $compare (int)

Returns:

  • (bool)

equals

Description:

Checks if values are equal.

Parameters:

  • $value (int)
  • $compare (int)

Returns:

  • (bool)

between

Description:

Checks if value is between given min and max values.

Parameters:

  • $value (int)
  • $min (int)
  • $max (int)

Returns:

  • (bool)

null

Description:

Checks if value is NULL.

Parameters:

  • $value (mixed)

Returns:

  • (bool)

integer

Description:

Checks if value is an integer.

Parameters:

  • $value (mixed)

Returns:

  • (bool)

float

Description:

Checks if value is a float.

Parameters:

  • $value (mixed)

Returns:

  • (bool)

boolean

Description:

Checks if value is a boolean.

Parameters:

  • $value (mixed)

Returns:

  • (bool)

object

Description:

Checks if value is an object.

Parameters:

  • $value (mixed)

Returns:

  • (bool)

array

Description:

Checks if value is an array.

Parameters:

  • $value (mixed)

Returns:

  • (bool)

string

Description:

Checks if value is a string.

Parameters:

  • $value (mixed)

Returns:

  • (bool)

json

Description:

Checks if value is a JSON string.

Parameters:

  • $value (mixed)

Returns:

  • (bool)

as

Description:

Validate array values against a set of rules.

Multiple rules can be validated against one key by separating them with a pipe (|).

Available rules are:

  • empty
  • email
  • url
  • ip
  • ipv4
  • ipv6
  • alpha
  • numeric
  • alphanumeric
  • uuid
  • uuidv4
  • null
  • integer
  • float
  • boolean
  • object
  • array
  • string
  • json

Parameters:

  • $array (array): Array to validate
  • $rules (array): Array whose keys are the array key to validate in dot notation and values are the rule
  • $require_existing = false (bool): Require all array keys to exist

Returns:

  • (bool)

Example:

use Bayfront\Validator\Validate;

$array = [
    'sku' => 12345,
    'type' => 'shirt',
    'color' => 'blue',
    'sizes' => [
        'small' => [
            'quantity' => 3,
            'price' => 9.99
        ]
    ],
    'on_sale' => true,
    'meta' => [
        'tags' => [
            'popular',
            'sale',
            'mens'
        ]
    ]
];

$rules = [
    'sku' => 'integer',
    'type' => 'string|null',
    'color' => 'string',
    'quantity' => 'integer',
    'sizes.small.quantity' => 'integer',
    'sizes.small.price' => 'float',
    'on_sale' => 'boolean|null',
    'meta.tags' => 'array'
];

if (!Validate::as($array, $rules)) {
    // Do something
}