Completed
Push — master ( dde30f...3b3524 )
by Cristiano
02:10
created

SearchPart::indexOf()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 3
rs 10
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the Phootwork package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @license MIT License
8
 * @copyright Thomas Gossmann
9
 */
10
namespace phootwork\lang\parts;
11
12
use phootwork\lang\ArrayObject;
13
use phootwork\lang\Text;
14
15
/**
16
 * Text searching methods
17
 *
18
 * @author ThomasGossmann
19
 * @author Cristiano Cinotti
20
 */
21
trait SearchPart {
22
	abstract protected function getString(): string;
23
24
	abstract public function length(): int;
25
26
	abstract protected function prepareOffset(int $offset): int;
27
28
	/**
29
	 * Returns the character at the given zero-related index
30
	 *
31
	 * <code>
32
	 * $str = new Text('Hello World!');<br>
33
	 * $str->at(6); // W
34
	 *
35
	 * $str = new Text('いちりんしゃ');<br>
36
	 * $str->at(4) // し
37
	 * </code>
38
	 *
39
	 * @param int $index zero-related index
40
	 *
41
	 * @return string the found character
42
	 */
43 2
	public function at(int $index): string {
44 2
		return mb_substr($this->getString(), $index, 1, $this->encoding);
45
	}
46
47
	/**
48
	 * Returns an ArrayObject consisting of the characters in the string.
49
	 *
50
	 * @return ArrayObject An ArrayObject of all chars
51
	 */
52 1
	public function chars(): ArrayObject {
53 1
		$chars = new ArrayObject();
54 1
		for ($i = 0, $l = $this->length(); $i < $l; $i++) {
55 1
			$chars->append($this->at($i));
56
		}
57
58 1
		return $chars;
59
	}
60
61
	/**
62
	 * Returns the index of a given string, starting at the optional zero-related offset
63
	 *
64
	 * @param string|Text $string
65
	 * @param int $offset zero-related offset
66
	 *
67
	 * @return int|null int for the index or null if the given string doesn't occur
68
	 */
69 16
	public function indexOf($string, int $offset = 0): ?int {
70 16
		$offset = $this->prepareOffset($offset);
71 16
		if ($string == '') {
72 1
			return $offset;
73
		}
74 15
		$output = mb_strpos($this->getString(), (string) $string, $offset, $this->encoding);
75
76 15
		return false === $output ? null : $output;
77
	}
78
79
	/**
80
	 * Returns the last index of a given string, starting at the optional offset
81
	 *
82
	 * @param string $string
83
	 * @param int $offset
84
	 *
85
	 * @return int|null int for the index or null if the given string doesn't occur
86
	 */
87 1
	public function lastIndexOf(string $string, ?int $offset = null): ?int {
88 1
		if (null === $offset) {
89 1
			$offset = $this->length();
90
		} else {
91 1
			$offset = $this->prepareOffset($offset);
92
		}
93
94 1
		if ($string === '') {
95 1
			return $offset;
96
		}
97
98
		/* Converts $offset to a negative offset as strrpos has a different
99
		 * behavior for positive offsets. */
100 1
		$output = mb_strrpos($this->getString(), (string) $string, $offset - $this->length(), $this->encoding);
101
102 1
		return false === $output ? null : $output;
103
	}
104
105
	/**
106
	 * Checks whether the string starts with the given string. Case sensitive!
107
	 *
108
	 * @see Text::startsWithIgnoreCase()
109
	 *
110
	 * @param string|Text $substring The substring to look for
111
	 *
112
	 * @return bool
113
	 */
114 6
	public function startsWith($substring): bool {
115 6
		$substringLength = mb_strlen((string) $substring, $this->encoding);
116 6
		$startOfStr = mb_substr($this->getString(), 0, $substringLength, $this->encoding);
117
118 6
		return (string) $substring === $startOfStr;
119
	}
120
121
	/**
122
	 * Checks whether the string starts with the given string. Ignores case.
123
	 *
124
	 * @see Text::startsWith()
125
	 *
126
	 * @param string|Text $substring The substring to look for
127
	 *
128
	 * @return bool
129
	 */
130 1
	public function startsWithIgnoreCase($substring): bool {
131 1
		$substring = mb_strtolower((string) $substring, $this->encoding);
132 1
		$substringLength = mb_strlen($substring, $this->encoding);
133 1
		$startOfStr = mb_strtolower(mb_substr($this->getString(), 0, $substringLength, $this->encoding));
134
135 1
		return (string) $substring === $startOfStr;
136
	}
137
138
	/**
139
	 * Checks whether the string ends with the given string. Case sensitive!
140
	 *
141
	 * @see Text::endsWithIgnoreCase()
142
	 *
143
	 * @param string|Text $substring The substring to look for
144
	 *
145
	 * @return bool
146
	 */
147 10
	public function endsWith($substring): bool {
148 10
		$substringLength = mb_strlen((string) $substring, $this->encoding);
149 10
		$endOfStr = mb_substr($this->getString(), $this->length() - $substringLength, $substringLength, $this->encoding);
150
151 10
		return (string) $substring === $endOfStr;
152
	}
153
154
	/**
155
	 * Checks whether the string ends with the given string. Ingores case.
156
	 *
157
	 * @see Text::endsWith()
158
	 *
159
	 * @param string|Text $substring The substring to look for
160
	 *
161
	 * @return bool
162
	 */
163 1
	public function endsWithIgnoreCase($substring): bool {
164 1
		$substring = mb_strtolower((string) $substring, $this->encoding);
165 1
		$substringLength = mb_strlen($substring, $this->encoding);
166 1
		$endOfStr = mb_strtolower(mb_substr($this->getString(), $this->length() - $substringLength, $substringLength, $this->encoding));
167
168 1
		return (string) $substring === $endOfStr;
169
	}
170
171
	/**
172
	 * Checks whether the given string occurs
173
	 *
174
	 * @param string|Text $string
175
	 *
176
	 * @return bool
177
	 */
178 4
	public function contains($string): bool {
179 4
		return $this->indexOf($string) !== null;
180
	}
181
182
	/**
183
	 * Performs a regular expression matching with the given regexp
184
	 *
185
	 * @param string $regexp
186
	 *
187
	 * @return bool
188
	 */
189 20
	public function match(string $regexp): bool {
190 20
		return (bool) preg_match($regexp, $this->getString());
191
	}
192
}
193