A simple and elegant assertion and guard methods library for input validation.

1.0.2 2016-08-04 16:06 UTC

This package is auto-updated.

Last update: 2024-11-06 09:48:04 UTC


README

Build Status Coverage Status SensioLabsInsight Latest Stable Version Total Downloads License Donate

While I know there are out there other assertion libraries I was unhappy with them, so I wrote my own.

Usage

Assertion library is really straight forward. Pass input to the method call and it will just run or throw an \Exception if it doesn't meet the requirements.

use NilPortugues\Assert\Assert;

try {
    Assert::nullOrIsLowercase(null);
    Assert::isUppercase('THIS IS GREAT');
    Assert::isNight(new \DateTime('now'));
    
} catch (Exception $e) {
    echo 'Did not meet the requirements, too bad!';
}

Assertion Methods

All assertion methods can be used against different data types (integer, double, objects..).

If you try to validate data for an unexpected data type, an Exception will be thrown making your assertion fail as expected.

Most methods can be called using the "nullOr" prefix instead of the "is" prefix. For instance:

Assert::nullOrIsLowercase('lowercase'); //OK
Assert::isLowercase(null); //raises Exception

Assertion List

All methods allow specifying a custom $message value. If not passed in, a library-defined default will be used.

Those accepting a callable as an argument, expect an Assert statement to be used inside the callable.

Generic Assertions

Assert::isRequired($value, $message = '');
Assert::isNotNull($value, $message = '');
Assert::notEquals($property, $value, $message = '');
Assert::greaterThanOrEqual($property, $value, $message = '');
Assert::greaterThan($property, $value, $message = '');
Assert::lessThanOrEqual($property, $value, $message = '');
Assert::lessThan($property, $value, $message = '');
Assert::isScalar($value, $message = '');

// nullOr assertions
Assert::nullOrNotEquals($property, $value, $message = '');
Assert::nullOrGreaterThanOrEqual($property, $value, $message = '');
Assert::nullOrGreaterThan($property, $value, $message = '');
Assert::nullOrLessThanOrEqual($property, $value, $message = '');
Assert::nullOrLessThan($property, $value, $message = '');
Assert::nullOrIsScalar($value, $message = '');

String Assertions

Assert::isString($value, $message = '')
Assert::isAlphanumeric($value, $message = '')
Assert::isAlpha($value, $message = '')
Assert::isBetween($value, $min, $max, $inclusive = false, $message = '')
Assert::isCharset($value, $charset, $message = '')
Assert::isAllConsonants($value, $message = '')
Assert::contains($value, $contains, $identical = false, $message = '')
Assert::isControlCharacters($value, $message = '')
Assert::isDigit($value, $message = '')
Assert::endsWith($value, $contains, $identical = false, $message = '')
Assert::equals($value, $comparedValue, $identical = false, $message = '')
Assert::in($value, $haystack, $identical = false, $message = '')
Assert::hasGraphicalCharsOnly($value, $message = '')
Assert::hasLength($value, $length, $message = '')
Assert::isLowercase($value, $message = '')
Assert::notEmpty($value, $message = '')
Assert::noWhitespace($value, $message = '')
Assert::hasPrintableCharsOnly($value, $message = '')
Assert::isPunctuation($value, $message = '')
Assert::matchesRegex($value, $regex, $message = '')
Assert::isSlug($value, $message = '')
Assert::isSpace($value, $message = '')
Assert::startsWith($value, $contains, $identical = false, $message = '')
Assert::isUppercase($value, $message = '')
Assert::isVersion($value, $message = '')
Assert::isVowel($value, $message = '')
Assert::isHexDigit($value, $message = '')
Assert::hasLowercase($value, $amount = null, $message = '')
Assert::hasUppercase($value, $amount = null, $message = '')
Assert::hasNumeric($value, $amount = null, $message = '')
Assert::hasSpecialCharacters($value, $amount = null, $message = '')
Assert::isEmail($value, $message = '')
Assert::isUrl($value, $message = '')
Assert::isUUID($value, $strict = true, $message = '')
Assert::isLatitude($latitude, $message = '')
Assert::isLongitude($lontitude, $message = '')
Assert::isTimeString($value, $message = '')
Assert::isDateString($value, $message = '')
Assert::isHexColor($value, $message = '')
Assert::isIpAddress($value, $message = '')
Assert::isIpv4Address($value, $message = '')
Assert::isIpv6Address($value, $message = '')
Assert::isJson($value, $message = '')
Assert::isCreditCard($value, $message = '')
Assert::isPalindrome($value, $message = '')
Assert::isUnderScore($value, $message = '')
Assert::isTitleCase($value, $message = '')
 
// nullOr assertions
Assert::nullOrIsString($value, $message = '')
Assert::nullOrIsAlphanumeric($value, $message = '')
Assert::nullOrIsAlpha($value, $message = '')
Assert::nullOrIsBetween($value, $min, $max, $inclusive = false, $message = '')
Assert::nullOrIsCharset($value, $charset, $message = '')
Assert::nullOrIsAllConsonants($value, $message = '')
Assert::nullOrContains($value, $contains, $identical = false, $message = '')
Assert::nullOrIsControlCharacters($value, $message = '')
Assert::nullOrIsDigit($value, $message = '')
Assert::nullOrEndsWith($value, $contains, $identical = false, $message = '')
Assert::nullOrEquals($value, $comparedValue, $identical = false, $message = '')
Assert::nullOrIn($value, $haystack, $identical = false, $message = '')
Assert::nullOrHasGraphicalCharsOnly($value, $message = '')
Assert::nullOrHasLength($value, $length, $message = '')
Assert::nullOrIsLowercase($value, $message = '')
Assert::nullOrNotEmpty($value, $message = '')
Assert::nullOrNoWhitespace($value, $message = '')
Assert::nullOrHasPrintableCharsOnly($value, $message = '')
Assert::nullOrIsPunctuation($value, $message = '')
Assert::nullOrMatchesRegex($value, $regex, $message = '')
Assert::nullOrIsSlug($value, $message = '')
Assert::nullOrIsSpace($value, $message = '')
Assert::nullOrStartsWith($value, $contains, $identical = false, $message = '')
Assert::nullOrIsUppercase($value, $message = '')
Assert::nullOrIsVersion($value, $message = '')
Assert::nullOrIsVowel($value, $message = '')
Assert::nullOrIsHexDigit($value, $message = '')
Assert::nullOrHasLowercase($value, $amount = null, $message = '')
Assert::nullOrHasUppercase($value, $amount = null, $message = '')
Assert::nullOrHasNumeric($value, $amount = null, $message = '')
Assert::nullOrHasSpecialCharacters($value, $amount = null, $message = '')
Assert::nullOrIsEmail($value, $message = '')
Assert::nullOrIsUrl($value, $message = '')
Assert::nullOrIsUUID($value, $strict = true, $message = '')
Assert::nullOrIsLatitude($latitude, $message = '')
Assert::nullOrIsLongitude($lontitude, $message = '')
Assert::nullOrIsTimeString($value, $message = '')
Assert::nullOrIsDateString($value, $message = '')
Assert::nullOrIsHexColor($value, $message = '')
Assert::nullOrIsIpAddress($value, $message = '')
Assert::nullOrIsIpv4Address($value, $message = '')
Assert::nullOrIsIpv6Address($value, $message = '')
Assert::nullOrIsJson($value, $message = '')
Assert::nullOrIsCreditCard($value, $message = '')
Assert::nullOrIsPalindrome($value, $message = '')
Assert::nullOrIsUnderScore($value, $message = '')
Assert::nullOrIsTitleCase($value, $message = '')

Integer Assertions

Assert::isInteger($value, $message = '')
Assert::isNotZero($value, $message = '')
Assert::isPositiveOrZero($value, $message = '')
Assert::isPositive($value, $message = '')
Assert::isNegativeOrZero($value, $message = '')
Assert::isNegative($value, $message = '')
Assert::isBetween($value, $min, $max, $inclusive = false, $message = '')
Assert::isOdd($value, $message = '')
Assert::isEven($value, $message = '')
Assert::isMultiple($value, $multiple, $message = '')

// nullOr assertions
Assert::nullOrIsInteger($value, $message = '')
Assert::nullOrIsNotZero($value, $message = '')
Assert::nullOrIsPositiveOrZero($value, $message = '')
Assert::nullOrIsPositive($value, $message = '')
Assert::nullOrIsNegativeOrZero($value, $message = '')
Assert::nullOrIsNegative($value, $message = '')
Assert::nullOrIsBetween($value, $min, $max, $inclusive = false, $message = '')
Assert::nullOrIsOdd($value, $message = '')
Assert::nullOrIsEven($value, $message = '')
Assert::nullOrIsMultiple($value, $multiple, $message = '')

Float & Double Assertions

Assert::isFloat($value, $message = '')
Assert::isNotZero($value, $message = '')
Assert::isPositiveOrZero($value, $message = '')
Assert::isPositive($value, $message = '')
Assert::isNegativeOrZero($value, $message = '')
Assert::isNegative($value, $message = '')
Assert::isBetween($value, $min, $max, $inclusive = false, $message = '')
Assert::isOdd($value, $message = '')
Assert::isEven($value, $message = '')
Assert::isMultiple($value, $multiple, $message = '')

// nullOr assertions
Assert::nullOrIsFloat($value, $message = '')
Assert::nullOrIsNotZero($value, $message = '')
Assert::nullOrIsPositiveOrZero($value, $message = '')
Assert::nullOrIsPositive($value, $message = '')
Assert::nullOrIsNegativeOrZero($value, $message = '')
Assert::nullOrIsNegative($value, $message = '')
Assert::nullOrIsBetween($value, $min, $max, $inclusive = false, $message = '')
Assert::nullOrIsOdd($value, $message = '')
Assert::nullOrIsEven($value, $message = '')
Assert::nullOrIsMultiple($value, $multiple, $message = '')

Array & Collection Assertions

Assert::isArray($value, $message = '')
Assert::each($value, callable $valueValidator, callable $keyValidator = null, $message = '')
Assert::hasKeyFormat($value, callable $keyValidator, $message = '')
Assert::endsWith($haystack, $needle, $strict = false, $message = '')
Assert::contains($haystack, $needle, $strict = false, $message = '')
Assert::hasKey($value, $keyName, $message = '')
Assert::hasLength($value, $length, $message = '')
Assert::isNotEmpty($value, $message = '')
Assert::startsWith($haystack, $needle, $strict = false, $message = '')

// nullOr assertions
Assert::nullOrIsArray($value, $message = '')
Assert::nullOrEach($value, callable $valueValidator, callable $keyValidator = null, $message = '')
Assert::nullOrHasKeyFormat($value, callable $keyValidator, $message = '')
Assert::nullOrEndsWith($haystack, $needle, $strict = false, $message = '')
Assert::nullOrContains($haystack, $needle, $strict = false, $message = '')
Assert::nullOrHasKey($value, $keyName, $message = '')
Assert::nullOrHasLength($value, $length, $message = '')
Assert::nullOrIsNotEmpty($value, $message = '')
Assert::nullOrStartsWith($haystack, $needle, $strict = false, $message = '')

DateTime Assertions

Assert::isDateTime($value, $message = '')
Assert::isAfter($value, $limit, $inclusive = false, $message = '')
Assert::isBefore($value, $limit, $inclusive = false, $message = '')
Assert::isDateRange($value, $minDate, $maxDate, $inclusive = false, $message = '')
Assert::isWeekend($value, $message = '')
Assert::isWeekday($value, $message = '')
Assert::isMonday($value, $message = '')
Assert::isTuesday($value, $message = '')
Assert::isWednesday($value, $message = '')
Assert::isThursday($value, $message = '')
Assert::isFriday($value, $message = '')
Assert::isSaturday($value, $message = '')
Assert::isSunday($value, $message = '')
Assert::isToday($value, $message = '')
Assert::isYesterday($value, $message = '')
Assert::isTomorrow($value, $message = '')
Assert::isLeapYear($value, $message = '')
Assert::isMorning($value, $message = '')
Assert::isAfternoon($value, $message = '')
Assert::isEvening($value, $message = '')
Assert::isNight($value, $message = '')
Assert::isFutureDate($value, $message = '')
Assert::isPastDate($value, $message = '')
Assert::isInNextWeek($value, $message = '')
Assert::isInWeek($value, $weekNumber, $message)
Assert::isInLastWeek($value, $message = '')
Assert::isInLastMonth($value, $message = '')
Assert::isInMonth($value, $monthNumber, $message)
Assert::isInNextMonth($value, $message = '')
Assert::isInLastYear($value, $message = '')
Assert::isInYear($value, $yearNumber, $message)
Assert::isInNextYear($value, $message = '')
Assert::isFirstHalfOfYear($value, $message = '')
Assert::isSecondHalfOfYear($value, $message = '')
Assert::isTrimesterOfYear($value, $trimester, $message = '')
Assert::isQuarterOfYear($value, $quarter, $message = '')
Assert::isDayLightSavingTime($value, \DateTimeZone $timezone, $message = '')

// nullOr assertions
Assert::nullOrIsDateTime($value, $message = '')
Assert::nullOrIsAfter($value, $limit, $inclusive = false, $message = '')
Assert::nullOrIsBefore($value, $limit, $inclusive = false, $message = '')
Assert::nullOrIsDateRange($value, $minDate, $maxDate, $inclusive = false, $message = '')
Assert::nullOrIsWeekend($value, $message = '')
Assert::nullOrIsWeekday($value, $message = '')
Assert::nullOrIsMonday($value, $message = '')
Assert::nullOrIsTuesday($value, $message = '')
Assert::nullOrIsWednesday($value, $message = '')
Assert::nullOrIsThursday($value, $message = '')
Assert::nullOrIsFriday($value, $message = '')
Assert::nullOrIsSaturday($value, $message = '')
Assert::nullOrIsSunday($value, $message = '')
Assert::nullOrIsToday($value, $message = '')
Assert::nullOrIsYesterday($value, $message = '')
Assert::nullOrIsTomorrow($value, $message = '')
Assert::nullOrIsLeapYear($value, $message = '')
Assert::nullOrIsMorning($value, $message = '')
Assert::nullOrIsAfternoon($value, $message = '')
Assert::nullOrIsEvening($value, $message = '')
Assert::nullOrIsNight($value, $message = '')
Assert::nullOrIsFutureDate($value, $message = '')
Assert::nullOrIsPastDate($value, $message = '')
Assert::nullOrIsInNextWeek($value, $message = '')
Assert::nullOrIsInWeek($value, $weekNumber, $message)
Assert::nullOrIsInLastWeek($value, $message = '')
Assert::nullOrIsInLastMonth($value, $message = '')
Assert::nullOrIsInMonth($value, $monthNumber, $message)
Assert::nullOrIsInNextMonth($value, $message = '')
Assert::nullOrIsInLastYear($value, $message = '')
Assert::nullOrIsInYear($value, $yearNumber, $message)
Assert::nullOrIsInNextYear($value, $message = '')
Assert::nullOrIsFirstHalfOfYear($value, $message = '')
Assert::nullOrIsSecondHalfOfYear($value, $message = '')
Assert::nullOrIsTrimesterOfYear($value, $trimester, $message = '')
Assert::nullOrIsQuarterOfYear($value, $quarter, $message = '')
Assert::nullOrIsDayLightSavingTime($value, \DateTimeZone $timezone, $message = '')

Object Assertions

Assert::isObject($value, $message = '')
Assert::isInstanceOf($value, $instanceOf, $message = '')
Assert::hasProperty($value, $property, $message = '')
Assert::hasMethod($value, $method, $message = '')
Assert::hasParentClass($value, $message = '')
Assert::isChildOf($value, $parentClass, $message = '')
Assert::inheritsFrom($value, $inheritsClass, $message = '')
Assert::hasInterface($value, $interface, $message = '')

// nullOr assertions
Assert::nullOrIsObject($value, $message = '')
Assert::nullOrIsInstanceOf($value, $instanceOf, $message = '')
Assert::nullOrHasProperty($value, $property, $message = '')
Assert::nullOrHasMethod($value, $method, $message = '')
Assert::nullOrHasParentClass($value, $message = '')
Assert::nullOrIsChildOf($value, $parentClass, $message = '')
Assert::nullOrInheritsFrom($value, $inheritsClass, $message = '')
Assert::nullOrHasInterface($value, $interface, $message = '')

FileUpload Assertions

FileUpload Assertions look into the $_FILES global variable.

Assert::isFileUploaded($uploadName, $message = '')
Assert::isFileUploadedBetweenFileSize($uploadName, $minSize, $maxSize, $format='B', $inclusive = false, $message = '')
Assert::hasFileUploadedFileNameFormat($uploadName, callable $assertion, $message = '')
Assert::hasFileUploadedValidUploadDirectory($uploadName, $uploadDir, $message = '')
Assert::isFileUploadedNotOverwritingExistingFile($uploadName, $uploadDir, $message = '')
Assert::hasFileUploadedFileNameLength($uploadName, $size, $message = '')
Assert::isFileUploadedImage($uploadName, $message = '')
Assert::isFileUploadedMimeType($uploadName, array $allowedTypes, $message = '') 

// nullOr assertions
Assert::nullOrIsFileUploaded($uploadName, $message = '')
Assert::nullOrIsFileUploadedBetweenFileSize($uploadName, $minSize, $maxSize, $format='B', $inclusive=false, $message='')
Assert::nullOrHasFileUploadedFileNameFormat($uploadName, callable $assertion, $message = '')
Assert::nullOrHasFileUploadedValidUploadDirectory($uploadName, $uploadDir, $message = '')
Assert::nullOrIsFileUploadedNotOverwritingExistingFile($uploadName, $uploadDir, $message = '')
Assert::nullOrHasFileUploadedFileNameLength($uploadName, $size, $message = '')
Assert::nullOrIsFileUploadedImage($uploadName, $message = '')
Assert::nullOrIsFileUploadedMimeType($uploadName, array $allowedTypes, $message = '') 

Contribute

Contributions to the package are always welcome!

Support

Get in touch with me using one of the following means:

Authors

License

The code base is licensed under the MIT license.