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 Contains |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Returns true if the string contains $needle, false otherwise. |
20
|
|
|
* |
21
|
|
|
* By default the comparison is case-sensitive, but can be made |
22
|
|
|
* insensitive by setting $caseSensitive to false. |
23
|
|
|
* |
24
|
|
|
* @param string $needle Substring to look for. |
25
|
|
|
* |
26
|
|
|
* @param bool $caseSensitive Whether or not to enforce case-sensitivity. |
27
|
|
|
* |
28
|
|
|
* @return bool Whether or not $str contains $needle. |
29
|
|
|
*/ |
30
|
|
|
public function contains($needle, $caseSensitive = true) |
31
|
|
|
{ |
32
|
|
|
if ($caseSensitive) |
33
|
|
|
{ |
34
|
|
|
return (UTF8::strpos($this->scalarString, $needle, 0, $this->encoding) !== false); |
35
|
|
|
} |
36
|
|
|
else |
37
|
|
|
{ |
38
|
|
|
return (UTF8::stripos($this->scalarString, $needle, 0, $this->encoding) !== false); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns true if the string contains all $needles, false otherwise. |
44
|
|
|
* |
45
|
|
|
* By default the comparison is case-sensitive, but can be made |
46
|
|
|
* insensitive by setting $caseSensitive to false. |
47
|
|
|
* |
48
|
|
|
* @param array $needles SubStrings to look for. |
49
|
|
|
* |
50
|
|
|
* @param bool $caseSensitive Whether or not to enforce case-sensitivity. |
51
|
|
|
* |
52
|
|
|
* @return bool Whether or not $str contains $needle. |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function containsAll($needles, $caseSensitive = true) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
if (empty($needles)) return false; |
57
|
|
|
|
58
|
|
|
foreach ($needles as $needle) |
59
|
|
|
{ |
60
|
|
|
if (!$this->contains($needle, $caseSensitive)) return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return true; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns true if the string contains any $needles, false otherwise. |
68
|
|
|
* |
69
|
|
|
* By default the comparison is case-sensitive, but can be made |
70
|
|
|
* insensitive by setting $caseSensitive to false. |
71
|
|
|
* |
72
|
|
|
* @param array $needles SubStrings to look for. |
73
|
|
|
* |
74
|
|
|
* @param bool $caseSensitive Whether or not to enforce case-sensitivity. |
75
|
|
|
* |
76
|
|
|
* @return bool Whether or not $str contains $needle. |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function containsAny($needles, $caseSensitive = true) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
if (empty($needles)) return false; |
81
|
|
|
|
82
|
|
|
foreach ($needles as $needle) |
83
|
|
|
{ |
84
|
|
|
if ($this->contains($needle, $caseSensitive)) return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
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.