Add a method to StringType to be able to compare strings formatted in different ways

This commit is contained in:
Timothy Warren 2017-09-13 16:43:58 -04:00
parent cf068198c4
commit 640f405a56
2 changed files with 80 additions and 0 deletions

View File

@ -23,5 +23,19 @@ use Stringy\Stringy;
*/ */
class StringType extends Stringy { class StringType extends Stringy {
/**
* See if two strings match, despite being delemeted differently,
* such as camelCase, PascalCase, kebab-case, or snake_case.
*
* @param string $strToMatch
* @return boolean
*/
public function fuzzyCaseMatch(string $strToMatch): bool
{
$firstStr = StringType::create($this->str)->dasherize($this->str)->__toString();
$secondStr = StringType::create($strToMatch)->dasherize()->__toString();
return $firstStr === $secondStr;
}
} }
// End of StringType.php // End of StringType.php

View File

@ -0,0 +1,66 @@
<?php declare(strict_types=1);
/**
* Ion
*
* Building blocks for web development
*
* PHP version 7
*
* @package Ion
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2017 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 2.1.0
* @link https://git.timshomepage.net/timw4mail/ion
*/
namespace Aviat\Ion\Tests\Type;
use Aviat\Ion\StringWrapper;
use Aviat\Ion\Tests\Ion_TestCase;
class StringTypeTest extends Ion_TestCase {
use StringWrapper;
public function dataFuzzyCaseMatch()
{
return [
'space separated' => [
'str1' => 'foo bar baz',
'str2' => 'foo-bar-baz',
'expected' => true
],
'camelCase' => [
'str1' => 'fooBarBaz',
'str2' => 'foo-bar-baz',
'expected' => true
],
'PascalCase' => [
'str1' => 'FooBarBaz',
'str2' => 'foo-bar-baz',
'expected' => true
],
'snake_case' => [
'str1' => 'foo_bar_baz',
'str2' => 'foo-bar-baz',
'expected' => true
],
'mEsSYcAse' => [
'str1' => 'fOObArBAZ',
'str2' => 'foo-bar-baz',
'expected' => false
],
];
}
/**
* @dataProvider dataFuzzyCaseMatch
*/
public function testFuzzyCaseMatch($str1, $str2, $expected)
{
$actual = $this->string($str1)->fuzzyCaseMatch($str2);
$this->assertEquals($expected, $actual);
}
}