diff --git a/src/Traits/Test/Base/BaseTestCaseTrait.php b/src/Traits/Test/Base/BaseTestCaseTrait.php index 6f3f2e5..2f0ae16 100644 --- a/src/Traits/Test/Base/BaseTestCaseTrait.php +++ b/src/Traits/Test/Base/BaseTestCaseTrait.php @@ -14,7 +14,9 @@ use Meritoo\Common\Exception\Type\UnknownOopVisibilityTypeException; use Meritoo\Common\Type\OopVisibilityType; use Meritoo\Common\Utilities\Miscellaneous; use ReflectionClass; +use ReflectionException; use ReflectionMethod; +use stdClass; /** * BaseTestCaseTrait @@ -103,6 +105,26 @@ trait BaseTestCaseTrait yield['surprise/me/one/more/time.txt']; } + /** + * Provides non scalar value, e.g. [] or null + * + * @return Generator + */ + public function provideNonScalarValue() + { + yield[ + [], + ]; + + yield[ + null, + ]; + + yield[ + new stdClass(), + ]; + } + /** * Returns path of file used by tests. * It should be placed in /.data/tests directory of this project. diff --git a/src/Utilities/Reflection.php b/src/Utilities/Reflection.php index 6577d08..23ecd58 100644 --- a/src/Utilities/Reflection.php +++ b/src/Utilities/Reflection.php @@ -428,7 +428,7 @@ class Reflection $parents = class_parents($childClassName); - if (is_array($parents)) { + if (is_array($parents) && 0 < count($parents)) { return in_array($parentClassName, $parents); } diff --git a/src/Utilities/Regex.php b/src/Utilities/Regex.php index c0a5dd1..cc39b19 100644 --- a/src/Utilities/Regex.php +++ b/src/Utilities/Regex.php @@ -154,88 +154,104 @@ class Regex */ public static function isValidPhoneNumber($phoneNumber) { + if (!is_string($phoneNumber)) { + return false; + } + $pattern = self::getPhoneNumberPattern(); - return (bool)preg_match($pattern, $phoneNumber); + return (bool)preg_match($pattern, trim($phoneNumber)); } /** - * Returns array values that matches given pattern (or values that keys matches) + * Returns array values that match given pattern (or values that keys match the pattern) * * @param string $pattern Pattern to match - * @param array $dataArray The array - * @param bool $itsKeyPattern (optional) If is set to true, keys are checks if they match pattern. Otherwise - - * values are checks. + * @param array $array The array (scalar values only) + * @param bool $itsKeyPattern (optional) If is set to true, keys will be checked if they match pattern. + * Otherwise - values will be checked (default behaviour). * @return array */ - public static function getArrayValuesByPattern($pattern, $dataArray, $itsKeyPattern = false) + public static function getArrayValuesByPattern($pattern, array $array, $itsKeyPattern = false) { + /* + * No elements? + * Nothing to do + */ + if (empty($array)) { + return []; + } + if ($itsKeyPattern) { $effect = []; - if (!empty($dataArray)) { - $matches = []; - - foreach ($dataArray as $key => $value) { - if (preg_match($pattern, $key, $matches)) { - $effect[$key] = $value; - } + foreach ($array as $key => $value) { + if ((bool)preg_match($pattern, $key)) { + $effect[$key] = $value; } } return $effect; } - return preg_grep($pattern, $dataArray); + return preg_grep($pattern, $array); } /** * Filters array by given expression and column * - * Expression can be simple compare expression, like ' == 2', or regular expression. + * Expression can be simple compare expression, like " == 2", or regular expression. * Returns filtered array. * - * @param array $array The array that should be filtered + * @param array $array The 2-dimensional array that should be filtered * @param string $arrayColumnKey Column name - * @param string $filterExpression Filter expression, e.g. '== 2' or '!= \'home\'' - * @param bool $itsRegularExpression (optional) If is set to true, means that filter expression is a - * regular expression + * @param string $filterExpression Simple filter expression (e.g. "== 2" or "!= \'home\'") or regular + * expression (e.g. "/\d+/" or "/[a-z]+[,;]{2,}/") + * @param bool $itsRegularExpression (optional) If is set to true, means that filter expression is a regular + * expression. Otherwise - not (default behaviour). * @return array */ public static function arrayFilter($array, $arrayColumnKey, $filterExpression, $itsRegularExpression = false) { - $effect = []; + /* + * No elements? + * Nothing to do + */ + if (empty($array)) { + return []; + } - if (!empty($array)) { - $effect = $array; + $effect = $array; - foreach ($effect as $key => &$item) { - if (isset($item[$arrayColumnKey])) { - $value = $item[$arrayColumnKey]; + foreach ($effect as $key => &$item) { + if (!isset($item[$arrayColumnKey])) { + continue; + } - if ($itsRegularExpression) { - $matches = []; - $pattern = '|' . $filterExpression . '|'; - $matchesCount = preg_match($pattern, $value, $matches); + $value = $item[$arrayColumnKey]; - $remove = 0 == $matchesCount; + if ($itsRegularExpression) { + $matchesCount = preg_match($filterExpression, $value); + $remove = 0 == $matchesCount; + } else { + if (is_string($value)) { + $value = sprintf('\'%s\'', $value); + } elseif (is_bool($value)) { + if (true === $value) { + $value = 'true'; } else { - if ('' == $value) { - $value = '\'\''; - } elseif (is_string($value)) { - $value = '\'' . $value . '\''; - } - - eval('$isTrue = ' . $value . $filterExpression . ';'); - - /* @var bool $isTrue */ - $remove = !$isTrue; - } - - if ($remove) { - unset($effect[$key]); + $value = 'false'; } } + + eval(sprintf('$isEqual = %s%s;', $value, $filterExpression)); + + /* @var bool $isEqual */ + $remove = !$isEqual; + } + + if ($remove) { + unset($effect[$key]); } } @@ -243,36 +259,41 @@ class Regex } /** - * Perform regular expression match with many given patterns. + * Performs regular expression match with many given patterns. * Returns information if given $subject matches one or all given $patterns. * * @param array|string $patterns The patterns to match * @param string $subject The string to check * @param bool $mustAllMatch (optional) If is set to true, $subject must match all $patterns. Otherwise - - * not. + * not (default behaviour). * @return bool */ public static function pregMultiMatch($patterns, $subject, $mustAllMatch = false) { + /* + * No patterns? + * Nothing to do + */ + if (empty($patterns)) { + return false; + } + $effect = false; $patterns = Arrays::makeArray($patterns); - if (!empty($patterns)) { + if ($mustAllMatch) { + $effect = true; + } + + foreach ($patterns as $pattern) { + $matched = (bool)preg_match_all($pattern, $subject); + if ($mustAllMatch) { - $effect = true; - } - - foreach ($patterns as $pattern) { - $matches = []; - $matched = (bool)preg_match_all($pattern, $subject, $matches); - - if ($mustAllMatch) { - $effect = $effect && $matched; - } else { - if ($matched) { - $effect = $matched; - break; - } + $effect = $effect && $matched; + } else { + if ($matched) { + $effect = $matched; + break; } } } @@ -709,6 +730,10 @@ class Regex */ public static function isValidMoneyValue($value) { + if (!is_scalar($value)) { + return false; + } + $pattern = self::getMoneyPattern(); return (bool)preg_match($pattern, $value); @@ -728,33 +753,53 @@ class Regex */ public static function getValidColorHexValue($color, $throwException = true) { + /* + * Not a scalar value? + * Nothing to do + */ + if (!is_scalar($color)) { + return false; + } + $color = Miscellaneous::replace($color, '/#/', ''); $length = strlen($color); - if (3 === $length) { - $color = Miscellaneous::replace($color, '/(.)(.)(.)/', '$1$1$2$2$3$3'); - } else { - if (6 !== $length) { - if ($throwException) { - throw new IncorrectColorHexLengthException($color); - } - - return false; - } - } - - $pattern = self::$patterns['color']; - $match = (bool)preg_match($pattern, $color); - - if (!$match) { + /* + * Color is not 3 or 6 characters long? + * Nothing to do + */ + if (3 !== $length && 6 !== $length) { if ($throwException) { - throw new InvalidColorHexValueException($color); + throw new IncorrectColorHexLengthException($color); } return false; } - return strtolower($color); + /* + * Color is 3 characters long? + * Let's make it 6 characters long + */ + if (3 === $length) { + $color = Miscellaneous::replace($color, '/(.)(.)(.)/', '$1$1$2$2$3$3'); + } + + $pattern = self::$patterns['color']; + $match = (bool)preg_match($pattern, $color); + + /* + * It's valid color + * Nothing to do more + */ + if ($match) { + return strtolower($color); + } + + if ($throwException) { + throw new InvalidColorHexValueException($color); + } + + return false; } /** diff --git a/tests/Utilities/RegexTest.php b/tests/Utilities/RegexTest.php index d95b389..db3c2b9 100644 --- a/tests/Utilities/RegexTest.php +++ b/tests/Utilities/RegexTest.php @@ -9,6 +9,8 @@ namespace Meritoo\Common\Utilities; use Generator; +use Meritoo\Common\Exception\Regex\IncorrectColorHexLengthException; +use Meritoo\Common\Exception\Regex\InvalidColorHexValueException; use Meritoo\Common\Test\Base\BaseTestCase; /** @@ -165,6 +167,12 @@ class RegexTest extends BaseTestCase public function testStartsWithDirectorySeparator() { + /* + * Not provided, default separator + */ + self::assertTrue(Regex::startsWithDirectorySeparator('/my/extra/directory')); + self::assertFalse(Regex::startsWithDirectorySeparator('my/extra/directory')); + /* * Slash as separator */ @@ -184,6 +192,12 @@ class RegexTest extends BaseTestCase public function testEndsWithDirectorySeparator() { + /* + * Not provided, default separator + */ + self::assertTrue(Regex::endsWithDirectorySeparator('my simple text/')); + self::assertFalse(Regex::endsWithDirectorySeparator('my simple text')); + /* * Slash as separator */ @@ -395,6 +409,233 @@ class RegexTest extends BaseTestCase self::assertEquals($expected, Regex::isValidTaxId($taxIdString)); } + /** + * @param mixed $emptyValue Empty value, e.g. "" + * @dataProvider provideEmptyValue + */ + public static function testIsValidPhoneNumberUsingEmptyValue($emptyValue) + { + self::assertFalse(Regex::isValidPhoneNumber($emptyValue)); + } + + /** + * @param string $phoneNumber The phone number to validate / verify + * @param bool $expected Information if phone number is valid + * + * @dataProvider providePhoneNumber + */ + public static function testIsValidPhoneNumber($phoneNumber, $expected) + { + self::assertEquals($expected, Regex::isValidPhoneNumber($phoneNumber)); + } + + /** + * @param string $pattern Pattern to match + * @param array $dataArray The array + * @param array $expected Expected array + * + * @dataProvider providePatternForArrayValues + */ + public static function testGetArrayValuesByPatternUsingValues($pattern, array $dataArray, $expected) + { + self::assertEquals($expected, Regex::getArrayValuesByPattern($pattern, $dataArray)); + } + + /** + * @param string $pattern Pattern to match + * @param array $dataArray The array + * @param array $expected Expected array + * + * @dataProvider providePatternForArrayKeys + */ + public static function testGetArrayValuesByPatternUsingKeys($pattern, array $dataArray, $expected) + { + self::assertEquals($expected, Regex::getArrayValuesByPattern($pattern, $dataArray, true)); + } + + /** + * @param array $array The array that should be filtered + * @param string $arrayColumnKey Column name + * @param string $filterExpression Simple filter expression, e.g. "== 2" or "!= \'home\'" + * @param array $expected Expected array + * + * @dataProvider provideSimpleExpressionForArrayFiltering + */ + public function testArrayFilterUsingSimpleExpression($array, $arrayColumnKey, $filterExpression, $expected) + { + self::assertEquals($expected, Regex::arrayFilter($array, $arrayColumnKey, $filterExpression)); + } + + /** + * @param array $array The array that should be filtered + * @param string $arrayColumnKey Column name + * @param string $filterExpression Regular expression, e.g. "/\d+/" or "/[a-z]+[,;]{2,}/" + * @param array $expected Expected array + * + * @dataProvider provideRegularExpressionForArrayFiltering + */ + public function testArrayFilterUsingRegularExpression($array, $arrayColumnKey, $filterExpression, $expected) + { + self::assertEquals($expected, Regex::arrayFilter($array, $arrayColumnKey, $filterExpression, true)); + } + + /** + * @param array|string $patterns The patterns to match + * @param string $subject The string to check + * @param bool $expected Information if given $subject matches given $patterns + * + * @dataProvider providePatternsAndSubjectForPregMultiMatch + */ + public function testPregMultiMatch($patterns, $subject, $expected) + { + self::assertEquals($expected, Regex::pregMultiMatch($patterns, $subject)); + } + + /** + * @param array|string $patterns The patterns to match + * @param string $subject The string to check + * @param bool $expected Information if given $subject matches given $patterns + * + * @dataProvider providePatternsAndSubjectForPregMultiMatchWhenMustMatchAllPatterns + */ + public function testPregMultiMatchWhenMustMatchAllPatterns($patterns, $subject, $expected) + { + self::assertEquals($expected, Regex::pregMultiMatch($patterns, $subject, true)); + } + + public function testGetMoneyPattern() + { + self::assertEquals('/^[-+]?\d+([\.,]{1}\d*)?$/', Regex::getMoneyPattern()); + } + + /** + * @param mixed $emptyValue Empty value, e.g. "" + * @dataProvider provideEmptyNonMoneyValue + */ + public function testIsValidMoneyValueUsingEmptyValue($emptyValue) + { + self::assertFalse(Regex::isValidMoneyValue($emptyValue)); + } + + /** + * @param mixed $value Value to verify + * @param bool $expected Information if given value is a money value + * + * @dataProvider provideMoneyValue + */ + public function testIsValidMoneyValue($value, $expected) + { + self::assertEquals($expected, Regex::isValidMoneyValue($value)); + } + + /** + * @param mixed $nonScalarValue Non scalar value, e.g. [] or null + * + * @throws IncorrectColorHexLengthException + * @throws InvalidColorHexValueException + * + * @dataProvider provideNonScalarValue + */ + public function testGetValidColorHexValueUsingNonScalarValue($nonScalarValue) + { + self::assertFalse(Regex::getValidColorHexValue($nonScalarValue)); + } + + /** + * @param mixed $emptyValue Empty value, e.g. "" + * + * @throws IncorrectColorHexLengthException + * @throws InvalidColorHexValueException + * + * @dataProvider provideColorEmptyValue + */ + public function testGetValidColorHexValueUsingEmptyValueWithoutException($emptyValue) + { + self::assertFalse(Regex::getValidColorHexValue($emptyValue, false)); + } + + /** + * @param mixed $emptyValue Empty value, e.g. "" + * + * @throws IncorrectColorHexLengthException + * @throws InvalidColorHexValueException + * + * @dataProvider provideColorEmptyValue + */ + public function testGetValidColorHexValueUsingEmptyValue($emptyValue) + { + $this->setExpectedException(IncorrectColorHexLengthException::class); + Regex::getValidColorHexValue($emptyValue); + } + + /** + * @param string $incorrectColor Incorrect value of color + * + * @throws IncorrectColorHexLengthException + * @throws InvalidColorHexValueException + * + * @dataProvider provideColorIncorrectLength + */ + public function testGetValidColorHexValueUsingIncorrectValueWithoutException($incorrectColor) + { + self::assertFalse(Regex::getValidColorHexValue($incorrectColor, false)); + } + + /** + * @param string $incorrectColor Incorrect value of color + * + * @throws IncorrectColorHexLengthException + * @throws InvalidColorHexValueException + * + * @dataProvider provideColorIncorrectLength + */ + public function testGetValidColorHexValueUsingIncorrectValue($incorrectColor) + { + $this->setExpectedException(IncorrectColorHexLengthException::class); + Regex::getValidColorHexValue($incorrectColor); + } + + /** + * @param string $invalidColor Invalid value of color + * + * @throws IncorrectColorHexLengthException + * @throws InvalidColorHexValueException + * + * @dataProvider provideColorInvalidValue + */ + public function testGetValidColorHexValueUsingInvalidValueWithoutException($invalidColor) + { + self::assertFalse(Regex::getValidColorHexValue($invalidColor, false)); + } + + /** + * @param string $invalidColor Invalid value of color + * + * @throws IncorrectColorHexLengthException + * @throws InvalidColorHexValueException + * + * @dataProvider provideColorInvalidValue + */ + public function testGetValidColorHexValueUsingInvalidValue($invalidColor) + { + $this->setExpectedException(InvalidColorHexValueException::class); + Regex::getValidColorHexValue($invalidColor); + } + + /** + * @param string $color Color to verify + * @param string $expected Expected value of color + * + * @throws IncorrectColorHexLengthException + * @throws InvalidColorHexValueException + * + * @dataProvider provideColor + */ + public function testGetValidColorHexValue($color, $expected) + { + self::assertEquals($expected, Regex::getValidColorHexValue($color)); + } + /** * Provides name of bundle and information if it's valid name * @@ -667,6 +908,804 @@ class RegexTest extends BaseTestCase ]; } + /** + * Provides phone number and information if it's valid + * + * @return Generator + */ + public function providePhoneNumber() + { + yield[ + 'abc', + false, + ]; + + yield[ + '1-2-3', + false, + ]; + + yield[ + '123', + true, + ]; + + yield[ + '123 456 789', + true, + ]; + + yield[ + '123456789', + true, + ]; + } + + /** + * Provides pattern and array with values that should match that pattern + * + * @return Generator + */ + public function providePatternForArrayValues() + { + yield[ + '/\d/', + [], + [], + ]; + + yield[ + '/\d+/', + [ + 'lorem', + 'ipsum', + 123, + 'dolor', + '456', + ], + [ + 2 => 123, + 4 => '456', + ], + ]; + + yield[ + '/\d+-[a-z]+/', + [ + 'lorem', + 123, + false, + 'dolor', + '456-ipsum', + ], + [ + 4 => '456-ipsum', + ], + ]; + } + + /** + * Provides pattern and array with keys that should match that pattern + * + * @return Generator + */ + public function providePatternForArrayKeys() + { + yield[ + '/\d/', + [], + [], + ]; + + yield[ + '/\d+/', + [ + 'lorem' => 'ipsum', + 'dolor' => 123, + 'sit', + 4 => '456', + ], + [ + 0 => 'sit', + 4 => '456', + ], + ]; + + yield[ + '/\d+-[a-z]+/', + [ + 'lorem', + '456-ipsum' => 123, + '001-sit' => false, + 'dolor', + ], + [ + '456-ipsum' => 123, + '001-sit' => false, + ], + ]; + } + + /** + * Provides simple compare expression for array filtering and the array + * + * @return Generator + */ + public function provideSimpleExpressionForArrayFiltering() + { + yield[ + [], + 'id', + ' == 2', + [], + ]; + + yield[ + [ + [ + 'id' => 1, + 'first_name' => 'Jane', + 'last_name' => 'Scott', + 'is_active' => true, + ], + [ + 'id' => 2, + 'first_name' => 'George', + 'last_name' => 'Brown', + 'is_active' => true, + ], + [ + 'id' => 3, + 'first_name' => 'Mike', + 'last_name' => 'Green', + 'is_active' => false, + ], + ], + 'birth_date', + ' == 2', + [ + [ + 'id' => 1, + 'first_name' => 'Jane', + 'last_name' => 'Scott', + 'is_active' => true, + ], + [ + 'id' => 2, + 'first_name' => 'George', + 'last_name' => 'Brown', + 'is_active' => true, + ], + [ + 'id' => 3, + 'first_name' => 'Mike', + 'last_name' => 'Green', + 'is_active' => false, + ], + ], + ]; + + yield[ + [ + [ + 'id' => 1, + 'first_name' => 'Jane', + 'last_name' => 'Scott', + 'is_active' => true, + ], + [ + 'id' => 2, + 'first_name' => 'George', + 'last_name' => 'Brown', + 'is_active' => true, + ], + [ + 'id' => 3, + 'first_name' => 'Mike', + 'last_name' => 'Green', + 'is_active' => false, + ], + ], + 'id', + ' == 2', + [ + 1 => [ + 'id' => 2, + 'first_name' => 'George', + 'last_name' => 'Brown', + 'is_active' => true, + ], + ], + ]; + + yield[ + [ + [ + 'id' => 1, + 'first_name' => 'Jane', + 'last_name' => 'Scott', + 'is_active' => true, + ], + [ + 'id' => 2, + 'first_name' => 'George', + 'last_name' => 'Brown', + 'is_active' => true, + ], + [ + 'id' => 3, + 'first_name' => 'Mike', + 'last_name' => 'Green', + 'is_active' => false, + ], + ], + 'id', + ' >= 2', + [ + 1 => [ + 'id' => 2, + 'first_name' => 'George', + 'last_name' => 'Brown', + 'is_active' => true, + ], + 2 => [ + 'id' => 3, + 'first_name' => 'Mike', + 'last_name' => 'Green', + 'is_active' => false, + ], + ], + ]; + + yield[ + [ + [ + 'id' => 1, + 'first_name' => 'Jane', + 'last_name' => 'Scott', + 'is_active' => true, + ], + [ + 'id' => 2, + 'first_name' => 'George', + 'last_name' => 'Brown', + 'is_active' => true, + ], + [ + 'id' => 3, + 'first_name' => 'Mike', + 'last_name' => 'Green', + 'is_active' => false, + ], + ], + 'is_active', + ' !== true', + [ + 2 => [ + 'id' => 3, + 'first_name' => 'Mike', + 'last_name' => 'Green', + 'is_active' => false, + ], + ], + ]; + + yield[ + [ + [ + 'id' => 1, + 'first_name' => 'Jane', + 'last_name' => 'Scott', + 'is_active' => true, + ], + [ + 'id' => 2, + 'first_name' => 'George', + 'last_name' => 'Brown', + 'is_active' => true, + ], + [ + 'id' => 3, + 'first_name' => 'Mike', + 'last_name' => 'Green', + 'is_active' => false, + ], + ], + 'first_name', + ' == \'Mike\'', + [ + 2 => [ + 'id' => 3, + 'first_name' => 'Mike', + 'last_name' => 'Green', + 'is_active' => false, + ], + ], + ]; + } + + /** + * Provides regular expression for array filtering and the array + * + * @return Generator + */ + public function provideRegularExpressionForArrayFiltering() + { + yield[ + [], + 'id', + '/\d+/', + [], + ]; + + yield[ + [ + [ + 'id' => 1, + 'first_name' => 'Jane', + 'last_name' => 'Scott', + 'is_active' => true, + ], + [ + 'id' => 2, + 'first_name' => 'George', + 'last_name' => 'Brown', + 'is_active' => true, + ], + [ + 'id' => 3, + 'first_name' => 'Mike', + 'last_name' => 'Green', + 'is_active' => false, + ], + ], + 'birth_date', + '/\d+/', + [ + [ + 'id' => 1, + 'first_name' => 'Jane', + 'last_name' => 'Scott', + 'is_active' => true, + ], + [ + 'id' => 2, + 'first_name' => 'George', + 'last_name' => 'Brown', + 'is_active' => true, + ], + [ + 'id' => 3, + 'first_name' => 'Mike', + 'last_name' => 'Green', + 'is_active' => false, + ], + ], + ]; + + yield[ + [ + [ + 'id' => 1, + 'first_name' => 'Jane', + 'last_name' => 'Scott', + 'is_active' => true, + ], + [ + 'id' => 123, + 'first_name' => 'George', + 'last_name' => 'Brown', + 'is_active' => true, + ], + [ + 'id' => 3, + 'first_name' => 'Mike', + 'last_name' => 'Green', + 'is_active' => false, + ], + ], + 'id', + '/\d{3}/', + [ + 1 => [ + 'id' => 123, + 'first_name' => 'George', + 'last_name' => 'Brown', + 'is_active' => true, + ], + ], + ]; + + yield[ + [ + [ + 'id' => 1, + 'first_name' => 'Jane', + 'last_name' => 'Scott', + 'is_active' => true, + ], + [ + 'id' => 123, + 'first_name' => 'George', + 'last_name' => 'Brown', + 'is_active' => true, + ], + [ + 'id' => 456, + 'first_name' => 'Mike', + 'last_name' => 'Green', + 'is_active' => false, + ], + ], + 'first_name', + '/George|Mike/', + [ + 1 => [ + 'id' => 123, + 'first_name' => 'George', + 'last_name' => 'Brown', + 'is_active' => true, + ], + 2 => [ + 'id' => 456, + 'first_name' => 'Mike', + 'last_name' => 'Green', + 'is_active' => false, + ], + ], + ]; + + yield[ + [ + [ + 'id' => 1, + 'first_name' => 'Jane', + 'last_name' => 'Scott', + 'is_active' => true, + ], + [ + 'id' => 2, + 'first_name' => 'George', + 'last_name' => 'Brown', + 'is_active' => true, + ], + [ + 'id' => 3, + 'first_name' => 'Mike', + 'last_name' => 'Green-Blue', + 'is_active' => false, + ], + ], + 'last_name', + '/\w+-\w+/', + [ + 2 => [ + 'id' => 3, + 'first_name' => 'Mike', + 'last_name' => 'Green-Blue', + 'is_active' => false, + ], + ], + ]; + } + + /** + * Provides patterns and subject for the pregMultiMatch() method + * + * @return Generator + */ + public function providePatternsAndSubjectForPregMultiMatch() + { + yield[ + '', + '', + false, + ]; + + yield[ + [], + '', + false, + ]; + + yield[ + '/\d+/', + 'Lorem ipsum dolor sit', + false, + ]; + + yield[ + [ + '/\d+/', + '/^[a-z]{4}$/', + ], + 'Lorem ipsum dolor sit', + false, + ]; + + yield[ + '/\w+/', + 'Lorem ipsum dolor sit', + true, + ]; + + yield[ + [ + '/\d+/', + '/\w+/', + ], + 'Lorem ipsum dolor sit', + true, + ]; + } + + /** + * Provides patterns and subject for the pregMultiMatch() method when must match all patterns + * + * @return Generator + */ + public function providePatternsAndSubjectForPregMultiMatchWhenMustMatchAllPatterns() + { + yield[ + '', + '', + false, + ]; + + yield[ + [], + '', + false, + ]; + + yield[ + '/\d+/', + 'Lorem ipsum dolor sit', + false, + ]; + + yield[ + [ + '/\d+/', + '/^[a-z]{4}$/', + ], + 'Lorem ipsum dolor sit', + false, + ]; + + yield[ + '/\w+/', + 'Lorem ipsum dolor sit', + true, + ]; + + yield[ + [ + '/[a-zA-Z ]+/', + '/\w+/', + ], + 'Lorem ipsum dolor sit', + true, + ]; + } + + /** + * Provides empty non money-related value + * + * @return Generator + */ + public function provideEmptyNonMoneyValue() + { + yield['']; + yield[' ']; + yield[null]; + yield[false]; + yield[[]]; + } + + /** + * Provides money-related value and information if the value is valid + * + * @return Generator + */ + public function provideMoneyValue() + { + yield[ + 'abc', + false, + ]; + + yield[ + '-a.b', + false, + ]; + + yield[ + 'a,b', + false, + ]; + + yield[ + 0, + true, + ]; + + yield[ + 1, + true, + ]; + + yield[ + -1, + true, + ]; + + yield[ + 1.2, + true, + ]; + + yield[ + 1.202, + true, + ]; + + yield[ + -1.202, + true, + ]; + + yield[ + '0', + true, + ]; + + yield[ + '1', + true, + ]; + + yield[ + '-1', + true, + ]; + + yield[ + '1.2', + true, + ]; + + yield[ + '1.202', + true, + ]; + + yield[ + '-1.202', + true, + ]; + + yield[ + '1,202', + true, + ]; + + yield[ + '-1,2', + true, + ]; + + yield[ + '-1,202', + true, + ]; + } + + /** + * Provides value of color with incorrect length + * + * @return Generator + */ + public function provideColorIncorrectLength() + { + yield[ + '12', + ]; + + yield[ + '1234', + ]; + + yield[ + '12345678', + ]; + + yield[ + '#12', + ]; + + yield[ + '#1234', + ]; + + yield[ + '#12345678', + ]; + } + + /** + * Provides invalid value of color + * + * @return Generator + */ + public function provideColorInvalidValue() + { + yield[ + '#qwerty', + ]; + + yield[ + 'qwerty', + ]; + } + + /** + * Provides empty non color-related value + * + * @return Generator + */ + public function provideColorEmptyValue() + { + yield[ + '', + ]; + + yield[ + 0, + ]; + + yield[ + '0', + ]; + + yield[ + false, + ]; + } + + /** + * Provides value of color + * + * @return Generator + */ + public function provideColor() + { + yield[ + '#1b0', + '11bb00', + ]; + + yield[ + '#1B0', + '11bb00', + ]; + + yield[ + '#1ab1ab', + '1ab1ab', + ]; + + yield[ + '#1AB1AB', + '1ab1ab', + ]; + + yield[ + '#000', + '000000', + ]; + } + /** * {@inheritdoc} */