Regex - isBinaryValue() method - returns information if given value is a binary value

This commit is contained in:
Meritoo
2018-01-10 17:35:42 +01:00
parent 780d4df17e
commit e9b8fb8852
2 changed files with 85 additions and 0 deletions

View File

@@ -39,6 +39,7 @@ class Regex
'money' => '/^[-+]?\d+([\.,]{1}\d*)?$/', 'money' => '/^[-+]?\d+([\.,]{1}\d*)?$/',
'color' => '/^[a-f0-9]{6}$/i', 'color' => '/^[a-f0-9]{6}$/i',
'bundleName' => '/^(([A-Z]{1}[a-z0-9]+)((?2))*)(Bundle)$/', 'bundleName' => '/^(([A-Z]{1}[a-z0-9]+)((?2))*)(Bundle)$/',
'binaryValue' => '/[^\x20-\x7E\t\r\n]/',
]; ];
/** /**
@@ -796,4 +797,21 @@ class Regex
return (bool)preg_match_all($pattern, $htmlAttributes); return (bool)preg_match_all($pattern, $htmlAttributes);
} }
/**
* Returns information if given value is a binary value
*
* @param string $value Value to verify
* @return bool
*/
public static function isBinaryValue($value)
{
if (!is_string($value)) {
return false;
}
$pattern = self::$patterns['binaryValue'];
return (bool)preg_match($pattern, $value);
}
} }

View File

@@ -344,6 +344,17 @@ class RegexTest extends BaseTestCase
self::assertEquals($expected, Regex::areValidHtmlAttributes($htmlAttributes)); self::assertEquals($expected, Regex::areValidHtmlAttributes($htmlAttributes));
} }
/**
* @param string $value Value to verify
* @param bool $expected Information if value is a binary value
*
* @dataProvider provideBinaryValue
*/
public static function testIsBinaryValue($value, $expected)
{
self::assertEquals($expected, Regex::isBinaryValue($value));
}
/** /**
* Provides name of bundle and information if it's valid name * Provides name of bundle and information if it's valid name
* *
@@ -473,6 +484,62 @@ class RegexTest extends BaseTestCase
]; ];
} }
/**
* Provides value to verify if it is a binary value
*
* @return Generator
*/
public function provideBinaryValue()
{
$file1Path = $this->getFilePathForTesting('lorem-ipsum.txt');
$file2Path = $this->getFilePathForTesting('minion.jpg');
yield[
null,
false,
];
yield[
[],
false,
];
yield[
'',
false,
];
yield[
'abc',
false,
];
yield[
'1234',
false,
];
yield[
1234,
false,
];
yield[
12.34,
false,
];
yield[
fread(fopen($file1Path, 'r'), 1),
false,
];
yield[
fread(fopen($file2Path, 'r'), 1),
true,
];
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */