Stringable.php 23 KB

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