Skip to content

MaplePHP/Validate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MaplePHP - Validation

MaplePHP - Validation is a lightweight and powerful PHP library designed to simplify the validation of various data inputs. Whether you're verifying if a value is a valid email or phone number, ensuring string lengths, or performing more advanced checks like credit card numbers and dates, MaplePHP - Validation offers a comprehensive and intuitive approach. With its wide range of built-in validators and simple syntax, it makes handling complex validation tasks easier, leading to cleaner and more reliable code.

Installation

composer require maplephp/validate

Initiation

You will always initiate an instance with the static method _val followed by a value you want to validate.

use MaplePHP\Validate\Inp;

// Validate option 1
$inp = new Inp("Lorem ipsum dolor");
var_dump($inp->length(1, 200)); // true

// Validate option 2
$valid = Inp::value("Lorem ipsum dolor")->length(1, 200);
var_dump($valid); // true

Validations

Required field

Inp::value("Lorem ipsum dolor")->required();

Check if there is any value (even if it's 0)

Inp::value(0)->hasValue();

Check string length (min, max)

  • Min only:
Inp::value("Lorem ipsum dolor")->length(1);
  • Min and Max:
Inp::value("Lorem ipsum dolor")->length(1, 160);

Check if string has an exact length

Inp::value("Lorem ipsum dolor")->equalLength(10);

Check if value equals or not equals another value

  • Equals:
Inp::value("Lorem ipsum dolor")->equal("Lorem ipsum dolor");
  • Not equals:
Inp::value("Lorem ipsum dolor")->notEqual("Lorem ipsum");

Validate if it's a valid email

Inp::value("john@gmail.com")->email();

Validate if it's a valid phone number

Allows numbers and special characters ("-", "+", " ").

Inp::value("+46709676040")->phone();

Validate Swedish personal number (personnummer)

Inp::value("198808213412")->socialNumber();

Validate Swedish organization number

Inp::value("197511043412")->orgNumber();

Validate credit card number

Inp::value("1616523623422334")->creditCard();

Validate VAT number

Inp::value("SE8272267913")->vatNumber();

Check if value is a valid float

Inp::value("3.1415")->isFloat();

Check if value is a valid integer

Inp::value("42")->isInt();

Check if value is a valid number (numeric)

Inp::value("42")->number();

Check if value is positive or negative

  • Positive:
Inp::value("20")->positive();
  • Negative:
Inp::value("-20")->negative();

Check if value is a valid version number

Inp::value("1.0.0")->validVersion(true); // strict semantic versioning

Compare version with another version

Inp::value("1.0.0")->versionCompare("2.0.0", '>=');

Validate password (lossy or strict)

  • Lossy password (minimum character set):
Inp::value("password123")->lossyPassword(8);
  • Strict password (requires at least one lowercase, uppercase, digit, and special character):
Inp::value("Password#123!")->strictPassword(8);

Validate if value is string and contains only A-Z

  • Both cases:
Inp::value("HelloWorld")->atoZ();
  • Lowercase only:
Inp::value("helloworld")->lowerAtoZ();
  • Uppercase only:
Inp::value("HELLOWORLD")->upperAtoZ();

Check if it's a valid hex color code

Inp::value("#000000")->hex();

Check if it's a valid date

Inp::value("2022-02-13")->date("Y-m-d");

Check if it's a valid date and time

Inp::value("2022-02-13 14:15")->dateTime("Y-m-d H:i");

Check if it's a valid time

Inp::value("14:15")->time("H:i");

Check if someone is at least a certain age

Inp::value("1988-05-22")->age(18);

Check if it's a valid domain name

Inp::value("example.com")->domain();

Check if it's a valid URL (http/https is required)

Inp::value("https://example.com/page")->url();

Check if it's a valid DNS entry

Inp::value("example.com")->dns();

Validate file and directory properties

  • Check if it's a valid file:
Inp::value("/path/to/file.txt")->isFile();
  • Check if it's a directory:
Inp::value("/path/to/directory")->isDir();
  • Check if it's writable:
Inp::value("/path/to/file.txt")->isWritable();
  • Check if it's readable:
Inp::value("/path/to/file.txt")->isReadable();

Validate ZIP code (with custom length)

Inp::value("12345")->zip(5);

Validate if value matches a pattern (regex)

Inp::value("abc")->pregMatch("a-zA-Z");

Validate if value is an array, object, or resource

  • Array:
Inp::value([1, 2, 3])->isArray();
  • Object:
Inp::value($obj)->isObject();
  • Resource:
Inp::value($resource)->isResource();

Validate if value is boolean or interpretable as a boolean

  • Is Boolean:
Inp::value(true)->isBool();
  • Is Boolean-like value (e.g., "yes", "no", "1", "0"):
Inp::value("yes")->isBoolVal();

Validate using multiple methods (one or all must match)

  • Validate if one method passes:
Inp::value("12345")->oneOf(['isInt' => []]);
  • Validate if all methods pass:
Inp::value("12345")->allOf(['isInt' => [], 'length' => [5]]);