Stringable.php 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Stringable;
  12. use ArrayAccess;
  13. use Closure;
  14. use Hyperf\Collection\Collection;
  15. use Hyperf\Conditionable\Conditionable;
  16. use Hyperf\Macroable\Macroable;
  17. use Hyperf\Tappable\Tappable;
  18. use JsonSerializable;
  19. use function Hyperf\Collection\collect;
  20. use function Hyperf\Support\class_basename;
  21. class Stringable implements JsonSerializable, \Stringable, ArrayAccess
  22. {
  23. use Conditionable;
  24. use Macroable;
  25. use Tappable;
  26. /**
  27. * The underlying string value.
  28. */
  29. protected string $value;
  30. /**
  31. * Create a new instance of the class.
  32. *
  33. * @param string $value
  34. */
  35. public function __construct($value = '')
  36. {
  37. $this->value = (string) $value;
  38. }
  39. /**
  40. * Proxy dynamic properties onto methods.
  41. *
  42. * @param string $key
  43. * @return mixed
  44. */
  45. public function __get($key)
  46. {
  47. return $this->{$key}();
  48. }
  49. /**
  50. * Get the raw string value.
  51. *
  52. * @return string
  53. */
  54. public function __toString()
  55. {
  56. return $this->value;
  57. }
  58. /**
  59. * Return the remainder of a string after the first occurrence of a given value.
  60. *
  61. * @param string $search
  62. * @return static
  63. */
  64. public function after($search)
  65. {
  66. return new static(Str::after($this->value, $search));
  67. }
  68. /**
  69. * Return the remainder of a string after the last occurrence of a given value.
  70. *
  71. * @param string $search
  72. * @return static
  73. */
  74. public function afterLast($search)
  75. {
  76. return new static(Str::afterLast($this->value, $search));
  77. }
  78. /**
  79. * Convert the given string to APA-style title case.
  80. *
  81. * @return static
  82. */
  83. public function apa()
  84. {
  85. return new static(Str::apa($this->value));
  86. }
  87. /**
  88. * Append the given values to the string.
  89. *
  90. * @param string $values
  91. * @return static
  92. */
  93. public function append(...$values)
  94. {
  95. return new static($this->value . implode('', $values));
  96. }
  97. /**
  98. * Transliterate a UTF-8 value to ASCII.
  99. *
  100. * @param string $language
  101. * @return static
  102. */
  103. public function ascii($language = 'en')
  104. {
  105. return new static(Str::ascii($this->value, $language));
  106. }
  107. /**
  108. * Get the trailing name component of the path.
  109. *
  110. * @param string $suffix
  111. * @return static
  112. */
  113. public function basename($suffix = '')
  114. {
  115. return new static(basename($this->value, $suffix));
  116. }
  117. /**
  118. * Get the character at the specified index.
  119. *
  120. * @param int $index
  121. * @return false|string
  122. */
  123. public function charAt($index)
  124. {
  125. return Str::charAt($this->value, $index);
  126. }
  127. /**
  128. * Remove the given string if it exists at the end of the current string.
  129. *
  130. * @param array|string $needle
  131. * @return static
  132. */
  133. public function chopEnd($needle)
  134. {
  135. return new static(Str::chopEnd($this->value, $needle));
  136. }
  137. /**
  138. * Remove the given string if it exists at the start of the current string.
  139. *
  140. * @param array|string $needle
  141. * @return static
  142. */
  143. public function chopStart($needle)
  144. {
  145. return new static(Str::chopStart($this->value, $needle));
  146. }
  147. /**
  148. * Get the basename of the class path.
  149. *
  150. * @return static
  151. */
  152. public function classBasename()
  153. {
  154. return new static(class_basename($this->value));
  155. }
  156. /**
  157. * Get the portion of a string before the first occurrence of a given value.
  158. *
  159. * @param string $search
  160. * @return static
  161. */
  162. public function before($search)
  163. {
  164. return new static(Str::before($this->value, $search));
  165. }
  166. /**
  167. * Get the portion of a string before the last occurrence of a given value.
  168. *
  169. * @param string $search
  170. * @return static
  171. */
  172. public function beforeLast($search)
  173. {
  174. return new static(Str::beforeLast($this->value, $search));
  175. }
  176. /**
  177. * Get the portion of a string between two given values.
  178. *
  179. * @param string $from
  180. * @param string $to
  181. * @return static
  182. */
  183. public function between($from, $to)
  184. {
  185. return new static(Str::between($this->value, $from, $to));
  186. }
  187. /**
  188. * Convert a value to camel case.
  189. *
  190. * @return static
  191. */
  192. public function camel()
  193. {
  194. return new static(Str::camel($this->value));
  195. }
  196. /**
  197. * Determine if a given string contains a given substring.
  198. *
  199. * @param string|string[] $needles
  200. * @return bool
  201. */
  202. public function contains($needles)
  203. {
  204. return Str::contains($this->value, $needles);
  205. }
  206. /**
  207. * Determine if a given string contains all array values.
  208. *
  209. * @return bool
  210. */
  211. public function containsAll(array $needles)
  212. {
  213. return Str::containsAll($this->value, $needles);
  214. }
  215. /**
  216. * Get the parent directory's path.
  217. *
  218. * @param int $levels
  219. * @return static
  220. */
  221. public function dirname($levels = 1)
  222. {
  223. return new static(dirname($this->value, $levels));
  224. }
  225. /**
  226. * Determine if a given string ends with a given substring.
  227. *
  228. * @param string|string[] $needles
  229. * @return bool
  230. */
  231. public function endsWith($needles)
  232. {
  233. return Str::endsWith($this->value, $needles);
  234. }
  235. /**
  236. * Determine if the string is an exact match with the given value.
  237. *
  238. * @param string|\Stringable $value
  239. * @return bool
  240. */
  241. public function exactly($value)
  242. {
  243. if ($value instanceof \Stringable) {
  244. $value = $value->__toString();
  245. }
  246. return $this->value === $value;
  247. }
  248. /**
  249. * Explode the string into an array.
  250. *
  251. * @param string $delimiter
  252. * @param int $limit
  253. * @return Collection
  254. */
  255. public function explode($delimiter, $limit = PHP_INT_MAX)
  256. {
  257. return collect(explode($delimiter, $this->value, $limit));
  258. }
  259. /**
  260. * Split a string using a regular expression or by length.
  261. *
  262. * @param int|string $pattern
  263. * @param int $limit
  264. * @param int $flags
  265. * @return Collection
  266. */
  267. public function split($pattern, $limit = -1, $flags = 0)
  268. {
  269. if (filter_var($pattern, FILTER_VALIDATE_INT) !== false) {
  270. return collect(mb_str_split($this->value, $pattern));
  271. }
  272. $segments = preg_split($pattern, $this->value, $limit, $flags);
  273. return ! empty($segments) ? collect($segments) : collect();
  274. }
  275. /**
  276. * Cap a string with a single instance of a given value.
  277. *
  278. * @param string $cap
  279. * @return static
  280. */
  281. public function finish($cap)
  282. {
  283. return new static(Str::finish($this->value, $cap));
  284. }
  285. /**
  286. * Determine if a given string matches a given pattern.
  287. *
  288. * @param string|string[] $pattern
  289. * @param bool $ignoreCase
  290. * @return bool
  291. */
  292. public function is($pattern, $ignoreCase = false)
  293. {
  294. return Str::is($pattern, $this->value, $ignoreCase);
  295. }
  296. /**
  297. * Determine if a given string is 7 bit ASCII.
  298. *
  299. * @return bool
  300. */
  301. public function isAscii()
  302. {
  303. return Str::isAscii($this->value);
  304. }
  305. /**
  306. * Determine if the given string is empty.
  307. *
  308. * @return bool
  309. */
  310. public function isEmpty()
  311. {
  312. return $this->value === '';
  313. }
  314. /**
  315. * Determine if the given string is not empty.
  316. *
  317. * @return bool
  318. */
  319. public function isNotEmpty()
  320. {
  321. return ! $this->isEmpty();
  322. }
  323. public function isUlid(): bool
  324. {
  325. return Str::isUlid($this->value);
  326. }
  327. public function isUrl(): bool
  328. {
  329. return Str::isUrl($this->value);
  330. }
  331. public function isUuid(): bool
  332. {
  333. return Str::isUuid($this->value);
  334. }
  335. /**
  336. * Convert a string to kebab case.
  337. *
  338. * @return static
  339. */
  340. public function kebab()
  341. {
  342. return new static(Str::kebab($this->value));
  343. }
  344. /**
  345. * Return the length of the given string.
  346. *
  347. * @param string $encoding
  348. * @return int
  349. */
  350. public function length($encoding = null)
  351. {
  352. return Str::length($this->value, $encoding);
  353. }
  354. /**
  355. * Limit the number of characters in a string.
  356. *
  357. * @param int $limit
  358. * @param string $end
  359. * @return static
  360. */
  361. public function limit($limit = 100, $end = '...')
  362. {
  363. return new static(Str::limit($this->value, $limit, $end));
  364. }
  365. /**
  366. * Convert the given string to lower-case.
  367. *
  368. * @return static
  369. */
  370. public function lower()
  371. {
  372. return new static(Str::lower($this->value));
  373. }
  374. /**
  375. * Get the string matching the given pattern.
  376. *
  377. * @param string $pattern
  378. * @return static
  379. */
  380. public function match($pattern)
  381. {
  382. preg_match($pattern, $this->value, $matches);
  383. if (! $matches) {
  384. return new static();
  385. }
  386. return new static($matches[1] ?? $matches[0]);
  387. }
  388. /**
  389. * Determine if a given string matches a given pattern.
  390. *
  391. * @param iterable<string>|string $pattern
  392. * @return bool
  393. */
  394. public function isMatch($pattern)
  395. {
  396. return Str::isMatch($pattern, $this->value);
  397. }
  398. /**
  399. * Get the string matching the given pattern.
  400. *
  401. * @param string $pattern
  402. * @return Collection
  403. */
  404. public function matchAll($pattern)
  405. {
  406. preg_match_all($pattern, $this->value, $matches);
  407. if (empty($matches[0])) {
  408. return collect();
  409. }
  410. return collect($matches[1] ?? $matches[0]);
  411. }
  412. /**
  413. * Determine if the string matches the given pattern.
  414. *
  415. * @param string $pattern
  416. * @return bool
  417. */
  418. public function test($pattern)
  419. {
  420. return $this->match($pattern)->isNotEmpty();
  421. }
  422. /**
  423. * Pad both sides of the string with another.
  424. *
  425. * @param int $length
  426. * @param string $pad
  427. * @return static
  428. */
  429. public function padBoth($length, $pad = ' ')
  430. {
  431. return new static(Str::padBoth($this->value, $length, $pad));
  432. }
  433. /**
  434. * Pad the left side of the string with another.
  435. *
  436. * @param int $length
  437. * @param string $pad
  438. * @return static
  439. */
  440. public function padLeft($length, $pad = ' ')
  441. {
  442. return new static(Str::padLeft($this->value, $length, $pad));
  443. }
  444. /**
  445. * Pad the right side of the string with another.
  446. *
  447. * @param int $length
  448. * @param string $pad
  449. * @return static
  450. */
  451. public function padRight($length, $pad = ' ')
  452. {
  453. return new static(Str::padRight($this->value, $length, $pad));
  454. }
  455. /**
  456. * Parse a Class@method style callback into class and method.
  457. *
  458. * @param null|string $default
  459. * @return array
  460. */
  461. public function parseCallback($default = null)
  462. {
  463. return Str::parseCallback($this->value, $default);
  464. }
  465. /**
  466. * Call the given callback and return a new string.
  467. *
  468. * @return static
  469. */
  470. public function pipe(callable $callback)
  471. {
  472. return new static(call_user_func($callback, $this));
  473. }
  474. /**
  475. * Get the plural form of an English word.
  476. *
  477. * @param int $count
  478. * @return static
  479. */
  480. public function plural($count = 2)
  481. {
  482. return new static(Str::plural($this->value, $count));
  483. }
  484. /**
  485. * Pluralize the last word of an English, studly caps case string.
  486. *
  487. * @param int $count
  488. * @return static
  489. */
  490. public function pluralStudly($count = 2)
  491. {
  492. return new static(Str::pluralStudly($this->value, $count));
  493. }
  494. /**
  495. * Find the multi-byte safe position of the first occurrence of the given substring.
  496. *
  497. * @param string $needle
  498. * @param int $offset
  499. * @param null|string $encoding
  500. * @return false|int
  501. */
  502. public function position($needle, $offset = 0, $encoding = null)
  503. {
  504. return Str::position($this->value, $needle, $offset, $encoding);
  505. }
  506. /**
  507. * Prepend the given values to the string.
  508. *
  509. * @param string $values
  510. * @return static
  511. */
  512. public function prepend(...$values)
  513. {
  514. return new static(implode('', $values) . $this->value);
  515. }
  516. /**
  517. * Remove any occurrence of the given string in the subject.
  518. *
  519. * @param string|string[] $search
  520. * @param bool $caseSensitive
  521. * @return static
  522. */
  523. public function remove($search, $caseSensitive = true)
  524. {
  525. return new static(Str::remove($search, $this->value, $caseSensitive));
  526. }
  527. /**
  528. * Repeat the string.
  529. *
  530. * @return static
  531. */
  532. public function repeat(int $times)
  533. {
  534. return new static(Str::repeat($this->value, $times));
  535. }
  536. /**
  537. * Replace the given value in the given string.
  538. *
  539. * @param string|string[] $search
  540. * @param string|string[] $replace
  541. * @return static
  542. */
  543. public function replace($search, $replace)
  544. {
  545. return new static(Str::replace($search, $replace, $this->value));
  546. }
  547. /**
  548. * Replace a given value in the string sequentially with an array.
  549. *
  550. * @param string $search
  551. * @return static
  552. */
  553. public function replaceArray($search, array $replace)
  554. {
  555. return new static(Str::replaceArray($search, $replace, $this->value));
  556. }
  557. /**
  558. * Replace the first occurrence of a given value in the string.
  559. *
  560. * @param string $search
  561. * @param string $replace
  562. * @return static
  563. */
  564. public function replaceFirst($search, $replace)
  565. {
  566. return new static(Str::replaceFirst($search, $replace, $this->value));
  567. }
  568. /**
  569. * Replace the last occurrence of a given value in the string.
  570. *
  571. * @param string $search
  572. * @param string $replace
  573. * @return static
  574. */
  575. public function replaceLast($search, $replace)
  576. {
  577. return new static(Str::replaceLast($search, $replace, $this->value));
  578. }
  579. /**
  580. * Replace the patterns matching the given regular expression.
  581. *
  582. * @param string $pattern
  583. * @param Closure|string $replace
  584. * @param int $limit
  585. * @return static
  586. */
  587. public function replaceMatches($pattern, $replace, $limit = -1)
  588. {
  589. return new static(Str::replaceMatches($pattern, $replace, $this->value, $limit));
  590. }
  591. /**
  592. * Begin a string with a single instance of a given value.
  593. *
  594. * @param string $prefix
  595. * @return static
  596. */
  597. public function start($prefix)
  598. {
  599. return new static(Str::start($this->value, $prefix));
  600. }
  601. /**
  602. * Strip HTML and PHP tags from the given string.
  603. *
  604. * @param null|string|string[] $allowedTags
  605. * @return static
  606. */
  607. public function stripTags($allowedTags = null)
  608. {
  609. return new static(strip_tags($this->value, $allowedTags));
  610. }
  611. /**
  612. * Convert the given string to upper-case.
  613. *
  614. * @return static
  615. */
  616. public function upper()
  617. {
  618. return new static(Str::upper($this->value));
  619. }
  620. /**
  621. * Convert the given string to title case.
  622. *
  623. * @return static
  624. */
  625. public function title()
  626. {
  627. return new static(Str::title($this->value));
  628. }
  629. /**
  630. * Convert the given string to proper case for each word.
  631. *
  632. * @return static
  633. */
  634. public function headline()
  635. {
  636. return new static(Str::headline($this->value));
  637. }
  638. /**
  639. * Get the singular form of an English word.
  640. *
  641. * @return static
  642. */
  643. public function singular()
  644. {
  645. return new static(Str::singular($this->value));
  646. }
  647. /**
  648. * Generate a URL friendly "slug" from a given string.
  649. *
  650. * @param string $separator
  651. * @param null|string $language
  652. * @return static
  653. */
  654. public function slug($separator = '-', $language = 'en')
  655. {
  656. return new static(Str::slug($this->value, $separator, $language));
  657. }
  658. /**
  659. * Convert a string to snake case.
  660. *
  661. * @param string $delimiter
  662. * @return static
  663. */
  664. public function snake($delimiter = '_')
  665. {
  666. return new static(Str::snake($this->value, $delimiter));
  667. }
  668. /**
  669. * Determine if a given string starts with a given substring.
  670. *
  671. * @param string|string[] $needles
  672. * @return bool
  673. */
  674. public function startsWith($needles)
  675. {
  676. return Str::startsWith($this->value, $needles);
  677. }
  678. /**
  679. * Convert a value to studly caps case.
  680. *
  681. * @return static
  682. */
  683. public function studly()
  684. {
  685. return new static(Str::studly($this->value));
  686. }
  687. /**
  688. * Returns the portion of the string specified by the start and length parameters.
  689. *
  690. * @param int $start
  691. * @param null|int $length
  692. * @return static
  693. */
  694. public function substr($start, $length = null)
  695. {
  696. return new static(Str::substr($this->value, $start, $length));
  697. }
  698. /**
  699. * Returns the number of substring occurrences.
  700. *
  701. * @param string $needle
  702. * @param null|int $offset
  703. * @param null|int $length
  704. * @return int
  705. */
  706. public function substrCount($needle, $offset = null, $length = null)
  707. {
  708. return Str::substrCount($this->value, $needle, $offset ?? 0, $length);
  709. }
  710. /**
  711. * Take the first or last {$limit} characters.
  712. *
  713. * @return static
  714. */
  715. public function take(int $limit)
  716. {
  717. if ($limit < 0) {
  718. return $this->substr($limit);
  719. }
  720. return $this->substr(0, $limit);
  721. }
  722. /**
  723. * Convert the string to Base64 encoding.
  724. *
  725. * @return static
  726. */
  727. public function toBase64()
  728. {
  729. return new static(base64_encode($this->value));
  730. }
  731. /**
  732. * Trim the string of the given characters.
  733. *
  734. * @param string $characters
  735. * @return static
  736. */
  737. public function trim($characters = null)
  738. {
  739. return new static(Str::trim(...array_merge([$this->value], func_get_args())));
  740. }
  741. /**
  742. * Left trim the string of the given characters.
  743. *
  744. * @param string $characters
  745. * @return static
  746. */
  747. public function ltrim($characters = null)
  748. {
  749. return new static(Str::ltrim(...array_merge([$this->value], func_get_args())));
  750. }
  751. /**
  752. * Right trim the string of the given characters.
  753. *
  754. * @param string $characters
  755. * @return static
  756. */
  757. public function rtrim($characters = null)
  758. {
  759. return new static(Str::rtrim(...array_merge([$this->value], func_get_args())));
  760. }
  761. /**
  762. * Make a string's first character uppercase.
  763. *
  764. * @return static
  765. */
  766. public function ucfirst()
  767. {
  768. return new static(Str::ucfirst($this->value));
  769. }
  770. /**
  771. * Unwrap the string with the given strings.
  772. *
  773. * @param string $before
  774. * @param null|string $after
  775. * @return static
  776. */
  777. public function unwrap($before, $after = null)
  778. {
  779. return new static(Str::unwrap($this->value, $before, $after));
  780. }
  781. /**
  782. * Replaces the first or the last ones chars from a string by a given char.
  783. *
  784. * @param int $offset if is negative it starts from the end
  785. * @param string $replacement default is *
  786. * @return static
  787. */
  788. public function mask(int $offset = 0, int $length = 0, string $replacement = '*')
  789. {
  790. return new static(Str::mask($this->value, $offset, $length, $replacement));
  791. }
  792. /**
  793. * Execute the given callback if the string is empty.
  794. *
  795. * @param callable $callback
  796. * @param null|callable $default
  797. * @return static
  798. */
  799. public function whenEmpty($callback, $default = null)
  800. {
  801. return $this->when($this->isEmpty(), $callback, $default);
  802. }
  803. /**
  804. * Execute the given callback if the string is not empty.
  805. *
  806. * @param callable $callback
  807. * @param null|callable $default
  808. * @return static
  809. */
  810. public function whenNotEmpty($callback, $default = null)
  811. {
  812. return $this->when($this->isNotEmpty(), $callback, $default);
  813. }
  814. /**
  815. * Limit the number of words in a string.
  816. *
  817. * @param int $words
  818. * @param string $end
  819. * @return static
  820. */
  821. public function words($words = 100, $end = '...')
  822. {
  823. return new static(Str::words($this->value, $words, $end));
  824. }
  825. /**
  826. * Get the number of words a string contains.
  827. *
  828. * @return int
  829. */
  830. public function wordCount()
  831. {
  832. return str_word_count($this->value);
  833. }
  834. /**
  835. * Convert the object to a string when JSON encoded.
  836. */
  837. public function jsonSerialize(): mixed
  838. {
  839. return $this->__toString();
  840. }
  841. /**
  842. * Determine if the given offset exists.
  843. */
  844. public function offsetExists(mixed $offset): bool
  845. {
  846. return isset($this->value[$offset]);
  847. }
  848. /**
  849. * Get the value at the given offset.
  850. */
  851. public function offsetGet(mixed $offset): string
  852. {
  853. return $this->value[$offset];
  854. }
  855. /**
  856. * Set the value at the given offset.
  857. */
  858. public function offsetSet(mixed $offset, mixed $value): void
  859. {
  860. $this->value[$offset] = $value;
  861. }
  862. /**
  863. * Unset the value at the given offset.
  864. */
  865. public function offsetUnset(mixed $offset): void
  866. {
  867. unset($this->value[$offset]);
  868. }
  869. public function betweenFirst($from, $to): static
  870. {
  871. return new static(Str::betweenFirst($this->value, $from, $to));
  872. }
  873. public function classNamespace(): static
  874. {
  875. return new static(Str::classNamespace($this->value));
  876. }
  877. public function convertCase($mode = MB_CASE_FOLD, $encoding = 'UTF-8'): static
  878. {
  879. return new static(Str::convertCase($this->value, $mode, $encoding));
  880. }
  881. public function excerpt($phrase = '', $options = []): ?string
  882. {
  883. return Str::excerpt($this->value, $phrase, $options);
  884. }
  885. public function isJson(): bool
  886. {
  887. return Str::isJson($this->value);
  888. }
  889. public function lcfirst(): static
  890. {
  891. return new static(Str::lcfirst($this->value));
  892. }
  893. public function newLine($count = 1): static
  894. {
  895. return $this->append(str_repeat(PHP_EOL, $count));
  896. }
  897. public function replaceStart($search, $replace): static
  898. {
  899. return new static(Str::replaceStart($search, $replace, $this->value));
  900. }
  901. public function replaceEnd($search, $replace): static
  902. {
  903. return new static(Str::replaceEnd($search, $replace, $this->value));
  904. }
  905. public function reverse(): static
  906. {
  907. return new static(Str::reverse($this->value));
  908. }
  909. public function scan($format): Collection
  910. {
  911. return collect(sscanf($this->value, $format));
  912. }
  913. public function squish(): static
  914. {
  915. return new static(Str::squish($this->value));
  916. }
  917. public function substrReplace($replace, $offset = 0, $length = null): static
  918. {
  919. return new static(Str::substrReplace($this->value, $replace, $offset, $length));
  920. }
  921. public function swap(array $map)
  922. {
  923. return new static(strtr($this->value, $map));
  924. }
  925. public function toString(): string
  926. {
  927. return $this->value;
  928. }
  929. public function ucsplit(): Collection
  930. {
  931. return collect(Str::ucsplit($this->value));
  932. }
  933. public function value(): string
  934. {
  935. return $this->toString();
  936. }
  937. public function whenContains($needles, $callback, $default = null)
  938. {
  939. return $this->when($this->contains($needles), $callback, $default);
  940. }
  941. public function whenContainsAll(array $needles, $callback, $default = null)
  942. {
  943. return $this->when($this->containsAll($needles), $callback, $default);
  944. }
  945. public function whenEndsWith($needles, $callback, $default = null)
  946. {
  947. return $this->when($this->endsWith($needles), $callback, $default);
  948. }
  949. public function whenExactly($needles, $callback, $default = null)
  950. {
  951. return $this->when($this->exactly($needles), $callback, $default);
  952. }
  953. public function whenIs($pattern, $callback, $default = null, $ignoreCase = false)
  954. {
  955. return $this->when($this->is($pattern, $ignoreCase), $callback, $default);
  956. }
  957. public function whenIsUlid($callback, $default = null)
  958. {
  959. return $this->when($this->isUlid(), $callback, $default);
  960. }
  961. public function whenIsUuid($callback, $default = null)
  962. {
  963. return $this->when($this->isUuid(), $callback, $default);
  964. }
  965. public function whenNotExactly($needles, $callback, $default = null)
  966. {
  967. return $this->when(! $this->exactly($needles), $callback, $default);
  968. }
  969. public function whenStartsWith($needles, $callback, $default = null)
  970. {
  971. return $this->when($this->startsWith($needles), $callback, $default);
  972. }
  973. public function whenTest($pattern, $callback, $default = null)
  974. {
  975. return $this->when($this->test($pattern), $callback, $default);
  976. }
  977. public function wrap($before, $after = null)
  978. {
  979. return new static(Str::wrap($this->value, $before, $after));
  980. }
  981. /**
  982. * Wrap a string to a given number of characters.
  983. *
  984. * @param mixed $characters
  985. * @param mixed $break
  986. * @param mixed $cutLongWords
  987. */
  988. public function wordWrap($characters = 75, $break = "\n", $cutLongWords = false): static
  989. {
  990. return new static(Str::wordWrap($this->value, $characters, $break, $cutLongWords));
  991. }
  992. }