diff --git a/src/Utilities/Regex.php b/src/Utilities/Regex.php index 427bf49..525720a 100644 --- a/src/Utilities/Regex.php +++ b/src/Utilities/Regex.php @@ -39,6 +39,7 @@ class Regex 'money' => '/^[-+]?\d+([\.,]{1}\d*)?$/', 'color' => '/^[a-f0-9]{6}$/i', '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); } + + /** + * 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); + } } diff --git a/tests/Utilities/RegexTest.php b/tests/Utilities/RegexTest.php index e97544c..f040190 100644 --- a/tests/Utilities/RegexTest.php +++ b/tests/Utilities/RegexTest.php @@ -344,6 +344,17 @@ class RegexTest extends BaseTestCase 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 * @@ -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} */