@ 2017-07-04T12:30:26Z <?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.130 0.020 35.07 7.1.5 0.010 0.013 34.72 7.1.4 0.107 0.013 32.44 7.1.3 0.100 0.013 32.48 7.1.2 0.113 0.007 32.63 7.1.1 0.077 0.003 14.64 7.1.0 0.053 0.007 14.66 7.0.20 0.000 0.010 16.49 7.0.19 0.003 0.010 16.46 7.0.18 0.090 0.010 14.29 7.0.17 0.087 0.007 14.29 7.0.16 0.010 0.010 14.20 7.0.15 0.093 0.013 14.14 7.0.14 0.010 0.007 14.44 7.0.13 0.010 0.003 14.66 7.0.12 0.073 0.010 14.46 7.0.11 0.070 0.007 14.29 7.0.10 0.070 0.007 14.26 7.0.9 0.077 0.013 14.36 7.0.8 0.033 0.013 14.26 7.0.7 0.087 0.007 14.36 7.0.6 0.093 0.007 14.16 7.0.5 0.093 0.013 14.30 7.0.4 0.103 0.010 14.54 7.0.3 0.120 0.007 14.45 7.0.2 0.013 0.013 14.39 7.0.1 0.113 0.010 14.52 7.0.0 0.073 0.010 14.35
preferences:dark mode live preview
137.01 ms | 1394 KiB | 7 Q