1
|
|
|
<?php namespace Gears\String\Methods; |
2
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
3
|
|
|
// __________ __ ________ __________ |
4
|
|
|
// \______ \ |__ ______ / _____/ ____ _____ ______\______ \ _______ ___ |
5
|
|
|
// | ___/ | \\____ \/ \ ____/ __ \\__ \\_ __ \ | _// _ \ \/ / |
6
|
|
|
// | | | Y \ |_> > \_\ \ ___/ / __ \| | \/ | ( <_> > < |
7
|
|
|
// |____| |___| / __/ \______ /\___ >____ /__| |______ /\____/__/\_ \ |
8
|
|
|
// \/|__| \/ \/ \/ \/ \/ |
9
|
|
|
// ----------------------------------------------------------------------------- |
10
|
|
|
// Designed and Developed by Brad Jones <brad @="bjc.id.au" /> |
11
|
|
|
// ----------------------------------------------------------------------------- |
12
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
13
|
|
|
|
14
|
|
|
use voku\helper\UTF8; |
15
|
|
|
|
16
|
|
|
trait StartEndWith |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Returns true if the string begins with $substring, false otherwise. |
20
|
|
|
* |
21
|
|
|
* By default, the comparison is case-sensitive, |
22
|
|
|
* but can be made insensitive by setting $caseSensitive |
23
|
|
|
* to false. |
24
|
|
|
* |
25
|
|
|
* @param string $substring The substring to look for |
26
|
|
|
* @param bool $caseSensitive Whether or not to enforce case-sensitivity |
27
|
|
|
* |
28
|
|
|
* @return bool Whether or not $str starts with $substring |
29
|
|
|
*/ |
30
|
|
|
public function startsWith($substring, $caseSensitive = true) |
31
|
|
|
{ |
32
|
|
|
$startOfStr = UTF8::substr |
33
|
|
|
( |
34
|
|
|
$this->scalarString, |
35
|
|
|
0, |
36
|
|
|
UTF8::strlen($substring, $this->encoding), |
37
|
|
|
$this->encoding |
38
|
|
|
); |
39
|
|
|
|
40
|
|
View Code Duplication |
if (!$caseSensitive) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$substring = UTF8::strtolower($substring, $this->encoding); |
43
|
|
|
$startOfStr = UTF8::strtolower($startOfStr, $this->encoding); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return (string)$substring === $startOfStr; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns true if the string ends with $substring, false otherwise. By |
51
|
|
|
* default, the comparison is case-sensitive, but can be made insensitive |
52
|
|
|
* by setting $caseSensitive to false. |
53
|
|
|
* |
54
|
|
|
* @param string $substring The substring to look for |
55
|
|
|
* @param bool $caseSensitive Whether or not to enforce case-sensitivity |
56
|
|
|
* |
57
|
|
|
* @return bool Whether or not $str ends with $substring |
58
|
|
|
*/ |
59
|
|
|
public function endsWith($substring, $caseSensitive = true) |
60
|
|
|
{ |
61
|
|
|
$substringLength = UTF8::strlen($substring, $this->encoding); |
62
|
|
|
|
63
|
|
|
$endOfStr = UTF8::substr |
64
|
|
|
( |
65
|
|
|
$this->scalarString, |
66
|
|
|
$this->getLength() - $substringLength, |
67
|
|
|
$substringLength, |
68
|
|
|
$this->encoding |
69
|
|
|
); |
70
|
|
|
|
71
|
|
View Code Duplication |
if (!$caseSensitive) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$substring = UTF8::strtolower($substring, $this->encoding); |
74
|
|
|
$endOfStr = UTF8::strtolower($endOfStr, $this->encoding); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return (string)$substring === $endOfStr; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.