Last active
November 28, 2018 11:07
-
-
Save telless/b72afe456ea71d6c89221c63f9745776 to your computer and use it in GitHub Desktop.
Yii2-style bool-checker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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