Regex - isValidBundleName() method - returns information if given name of bundle is valid

This commit is contained in:
Meritoo
2017-12-21 21:47:45 +01:00
parent 5aaf7cde72
commit 66aefa2446
2 changed files with 82 additions and 0 deletions

View File

@@ -37,6 +37,7 @@ class Regex
'windowsBasedPath' => '/^[A-Z]{1}:\\\.*$/', 'windowsBasedPath' => '/^[A-Z]{1}:\\\.*$/',
'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)$/',
]; ];
/** /**
@@ -723,4 +724,21 @@ class Regex
return strtolower($color); return strtolower($color);
} }
/**
* Returns information if given name of bundle is valid
*
* @param string $bundleName Full name of bundle to verify, e.g. "MyExtraBundle"
* @return bool
*/
public static function isValidBundleName($bundleName)
{
if (!is_string($bundleName)) {
return false;
}
$pattern = self::$patterns['bundleName'];
return (bool)preg_match($pattern, $bundleName);
}
} }

View File

@@ -8,6 +8,7 @@
namespace Meritoo\Common\Utilities; namespace Meritoo\Common\Utilities;
use Generator;
use Meritoo\Common\Test\Base\BaseTestCase; use Meritoo\Common\Test\Base\BaseTestCase;
/** /**
@@ -273,6 +274,69 @@ class RegexTest extends BaseTestCase
self::assertTrue(Regex::isValidNip('5252530705')); // Facebook Poland sp. z o.o. self::assertTrue(Regex::isValidNip('5252530705')); // Facebook Poland sp. z o.o.
} }
/**
* @param mixed $emptyValue Empty value, e.g. ""
* @dataProvider provideEmptyValue
*/
public function testIsValidBundleNameUsingEmptyValue($emptyValue)
{
self::assertFalse(Regex::isValidBundleName($emptyValue));
}
/**
* @param string $bundleName Full name of bundle to verify, e.g. "MyExtraBundle"
* @param bool $expected Information if it's valid name
*
* @dataProvider provideBundleName
*/
public function testIsValidBundleName($bundleName, $expected)
{
self::assertEquals($expected, Regex::isValidBundleName($bundleName));
}
/**
* Provides name of bundle and information if it's valid name
*
* @return Generator
*/
public function provideBundleName()
{
yield[
'something',
false,
];
yield[
'something_different',
false,
];
yield[
'something-else',
false,
];
yield[
'myExtraBundle',
false,
];
yield[
'MyExtra',
false,
];
yield[
'MyExtraBundle',
true,
];
yield[
'MySuperExtraGorgeousBundle',
true,
];
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */