Skip to content

Commit

Permalink
Add contains method
Browse files Browse the repository at this point in the history
  • Loading branch information
Nek- committed Aug 8, 2016
1 parent b8771ac commit fb27b46
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
14 changes: 14 additions & 0 deletions StringTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,18 @@ public static function removeStart($str, $toRemove)

return mb_substr($str, mb_strlen($toRemove));
}

/**
* @param string $str The string that should contains the needle
* @param string $needle What should be contained
* @return bool
*/
public static function contains($str, $needle)
{
$position = strpos($str, $needle);
if ($position === 0) {
return true;
}
return (bool) $position;
}
}
18 changes: 18 additions & 0 deletions spec/Nekland/Tools/StringToolsSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,22 @@ function it_should_remove_the_start_of_a_string()
$this::removeStart('Foo bar baz', 'Foo')->shouldReturn(' bar baz');
$this::removeStart('YOLOOOsgs gs gsg sggs g', 'g')->shouldReturn('YOLOOOsgs gs gsg sggs g');
}

function it_should_contain_str()
{
$this::contains('PHP Test India vous êtes accepté', 'accepté')->shouldReturn(true);
$this::contains('PHP Test India vous êtes accepté', 'êtes accepté')->shouldReturn(true);
$this::contains('PHP Test India vous êtes accepté', 'vous êtes accepté')->shouldReturn(true);
$this::contains('PHP Test India vous êtes accepté', 'Test India vous êtes accepté')->shouldReturn(true);
$this::contains('PHP Test India vous êtes accepté', 'PHP')->shouldReturn(true);
$this::contains('PHP Test India vous êtes accepté', 'PHP Test India vous êtes accepté')->shouldReturn(true);
$this::contains('PHP Test India vous êtes accepté', 'coucou le test')->shouldReturn(false);
$this::contains('PHP Test India vous êtes accepté', 'PHP Test India vous êtes accepte')->shouldReturn(false);
$this::contains('PHP Test India vous êtes accepté', 'PHP Test India vous êtes refusé')->shouldReturn(false);
$this::contains('PHP Test India vous êtes accepté', 'Lorem Ipsum vous êtes accepté')->shouldReturn(false);
$this::contains('PHP Test India vous êtes accepté', 'Lorem Ipsum vous êtes refusé')->shouldReturn(false);
$this::contains('Theres nothing to see here', 'there there')->shouldReturn(false);
$this::contains('Hello everybody, how are you today ? :)', 'everybody, how')->shouldReturn(true);
$this::contains('Hello world ! =)', '.+')->shouldReturn(false);
}
}

0 comments on commit fb27b46

Please sign in to comment.