Skip to content

Commit e0ea40e

Browse files
author
Dogan Ucar
committed
has prefix
1 parent 068ed13 commit e0ea40e

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"require": {
1818
"doganoo/php-algorithms": "*",
1919
"apache/log4php": "2.3.0",
20-
"ext-fileinfo": "*"
20+
"ext-fileinfo": "*",
21+
"ext-iconv": "*",
22+
"ext-mbstring": "*"
2123
},
2224
"require-dev": {
2325
"phpunit/phpunit": "6.5"

src/Datatype/StringClass.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,16 @@ public function getSubstring(int $count) {
181181
$return = substr($this->value, 0, $count);
182182
return $return;
183183
}
184+
185+
/**
186+
* checks a prefix
187+
*
188+
* have a look here: https://stackoverflow.com/a/2790919
189+
*
190+
* @param string $prefix
191+
* @return bool
192+
*/
193+
public function hasPrefix(string $prefix):bool {
194+
return true === (substr( $this->getValue(), 0, strlen($prefix)) === $prefix);
195+
}
184196
}

src/Util/StringUtil.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*/
2121

2222
namespace doganoo\PHPUtil\Util;
23+
use doganoo\PHPUtil\Datatype\StringClass;
24+
2325
/**
2426
* Class StringUtil
2527
*
@@ -86,4 +88,14 @@ public static function toUTF8(string $string): string {
8688
public static function getEncoding(string $string): string {
8789
return \mb_detect_encoding($string, "auto", true);
8890
}
91+
92+
/**
93+
* @param string $string
94+
* @param string $prefix
95+
* @return bool
96+
*/
97+
public static function hasPrefix(string $string, string $prefix):bool {
98+
$stringClass = new StringClass($string);
99+
return $stringClass->hasPrefix($prefix);
100+
}
89101
}

test/Util/StringUtilTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* MIT License
4+
*
5+
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
use doganoo\PHPUtil\Datatype\StringClass;
27+
use PHPUnit\Framework\TestCase;
28+
29+
/**
30+
* Class StringUtilTest
31+
*/
32+
class StringUtilTest extends TestCase {
33+
34+
/**
35+
* test prefix
36+
*/
37+
public function testPrefix(){
38+
$string = new StringClass("doganucar");
39+
40+
$this->assertTrue(true === $string->hasPrefix("dogan"));
41+
$this->assertTrue(false === $string->hasPrefix("ucar"));
42+
43+
}
44+
45+
}

0 commit comments

Comments
 (0)