Skip to content

Commit 14e861a

Browse files
committed
Add Str helper library
Tests included. Methods include: startsWith() endsWith() equals()
1 parent a0f7159 commit 14e861a

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

src/Str.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php declare(strict_types = 1);
2+
namespace Noname\Common;
3+
4+
/**
5+
* Class Str
6+
*
7+
* @package Noname\Common
8+
*/
9+
class Str
10+
{
11+
/**
12+
* Checks if string starts with given prefix.
13+
*
14+
* By default this method is case-sensitive.
15+
*
16+
* @param string $string
17+
* @param string $prefix
18+
* @param bool $caseSensitive
19+
* @return bool
20+
*/
21+
public static function startsWith(string $string, string $prefix, bool $caseSensitive = true): bool
22+
{
23+
if ($caseSensitive) {
24+
return strpos($string, $prefix) === 0;
25+
} else {
26+
return stripos($string, $prefix) === 0;
27+
}
28+
}
29+
30+
/**
31+
* Checks if string ends with given prefix.
32+
*
33+
* By default this method is case-sensitive.
34+
*
35+
* @param string $string
36+
* @param string $suffix
37+
* @param bool $caseSensitive
38+
* @return bool
39+
*/
40+
public static function endsWith(string $string, string $suffix, bool $caseSensitive = true): bool
41+
{
42+
$ending = strlen($string)-strlen($suffix);
43+
44+
if ($caseSensitive) {
45+
return strrpos($string, $suffix) === $ending;
46+
} else {
47+
return strripos($string, $suffix) === $ending;
48+
}
49+
}
50+
51+
/**
52+
* Checks if two strings equal each other.
53+
*
54+
* By default this method is case-sensitive.
55+
*
56+
* @param string $a
57+
* @param string $b
58+
* @param bool $caseSensitive
59+
* @return bool
60+
*/
61+
public static function equals(string $a, string $b, bool $caseSensitive = true): bool
62+
{
63+
if ($caseSensitive) {
64+
return $a === $b;
65+
} else {
66+
return strtolower($a) === strtolower($b);
67+
}
68+
}
69+
}

tests/StrTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php declare(strict_types=1);
2+
namespace Noname\Common;
3+
4+
class StrTest extends \PHPUnit_Framework_TestCase
5+
{
6+
/**
7+
* @covers Str::testStartsWith
8+
*/
9+
public function testStartsWith()
10+
{
11+
$caseSensitiveTrue = Str::startsWith('Case-sensitive', 'C');
12+
$caseSensitiveFalse = Str::startsWith('Case-sensitive', 'c');
13+
$caseInsensitive = Str::startsWith('case-insensitive', 'C', false);
14+
15+
$this->assertTrue($caseSensitiveTrue);
16+
$this->assertFalse($caseSensitiveFalse);
17+
$this->assertTrue($caseInsensitive);
18+
}
19+
20+
/**
21+
* @covers Str::testEndsWidth
22+
*/
23+
public function testEndsWidth()
24+
{
25+
$caseSensitiveTrue = Str::endsWith('sensitive-End', 'End');
26+
$caseSensitiveFalse = Str::endsWith('sensitive-End', 'end');
27+
$caseInsensitive = Str::endsWith('insensitive-end', 'End', false);
28+
29+
$this->assertTrue($caseSensitiveTrue);
30+
$this->assertFalse($caseSensitiveFalse);
31+
$this->assertTrue($caseInsensitive);
32+
}
33+
34+
/**
35+
* @covers Str::equals
36+
*/
37+
public function testEquals()
38+
{
39+
$caseSensitiveTrue = Str::equals('case-sensitive', 'case-sensitive');
40+
$caseSensitiveFalse = Str::equals('case-sensitive', 'Case-sensitive');
41+
$caseInsensitive = Str::equals('case-insensitive', 'Case-insensitive', false);
42+
43+
$this->assertTrue($caseSensitiveTrue);
44+
$this->assertFalse($caseSensitiveFalse);
45+
$this->assertTrue($caseInsensitive);
46+
}
47+
}

0 commit comments

Comments
 (0)