Skip to content

Instantly share code, notes, and snippets.

@itmayziii
Last active August 11, 2016 13:18
Show Gist options
  • Save itmayziii/a04426b7424b993b026c998f2e4fbab0 to your computer and use it in GitHub Desktop.
Save itmayziii/a04426b7424b993b026c998f2e4fbab0 to your computer and use it in GitHub Desktop.
<?php
class ValueValidator {
public static function checkEmpty($value) {
return (empty($value)) ? true : false;
}
public static function checkIsset($value) {
return (isset($value)) ? true : false;
}
public static function checkIsNull($value) {
return (is_null($value)) ? true : false;
}
public static function equalsStrictEmpty($value) {
return ($value === '') ? true : false;
}
public static function equalsStrictNull($value) {
return ($value === null) ? true : false;
}
public static function equalsLooseEmpty($value) {
return ($value == '') ? true : false;
}
public static function equalsLooseNull($value) {
return ($value == null) ? true : false;
}
public static function validateValue($value) {
if (is_string($value) || $value === null || $value === false || $value === true) {
switch ($value) {
case $value === null:
$printedValue = 'null';
break;
case $value === false:
$printedValue = 'false';
break;
case $value === true;
$printedValue = 'true';
break;
default:
$printedValue = $value;
}
echo (ValueValidator::checkEmpty($value)) ? "empty($printedValue) = TRUE\n" : "empty($printedValue) = FALSE\n";
echo (ValueValidator::equalsStrictEmpty($value)) ? "$printedValue === '' = TRUE\n" : "$printedValue === '' = FALSE\n";
echo (ValueValidator::equalsLooseEmpty($value)) ? "$printedValue == '' = TRUE\n" : "$printedValue == '' = FALSE\n";
echo (ValueValidator::checkIsNull($value)) ? "is_null($printedValue) = TRUE\n" : "is_null($printedValue) = FALSE\n";
echo (ValueValidator::equalsStrictNull($value)) ? "$printedValue === null = TRUE\n" : "$printedValue === null = FALSE\n";
echo (ValueValidator::equalsLooseNull($value)) ? "$printedValue == null = TRUE\n" : "$printedValue == null = FALSE\n";
echo (ValueValidator::checkIsset($value)) ? "isset($printedValue) = TRUE\n" : "isset($printedValue) = FALSE\n";
} else {
echo "Check PHP error log for results\n";
error_log('$value provided is ' . print_r($value, true));
(ValueValidator::checkEmpty($value)) ? error_log('empty($value) = TRUE') : error_log('empty($value) = FALSE');
(ValueValidator::equalsStrictEmpty($value)) ? error_log('$value === "" = TRUE') : error_log('$value === "" = FALSE');
(ValueValidator::equalsLooseEmpty($value)) ? error_log('$value == "" = TRUE') : error_log('$value == "" = FALSE');
(ValueValidator::checkIsNull($value)) ? error_log('is_null($value) = TRUE') : error_log('is_null($value) = FALSE');
(ValueValidator::equalsStrictNull($value)) ? error_log('$value === null = TRUE') : error_log('$value === null = FALSE');
(ValueValidator::equalsLooseNull($value)) ? error_log('$value == null = TRUE') : error_log('$value == null = FALSE');
(ValueValidator::checkIsset($value)) ? error_log('isset($value) = TRUE') : error_log('isset($value) = FALSE');
}
}
}
// Examples
$values = ['', null, false, true, [], ['123']];
foreach ($values as $value) {
ValueValidator::validateValue($value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment