| Total Complexity | 64 |
| Total Lines | 684 |
| Duplicated Lines | 0 % |
| Coverage | 99.47% |
| Changes | 0 | ||
Complex classes like Str often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Str, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Str implements PrimitiveInterface, StringableInterface |
||
| 10 | { |
||
| 11 | const PAD_RIGHT = STR_PAD_RIGHT; |
||
| 12 | const PAD_LEFT = STR_PAD_LEFT; |
||
| 13 | const PAD_BOTH = STR_PAD_BOTH; |
||
| 14 | const PREG_NO_FLAGS = 0; |
||
| 15 | const PREG_SPLIT_NO_EMPTY = PREG_SPLIT_NO_EMPTY; |
||
| 16 | const PREG_SPLIT_DELIM_CAPTURE = PREG_SPLIT_DELIM_CAPTURE; |
||
| 17 | const PREG_SPLIT_OFFSET_CAPTURE = PREG_SPLIT_OFFSET_CAPTURE; |
||
| 18 | const PREG_OFFSET_CAPTURE = PREG_OFFSET_CAPTURE; |
||
| 19 | |||
| 20 | private $value; |
||
| 21 | private $encoding; |
||
| 22 | |||
| 23 | 483 | public function __construct(string $value, string $encoding = null) |
|
| 24 | { |
||
| 25 | 483 | $this->value = $value; |
|
| 26 | 483 | $this->encoding = $encoding ?? \mb_internal_encoding(); |
|
| 27 | 483 | } |
|
| 28 | |||
| 29 | 6 | public static function of(string $value, string $encoding = null): self |
|
| 30 | { |
||
| 31 | 6 | return new self($value, $encoding); |
|
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * {@inheritdoc} |
||
| 36 | */ |
||
| 37 | 3 | public function toPrimitive() |
|
| 38 | { |
||
| 39 | 3 | return $this->value; |
|
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * {@inheritdoc} |
||
| 44 | */ |
||
| 45 | 318 | public function __toString(): string |
|
| 46 | { |
||
| 47 | 318 | return $this->value; |
|
| 48 | } |
||
| 49 | |||
| 50 | 9 | public function encoding(): self |
|
| 51 | { |
||
| 52 | 9 | return new self($this->encoding); |
|
|
1 ignored issue
–
show
|
|||
| 53 | } |
||
| 54 | |||
| 55 | 45 | public function toEncoding(string $encoding): self |
|
| 56 | { |
||
| 57 | 45 | return new self($this->value, $encoding); |
|
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Split the string into a collection of ones |
||
| 62 | * |
||
| 63 | * @param string $delimiter |
||
| 64 | * |
||
| 65 | * @return StreamInterface<self> |
||
| 66 | */ |
||
| 67 | 21 | public function split(string $delimiter = null): StreamInterface |
|
| 68 | { |
||
| 69 | 21 | if (\is_null($delimiter) || $delimiter === '') { |
|
| 70 | 9 | return $this->chunk(); |
|
| 71 | } |
||
| 72 | |||
| 73 | 15 | $parts = \explode($delimiter, $this->value); |
|
| 74 | 15 | $stream = new Stream(self::class); |
|
| 75 | |||
| 76 | 15 | foreach ($parts as $part) { |
|
| 77 | 15 | $stream = $stream->add(new self($part, $this->encoding)); |
|
|
1 ignored issue
–
show
|
|||
| 78 | } |
||
| 79 | |||
| 80 | 15 | return $stream; |
|
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Returns a collection of the string splitted by the given chunk size |
||
| 85 | * |
||
| 86 | * @param int $size |
||
| 87 | * |
||
| 88 | * @return StreamInterface<self> |
||
| 89 | */ |
||
| 90 | 21 | public function chunk(int $size = 1): StreamInterface |
|
| 91 | { |
||
| 92 | 21 | $stream = new Stream(self::class); |
|
| 93 | 21 | $string = $this; |
|
| 94 | |||
| 95 | 21 | while ($string->length() > 0) { |
|
| 96 | 21 | $stream = $stream->add($string->substring(0, $size)); |
|
| 97 | 21 | $string = $string->substring($size); |
|
| 98 | } |
||
| 99 | |||
| 100 | 21 | return $stream; |
|
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Returns the position of the first occurence of the string |
||
| 105 | * |
||
| 106 | * @param string $needle |
||
| 107 | * @param int $offset |
||
| 108 | * |
||
| 109 | * @throws SubstringException If the string is not found |
||
| 110 | * |
||
| 111 | * @return int |
||
| 112 | */ |
||
| 113 | 18 | public function position(string $needle, int $offset = 0): int |
|
| 114 | { |
||
| 115 | 18 | $position = \mb_strpos($this->value, $needle, $offset, $this->encoding); |
|
| 116 | |||
| 117 | 18 | if ($position === false) { |
|
|
1 ignored issue
–
show
|
|||
| 118 | 9 | throw new SubstringException(\sprintf( |
|
| 119 | 9 | 'Substring "%s" not found', |
|
| 120 | 9 | $needle |
|
| 121 | )); |
||
| 122 | } |
||
| 123 | |||
| 124 | 15 | return (int) $position; |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Replace all occurences of the search string with the replacement one |
||
| 129 | * |
||
| 130 | * @param string $search |
||
| 131 | * @param string $replacement |
||
| 132 | * |
||
| 133 | * @return self |
||
| 134 | */ |
||
| 135 | 6 | public function replace(string $search, string $replacement): self |
|
| 136 | { |
||
| 137 | 6 | if (!$this->contains($search)) { |
|
| 138 | 3 | return $this; |
|
| 139 | } |
||
| 140 | |||
| 141 | return $this |
||
| 142 | 6 | ->split($search) |
|
| 143 | 6 | ->join($replacement); |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Returns the string following the given delimiter |
||
| 148 | * |
||
| 149 | * @param string $delimiter |
||
| 150 | * |
||
| 151 | * @throws SubstringException If the string is not found |
||
| 152 | * |
||
| 153 | * @return self |
||
| 154 | */ |
||
| 155 | 9 | public function str(string $delimiter): self |
|
| 156 | { |
||
| 157 | 9 | $sub = \mb_strstr($this->value, $delimiter, false, $this->encoding); |
|
| 158 | |||
| 159 | 9 | if ($sub === false) { |
|
|
1 ignored issue
–
show
|
|||
| 160 | 3 | throw new SubstringException(\sprintf( |
|
| 161 | 3 | 'Substring "%s" not found', |
|
| 162 | 3 | $delimiter |
|
| 163 | )); |
||
| 164 | } |
||
| 165 | |||
| 166 | 6 | return new self($sub, $this->encoding); |
|
|
1 ignored issue
–
show
|
|||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Return the string in upper case |
||
| 171 | * |
||
| 172 | * @return self |
||
| 173 | */ |
||
| 174 | 9 | public function toUpper(): self |
|
| 175 | { |
||
| 176 | 9 | return new self(\mb_strtoupper($this->value), $this->encoding); |
|
|
1 ignored issue
–
show
|
|||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Return the string in lower case |
||
| 181 | * |
||
| 182 | * @return self |
||
| 183 | */ |
||
| 184 | 6 | public function toLower(): self |
|
| 185 | { |
||
| 186 | 6 | return new self(\mb_strtolower($this->value), $this->encoding); |
|
|
1 ignored issue
–
show
|
|||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Return the string length |
||
| 191 | * |
||
| 192 | * @return int |
||
| 193 | */ |
||
| 194 | 45 | public function length(): int |
|
| 195 | { |
||
| 196 | 45 | return \mb_strlen($this->value, $this->encoding); |
|
| 197 | } |
||
| 198 | |||
| 199 | 3 | public function empty(): bool |
|
| 200 | { |
||
| 201 | 3 | return $this->value === ''; |
|
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Reverse the string |
||
| 206 | * |
||
| 207 | * @return self |
||
| 208 | */ |
||
| 209 | 3 | public function reverse(): self |
|
| 210 | { |
||
| 211 | return $this |
||
| 212 | 3 | ->chunk() |
|
| 213 | 3 | ->reverse() |
|
| 214 | 3 | ->join(''); |
|
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Pad to the right |
||
| 219 | * |
||
| 220 | * @param int $length |
||
| 221 | * @param string $character |
||
| 222 | * |
||
| 223 | * @return self |
||
| 224 | */ |
||
| 225 | 3 | public function rightPad(int $length, string $character = ' '): self |
|
| 226 | { |
||
| 227 | 3 | return $this->pad($length, $character, self::PAD_RIGHT); |
|
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Pad to the left |
||
| 232 | * |
||
| 233 | * @param int $length |
||
| 234 | * @param string $character |
||
| 235 | * |
||
| 236 | * @return self |
||
| 237 | */ |
||
| 238 | 3 | public function leftPad(int $length, string $character = ' '): self |
|
| 239 | { |
||
| 240 | 3 | return $this->pad($length, $character, self::PAD_LEFT); |
|
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Pad both sides |
||
| 245 | * |
||
| 246 | * @param int $length |
||
| 247 | * @param string $character |
||
| 248 | * |
||
| 249 | * @return self |
||
| 250 | */ |
||
| 251 | 3 | public function uniPad(int $length, string $character = ' '): self |
|
| 252 | { |
||
| 253 | 3 | return $this->pad($length, $character, self::PAD_BOTH); |
|
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Find length of initial segment not matching mask |
||
| 258 | * |
||
| 259 | * @param string $mask |
||
| 260 | * @param int $start |
||
| 261 | * @param int $length |
||
| 262 | * |
||
| 263 | * @return int |
||
| 264 | */ |
||
| 265 | 3 | public function cspn(string $mask, int $start = 0, int $length = null): int |
|
| 266 | { |
||
| 267 | 3 | if ($length === null) { |
|
| 268 | 3 | $value = \strcspn($this->value, $mask, $start); |
|
| 269 | } else { |
||
| 270 | 3 | $value = \strcspn( |
|
| 271 | 3 | $this->value, |
|
| 272 | 3 | $mask, |
|
| 273 | 3 | $start, |
|
| 274 | 3 | $length |
|
| 275 | ); |
||
| 276 | } |
||
| 277 | |||
| 278 | 3 | return (int) $value; |
|
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Repeat the string n times |
||
| 283 | * |
||
| 284 | * @param int $repeat |
||
| 285 | * |
||
| 286 | * @return self |
||
| 287 | */ |
||
| 288 | 3 | public function repeat(int $repeat): self |
|
| 289 | { |
||
| 290 | 3 | return new self(\str_repeat($this->value, $repeat), $this->encoding); |
|
|
1 ignored issue
–
show
|
|||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Shuffle the string |
||
| 295 | * |
||
| 296 | * @return self |
||
| 297 | */ |
||
| 298 | 6 | public function shuffle(): self |
|
| 299 | { |
||
| 300 | 6 | $parts = $this->chunk()->toPrimitive(); |
|
| 301 | 6 | \shuffle($parts); |
|
| 302 | |||
| 303 | 6 | return new self(\implode('', $parts), $this->encoding); |
|
|
1 ignored issue
–
show
|
|||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Strip slashes |
||
| 308 | * |
||
| 309 | * @return self |
||
| 310 | */ |
||
| 311 | 3 | public function stripSlashes(): self |
|
| 312 | { |
||
| 313 | 3 | return new self(\stripslashes($this->value), $this->encoding); |
|
|
1 ignored issue
–
show
|
|||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Strip C-like slashes |
||
| 318 | * |
||
| 319 | * @return self |
||
| 320 | */ |
||
| 321 | 3 | public function stripCSlashes(): self |
|
| 322 | { |
||
| 323 | 3 | return new self(\stripcslashes($this->value), $this->encoding); |
|
|
1 ignored issue
–
show
|
|||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Return the word count |
||
| 328 | * |
||
| 329 | * @param string $charlist |
||
| 330 | * |
||
| 331 | * @return int |
||
| 332 | */ |
||
| 333 | 3 | public function wordCount(string $charlist = ''): int |
|
| 334 | { |
||
| 335 | 3 | return (int) \str_word_count( |
|
| 336 | 3 | $this->value, |
|
| 337 | 3 | 0, |
|
| 338 | 3 | $charlist |
|
| 339 | ); |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Return the collection of words |
||
| 344 | * |
||
| 345 | * @param string $charlist |
||
| 346 | * |
||
| 347 | * @return MapInterface<int, self> |
||
| 348 | */ |
||
| 349 | 3 | public function words(string $charlist = ''): MapInterface |
|
| 350 | { |
||
| 351 | 3 | $words = \str_word_count($this->value, 2, $charlist); |
|
| 352 | 3 | $map = new Map('int', self::class); |
|
| 353 | |||
| 354 | 3 | foreach ($words as $position => $word) { |
|
| 355 | 3 | $map = $map->put($position, new self($word, $this->encoding)); |
|
|
1 ignored issue
–
show
|
|||
| 356 | } |
||
| 357 | |||
| 358 | 3 | return $map; |
|
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Split the string using a regular expression |
||
| 363 | * |
||
| 364 | * @param string $regex |
||
| 365 | * @param int $limit |
||
| 366 | * |
||
| 367 | * @return StreamInterface<self> |
||
| 368 | */ |
||
| 369 | 6 | public function pregSplit(string $regex, int $limit = -1): StreamInterface |
|
| 370 | { |
||
| 371 | 6 | $strings = \preg_split($regex, $this->value, $limit); |
|
| 372 | 6 | $stream = new Stream(self::class); |
|
| 373 | |||
| 374 | 6 | foreach ($strings as $string) { |
|
| 375 | 6 | $stream = $stream->add(new self($string, $this->encoding)); |
|
|
1 ignored issue
–
show
|
|||
| 376 | } |
||
| 377 | |||
| 378 | 6 | return $stream; |
|
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Check if the string match the given regular expression |
||
| 383 | * |
||
| 384 | * @param string $regex |
||
| 385 | * @param int $offset |
||
| 386 | * |
||
| 387 | * @throws Exception If the regex failed |
||
| 388 | * |
||
| 389 | * @return bool |
||
| 390 | */ |
||
| 391 | 6 | public function matches(string $regex, int $offset = 0): bool |
|
| 392 | { |
||
| 393 | 6 | $matches = []; |
|
| 394 | 6 | $value = \preg_match($regex, $this->value, $matches, 0, $offset); |
|
| 395 | |||
| 396 | 6 | if ($value === false) { |
|
|
1 ignored issue
–
show
|
|||
| 397 | 3 | throw new RegexException('', \preg_last_error()); |
|
| 398 | } |
||
| 399 | |||
| 400 | 3 | return (bool) $value; |
|
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Return a collection of the elements matching the regex |
||
| 405 | * |
||
| 406 | * @deprecated replaced by self::capture, to be removed in 3.0 |
||
| 407 | * |
||
| 408 | * @param string $regex |
||
| 409 | * @param int $offset |
||
| 410 | * @param int $flags |
||
| 411 | * |
||
| 412 | * @throws Exception If the regex failed |
||
| 413 | * |
||
| 414 | * @return MapInterface<scalar, self> |
||
| 415 | */ |
||
| 416 | 3 | public function getMatches( |
|
| 417 | string $regex, |
||
| 418 | int $offset = 0, |
||
| 419 | int $flags = self::PREG_NO_FLAGS |
||
| 420 | ): MapInterface { |
||
| 421 | 3 | return $this->capture($regex, $offset, $flags); |
|
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Return a collection of the elements matching the regex |
||
| 426 | * |
||
| 427 | * @param string $regex |
||
| 428 | * @param int $offset |
||
| 429 | * @param int $flags |
||
| 430 | * |
||
| 431 | * @throws Exception If the regex failed |
||
| 432 | * |
||
| 433 | * @return MapInterface<scalar, self> |
||
| 434 | */ |
||
| 435 | 9 | public function capture( |
|
| 436 | string $regex, |
||
| 437 | int $offset = 0, |
||
| 438 | int $flags = self::PREG_NO_FLAGS |
||
| 439 | ): MapInterface { |
||
| 440 | 9 | $matches = []; |
|
| 441 | 9 | $value = \preg_match( |
|
| 442 | 9 | $regex, |
|
| 443 | 9 | $this->value, |
|
| 444 | 3 | $matches, |
|
| 445 | 9 | $flags, |
|
| 446 | 9 | $offset |
|
| 447 | ); |
||
| 448 | 9 | $map = new Map('scalar', self::class); |
|
| 449 | |||
| 450 | 9 | foreach ($matches as $key => $match) { |
|
| 451 | 6 | $map = $map->put($key, new self((string) $match, $this->encoding)); |
|
|
1 ignored issue
–
show
|
|||
| 452 | } |
||
| 453 | |||
| 454 | 9 | if ($value === false) { |
|
|
1 ignored issue
–
show
|
|||
| 455 | 3 | throw new RegexException('', \preg_last_error()); |
|
| 456 | } |
||
| 457 | |||
| 458 | 6 | return $map; |
|
| 459 | } |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Replace part of the string by using a regular expression |
||
| 463 | * |
||
| 464 | * @param string $regex |
||
| 465 | * @param string $replacement |
||
| 466 | * @param int $limit |
||
| 467 | * |
||
| 468 | * @throws Exception If the regex failed |
||
| 469 | * |
||
| 470 | * @return self |
||
| 471 | */ |
||
| 472 | 3 | public function pregReplace( |
|
| 473 | string $regex, |
||
| 474 | string $replacement, |
||
| 475 | int $limit = -1 |
||
| 476 | ): self { |
||
| 477 | 3 | $value = \preg_replace( |
|
| 478 | 3 | $regex, |
|
| 479 | 3 | $replacement, |
|
| 480 | 3 | $this->value, |
|
| 481 | 3 | $limit |
|
| 482 | ); |
||
| 483 | |||
| 484 | 3 | if ($value === null) { |
|
|
1 ignored issue
–
show
|
|||
| 485 | throw new RegexException('', \preg_last_error()); |
||
| 486 | } |
||
| 487 | |||
| 488 | 3 | return new self($value, $this->encoding); |
|
|
1 ignored issue
–
show
|
|||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Return part of the string |
||
| 493 | * |
||
| 494 | * @param int $start |
||
| 495 | * @param int $length |
||
| 496 | * |
||
| 497 | * @return self |
||
| 498 | */ |
||
| 499 | 39 | public function substring(int $start, int $length = null): self |
|
| 500 | { |
||
| 501 | 39 | if ($this->length() === 0) { |
|
| 502 | 3 | return $this; |
|
| 503 | } |
||
| 504 | |||
| 505 | 39 | $sub = \mb_substr($this->value, $start, $length, $this->encoding); |
|
| 506 | |||
| 507 | 39 | return new self($sub, $this->encoding); |
|
|
1 ignored issue
–
show
|
|||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Return a formatted string |
||
| 512 | * |
||
| 513 | * @return self |
||
| 514 | */ |
||
| 515 | 3 | public function sprintf(...$values): self |
|
| 516 | { |
||
| 517 | 3 | return new self(\sprintf($this->value, ...$values), $this->encoding); |
|
|
1 ignored issue
–
show
|
|||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Return the string with the first letter as uppercase |
||
| 522 | * |
||
| 523 | * @return self |
||
| 524 | */ |
||
| 525 | 6 | public function ucfirst(): self |
|
| 526 | { |
||
| 527 | return $this |
||
| 528 | 6 | ->substring(0, 1) |
|
| 529 | 6 | ->toUpper() |
|
| 530 | 6 | ->append((string) $this->substring(1)); |
|
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Return the string with the first letter as lowercase |
||
| 535 | * |
||
| 536 | * @return self |
||
| 537 | */ |
||
| 538 | 3 | public function lcfirst(): self |
|
| 539 | { |
||
| 540 | return $this |
||
| 541 | 3 | ->substring(0, 1) |
|
| 542 | 3 | ->toLower() |
|
| 543 | 3 | ->append((string) $this->substring(1)); |
|
| 544 | } |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Return a CamelCase representation of the string |
||
| 548 | * |
||
| 549 | * @return self |
||
| 550 | */ |
||
| 551 | 3 | public function camelize(): self |
|
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Append a string at the end of the current one |
||
| 564 | * |
||
| 565 | * @param string $string |
||
| 566 | * |
||
| 567 | * @return self |
||
| 568 | */ |
||
| 569 | 12 | public function append(string $string): self |
|
| 570 | { |
||
| 571 | 12 | return new self((string) $this.$string, $this->encoding); |
|
|
1 ignored issue
–
show
|
|||
| 572 | } |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Prepend a string at the beginning of the current one |
||
| 576 | * |
||
| 577 | * @param string $string |
||
| 578 | * |
||
| 579 | * @return self |
||
| 580 | */ |
||
| 581 | 3 | public function prepend(string $string): self |
|
| 584 | } |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Check if the 2 strings are equal |
||
| 588 | * |
||
| 589 | * @param self $string |
||
| 590 | * |
||
| 591 | * @return bool |
||
| 592 | */ |
||
| 593 | 93 | public function equals(self $string): bool |
|
| 594 | { |
||
| 595 | 93 | return (string) $this === (string) $string; |
|
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Trim the string |
||
| 600 | * |
||
| 601 | * @param string $mask |
||
| 602 | * |
||
| 603 | * @return self |
||
| 604 | */ |
||
| 605 | 3 | public function trim(string $mask = null): self |
|
| 606 | { |
||
| 607 | 3 | return new self( |
|
| 608 | 3 | $mask === null ? \trim((string) $this) : \trim((string) $this, $mask), |
|
| 609 | 3 | $this->encoding |
|
|
1 ignored issue
–
show
|
|||
| 610 | ); |
||
| 611 | } |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Trim the right side of the string |
||
| 615 | * |
||
| 616 | * @param string $mask |
||
| 617 | * |
||
| 618 | * @return self |
||
| 619 | */ |
||
| 620 | 3 | public function rightTrim(string $mask = null): self |
|
| 621 | { |
||
| 622 | 3 | return new self( |
|
| 623 | 3 | $mask === null ? \rtrim((string) $this) : \rtrim((string) $this, $mask), |
|
| 624 | 3 | $this->encoding |
|
|
1 ignored issue
–
show
|
|||
| 625 | ); |
||
| 626 | } |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Trim the left side of the string |
||
| 630 | * |
||
| 631 | * @param string $mask |
||
| 632 | * |
||
| 633 | * @return self |
||
| 634 | */ |
||
| 635 | 3 | public function leftTrim(string $mask = null): self |
|
| 636 | { |
||
| 637 | 3 | return new self( |
|
| 638 | 3 | $mask === null ? \ltrim((string) $this) : \ltrim((string) $this, $mask), |
|
| 639 | 3 | $this->encoding |
|
|
1 ignored issue
–
show
|
|||
| 640 | ); |
||
| 641 | } |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Check if the given string is present in the current one |
||
| 645 | * |
||
| 646 | * @param string $value |
||
| 647 | * |
||
| 648 | * @return bool |
||
| 649 | */ |
||
| 650 | 9 | public function contains(string $value): bool |
|
| 651 | { |
||
| 652 | try { |
||
| 653 | 9 | $this->position($value); |
|
| 654 | |||
| 655 | 9 | return true; |
|
| 656 | 6 | } catch (SubstringException $e) { |
|
| 657 | 6 | return false; |
|
| 658 | } |
||
| 659 | } |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Quote regular expression characters |
||
| 663 | * |
||
| 664 | * @param string $delimiter |
||
| 665 | * |
||
| 666 | * @return self |
||
| 667 | */ |
||
| 668 | 3 | public function pregQuote(string $delimiter = ''): self |
|
| 671 | } |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Pad the string |
||
| 675 | * |
||
| 676 | * @param int $length |
||
| 677 | * @param string $character |
||
| 678 | * @param int $direction |
||
| 679 | * |
||
| 680 | * @return self |
||
| 681 | */ |
||
| 682 | 3 | private function pad( |
|
| 683 | int $length, |
||
| 684 | string $character = ' ', |
||
| 685 | int $direction = self::PAD_RIGHT |
||
| 686 | ): self { |
||
| 687 | 3 | return new self(\str_pad( |
|
| 688 | 3 | $this->value, |
|
| 689 | 3 | $length, |
|
| 690 | 3 | $character, |
|
| 691 | 3 | $direction |
|
| 693 | } |
||
| 694 | } |
||
| 695 |