@ 2017-07-04T12:31:02Z <?php
abstract class Filter
{
abstract public static function check($value, array $options = array());
}
final class IntFilter
{
public static function check($value, array $options = array())
{
return $value;
}
}
final class StringFilter
{
public static function check($value, array $options = array())
{
if (false === is_string($value)) {
return false;
}
if (false !== ($options['trim'] ?? true)) {
$value = trim($value);
}
$length = mb_strlen($value, '8bit');
if ((false === ($options['empty'] ?? true)) && (0 === $length)) {
return false;
}
foreach (['min', 'max', 'size'] as $name) {
$sign = ['min' => '<', 'max' => '>', 'size' => '!=='];
if (isset($options[$name])) {
$optionLength = IntFilter::check($options[$name], ['min' => 1]);
if ($length $sign[$name] $optionLength) {
return false;
}
}
}
foreach (['startsWith', 'endsWith'] as $name) {
if (isset($options[$name])) {
$needle = StringFilter::check($options[$name]);
if (false === self::$name($value, $needle)) {
return false;
}
}
}
return $value;
}
private static function startsWith($haystack, $needle)
{
return $haystack[0] === $needle[0] ? strncmp($haystack, $needle, mb_strlen($needle, '8bit')) === 0 : false;
}
private static function endsWith($haystack, $needle)
{
$length = mb_strlen($needle, '8bit');
$haystack = mb_substr($haystack, -$length);
return $haystack[0] === $needle[0] ? strncmp($haystack, $needle, $length) === 0 : false;
}
}
var_dump(StringFilter::check(' r a ', ['min' => 1]));
var_dump(StringFilter::check(' r a ', ['startsWith' => 'r']));
var_dump(StringFilter::check(' r a ', ['startsWith' => 'z']));
var_dump(StringFilter::check(' r a ', ['endsWith' => 'a']));
var_dump(StringFilter::check(' r a ', ['endsWith' => 'z']));
var_dump(StringFilter::check(' r a ', ['startsWith' => 'r', 'endsWith' => 'a']));
var_dump(StringFilter::check(' r a ', ['startsWith' => 'r', 'endsWith' => 'z']));
var_dump(StringFilter::check(' r a ', ['startsWith' => 'z', 'endsWith' => 'a']));
var_dump(StringFilter::check(' r a ', ['startsWith' => 'z', 'endsWith' => 'y']));
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 7.1.6 0.067 0.023 33.09 7.1.5 0.093 0.010 32.56 7.1.4 0.083 0.017 32.32 7.1.3 0.107 0.007 32.41 7.1.2 0.123 0.013 32.66 7.1.1 0.053 0.010 14.27 7.1.0 0.087 0.013 14.65 7.0.20 0.070 0.007 14.39 7.0.19 0.077 0.010 14.77 7.0.18 0.070 0.007 14.26 7.0.17 0.060 0.010 14.16 7.0.16 0.057 0.007 14.00 7.0.15 0.077 0.010 14.01 7.0.14 0.063 0.010 14.53 7.0.13 0.063 0.007 14.44 7.0.12 0.097 0.007 14.44 7.0.11 0.107 0.007 14.48 7.0.10 0.100 0.013 14.41 7.0.9 0.103 0.010 14.27 7.0.8 0.070 0.003 14.29 7.0.7 0.147 0.007 14.28 7.0.6 0.100 0.010 14.03 7.0.5 0.023 0.007 14.34 7.0.4 0.010 0.007 14.46 7.0.3 0.050 0.010 14.77 7.0.2 0.007 0.007 14.40 7.0.1 0.003 0.007 14.33 7.0.0 0.007 0.003 14.70
preferences:dark mode live preview
138.71 ms | 1394 KiB | 7 Q