meta_filters/filters.inc.php

105 lines
2.6 KiB
PHP
Raw Normal View History

2023-05-26 17:50:05 +03:00
<?php
namespace metagen_php;
use Exception;
function flt_str2num($val, $name, $struct, $args)
{
if(is_string($val))
{
if(strlen($val) === 0)
throw new Exception("Bad value, string empty, crc28 can't be generated");
//special case
if(strpos($val, '@') !== 0)
throw new Exception("@ expected");
$name = substr($val, 1) . '.conf.js';
$path = \taskman\config_real_path($name);
if(\taskman\config_get_header($path, $proto_id, $alias))
{
if($alias !== $val)
throw new Exception("Config file header alias doesn't match: $alias vs $val");
$val = $proto_id;
}
else
throw new Exception("Config file header is not valid: $path");
}
if(!is_numeric($val))
throw new Exception("Bad value, not a number(" . serialize($val) . ")");
return 1*$val;
}
function flt_range($val, $name, $struct, $args)
{
if(count($args) != 2)
throw new Exception("Wrong usage of \"flt_range\". Use \"@flt_range:[0,1]\" where 0 - min, 1 - max");
if(!is_numeric($args[0]) || !is_numeric($args[1]))
throw new Exception("Wrong usage of \"flt_range\". Use \"@flt_range:[0,1]\" where 0 - min, 1 - max");
if(is_numeric($val))
{
if($val < $args[0] || $val > $args[1])
throw new Exception("Value ".$val.", not in range(" . $args[0] . "," . $args[1] . ")");
}
return $val;
}
function flt_time($val, $name, $struct, $args)
{
$str = "";
if(is_string($val))
{
if(strlen($val) === 0)
throw new Exception("Bad value, string empty, crc28 can't be generated");
$reg = "/^([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})$/";
if(!preg_match($reg, $val, $matches))
throw new Exception("Bad value, pattern 00:00:00");
$h = $matches[1];
$m = $matches[2];
$s = $matches[3];
return $h * 3600 + $m * 60 + $s;
}
return 0;
}
function flt_class($val, $name, $struct, $args)
{
global $GAME_ROOT;
if(is_string($val))
{
if(strpos($val, '@') === 0)
$val = substr($val, 1) . '.conf.js';
$conf_path = \taskman\config_real_path($val);
$lines = file($conf_path);
foreach($lines as $line)
{
if(preg_match('~"?class"?\s*:\s*"([^"]+)"~', $line, $matches))
{
$conf_class = $matches[1];
if($conf_class != $args[0] && !is_subclass_of($conf_class, $args[0]))
throw new Exception($val.". Config class is \"".$conf_class."\". Must be \"".$args[0]."\" or it's child");
else
return $val;
}
}
throw new Exception($val.". Config class is not found in '$conf_path'");
}
return $val;
}