Skip to content

Instantly share code, notes, and snippets.

@telless
Last active November 28, 2018 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save telless/b72afe456ea71d6c89221c63f9745776 to your computer and use it in GitHub Desktop.
Save telless/b72afe456ea71d6c89221c63f9745776 to your computer and use it in GitHub Desktop.
Yii2-style bool-checker
<?php
abstract class BooleanHelper
{
public static function equalTrue($value): bool
{
return self::process($value) === self::getTrueValue();
}
public static function notEqualTrue($value): bool
{
return self::equalTrue($value) !== true;
}
public static function equalFalse($value): bool
{
return self::process($value) === self::getFalseValue();
}
public static function notEqualFalse($value): bool
{
return self::equalFalse($value) === false;
}
private static function process($value): string
{
ob_start();
var_dump((bool)$value);
$result = ob_get_clean();
return rtrim($result);
}
private static function getTrueValue(): string
{
return 'bool(true)';
}
private static function getFalseValue(): string
{
return 'bool(false)';
}
public static function __callStatic($name, $arguments): bool
{
$value = array_pop($arguments);
$match = ['distance' => INF, 'choice' => null];
$functions = ['equalTrue', 'notEqualTrue', 'equalFalse', 'notEqualFalse'];
foreach ($functions as $key => $function) {
$distance = levenshtein($function, $name);
if ($match['distance'] > $distance) {
$match['distance'] = $distance;
$match['choice'] = $key;
}
}
return call_user_func([self::class, $functions[$match['choice']]], $value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment