1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
<?php namespace amekusa\plz; main::required;
/**
* Fail-safe utilities
*
* To get started, place the following line around top of your code.
* ```php
* use amekusa\plz\alt;
* ```
*/
abstract class alt {
/**
* If `$X` is `null`, returns `$Alt`. Otherwise returns `$X`
* @example Demonstration
* ```php
* $var1 = 'Not Null';
* $var2 = null;
* var_dump( alt::null($var1, 'Null') );
* var_dump( alt::null($var2, 'Null') );
* ```
* ```php
* string(8) "Not Null"
* string(4) "Null"
* ```
* @param mixed $X A variable to check
* @param mixed $Alt A fail-safe value
* @return mixed `$X` or `$Alt`
*/
static function null($X, $Alt) {
return is_null($X) ? $Alt : $X;
}
/**
* If `$X` is *falsy*, returns `$Alt`. Otherwise returns `$X`
* @example Demonstration
* ```php
* $var1 = 'Truthy';
* $var2 = '';
* var_dump( alt::false($var1, 'Falsy') );
* var_dump( alt::false($var2, 'Falsy') );
* ```
* ```php
* string(6) "Truthy"
* string(5) "Falsy"
* ```
* @param mixed $X A variable to check
* @param mixed $Alt A fail-safe value
* @return mixed `$X` or `$Alt`
*/
static function false($X, $Alt) {
return $X ? $X : $Alt;
}
}