for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/**
* This file is part of NACL.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* @copyright 2019 Nuglif (2018) Inc.
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @author Pierrick Charron <[email protected]>
* @author Charle Demers <[email protected]>
*/
declare(strict_types=1);
namespace Nuglif\Nacl;
class TypeCaster
{
public static function toNum(string $val): float|int
$f = (float) $val;
$i = (int) $val;
if ($i == $f) {
$res = $i;
} else {
$res = $f;
}
if (preg_match('/[^0-9]*$/', strtolower($val), $matches)) {
switch ($matches[0]) {
case 'g':
$res *= 1000;
/* no break */
case 'm':
case 'k':
break;
case 'gb':
$res *= 1024;
case 'mb':
case 'kb':
case 'y':
$res *= 60 * 60 * 24 * 365;
case 'w':
$res *= 7;
case 'd':
$res *= 24;
case 'h':
$res *= 60;
case 'min':
case 'ms':
$res /= 1000;
return $res;
public static function toBool(string $val): bool
$val = strtolower($val);
return 'true' === $val || 'yes' === $val || 'on' === $val;