Stringable.php 25 KB

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