Stringable.php 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  1. <?php
  2. namespace Illuminate\Support;
  3. use ArrayAccess;
  4. use Closure;
  5. use Illuminate\Support\Facades\Date;
  6. use Illuminate\Support\Traits\Conditionable;
  7. use Illuminate\Support\Traits\Dumpable;
  8. use Illuminate\Support\Traits\Macroable;
  9. use Illuminate\Support\Traits\Tappable;
  10. use JsonSerializable;
  11. use Stringable as BaseStringable;
  12. class Stringable implements JsonSerializable, ArrayAccess, BaseStringable
  13. {
  14. use Conditionable, Dumpable, Macroable, Tappable;
  15. /**
  16. * The underlying string value.
  17. *
  18. * @var string
  19. */
  20. protected $value;
  21. /**
  22. * Create a new instance of the class.
  23. *
  24. * @param string $value
  25. * @return void
  26. */
  27. public function __construct($value = '')
  28. {
  29. $this->value = (string) $value;
  30. }
  31. /**
  32. * Return the remainder of a string after the first occurrence of a given value.
  33. *
  34. * @param string $search
  35. * @return static
  36. */
  37. public function after($search)
  38. {
  39. return new static(Str::after($this->value, $search));
  40. }
  41. /**
  42. * Return the remainder of a string after the last occurrence of a given value.
  43. *
  44. * @param string $search
  45. * @return static
  46. */
  47. public function afterLast($search)
  48. {
  49. return new static(Str::afterLast($this->value, $search));
  50. }
  51. /**
  52. * Append the given values to the string.
  53. *
  54. * @param array|string ...$values
  55. * @return static
  56. */
  57. public function append(...$values)
  58. {
  59. return new static($this->value.implode('', $values));
  60. }
  61. /**
  62. * Append a new line to the string.
  63. *
  64. * @param int $count
  65. * @return $this
  66. */
  67. public function newLine($count = 1)
  68. {
  69. return $this->append(str_repeat(PHP_EOL, $count));
  70. }
  71. /**
  72. * Transliterate a UTF-8 value to ASCII.
  73. *
  74. * @param string $language
  75. * @return static
  76. */
  77. public function ascii($language = 'en')
  78. {
  79. return new static(Str::ascii($this->value, $language));
  80. }
  81. /**
  82. * Get the trailing name component of the path.
  83. *
  84. * @param string $suffix
  85. * @return static
  86. */
  87. public function basename($suffix = '')
  88. {
  89. return new static(basename($this->value, $suffix));
  90. }
  91. /**
  92. * Get the character at the specified index.
  93. *
  94. * @param int $index
  95. * @return string|false
  96. */
  97. public function charAt($index)
  98. {
  99. return Str::charAt($this->value, $index);
  100. }
  101. /**
  102. * Remove the given string if it exists at the start of the current string.
  103. *
  104. * @param string|array $needle
  105. * @return static
  106. */
  107. public function chopStart($needle)
  108. {
  109. return new static(Str::chopStart($this->value, $needle));
  110. }
  111. /**
  112. * Remove the given string if it exists at the end of the current string.
  113. *
  114. * @param string|array $needle
  115. * @return static
  116. */
  117. public function chopEnd($needle)
  118. {
  119. return new static(Str::chopEnd($this->value, $needle));
  120. }
  121. /**
  122. * Get the basename of the class path.
  123. *
  124. * @return static
  125. */
  126. public function classBasename()
  127. {
  128. return new static(class_basename($this->value));
  129. }
  130. /**
  131. * Get the portion of a string before the first occurrence of a given value.
  132. *
  133. * @param string $search
  134. * @return static
  135. */
  136. public function before($search)
  137. {
  138. return new static(Str::before($this->value, $search));
  139. }
  140. /**
  141. * Get the portion of a string before the last occurrence of a given value.
  142. *
  143. * @param string $search
  144. * @return static
  145. */
  146. public function beforeLast($search)
  147. {
  148. return new static(Str::beforeLast($this->value, $search));
  149. }
  150. /**
  151. * Get the portion of a string between two given values.
  152. *
  153. * @param string $from
  154. * @param string $to
  155. * @return static
  156. */
  157. public function between($from, $to)
  158. {
  159. return new static(Str::between($this->value, $from, $to));
  160. }
  161. /**
  162. * Get the smallest possible portion of a string between two given values.
  163. *
  164. * @param string $from
  165. * @param string $to
  166. * @return static
  167. */
  168. public function betweenFirst($from, $to)
  169. {
  170. return new static(Str::betweenFirst($this->value, $from, $to));
  171. }
  172. /**
  173. * Convert a value to camel case.
  174. *
  175. * @return static
  176. */
  177. public function camel()
  178. {
  179. return new static(Str::camel($this->value));
  180. }
  181. /**
  182. * Determine if a given string contains a given substring.
  183. *
  184. * @param string|iterable<string> $needles
  185. * @param bool $ignoreCase
  186. * @return bool
  187. */
  188. public function contains($needles, $ignoreCase = false)
  189. {
  190. return Str::contains($this->value, $needles, $ignoreCase);
  191. }
  192. /**
  193. * Determine if a given string contains all array values.
  194. *
  195. * @param iterable<string> $needles
  196. * @param bool $ignoreCase
  197. * @return bool
  198. */
  199. public function containsAll($needles, $ignoreCase = false)
  200. {
  201. return Str::containsAll($this->value, $needles, $ignoreCase);
  202. }
  203. /**
  204. * Convert the case of a string.
  205. *
  206. * @param int $mode
  207. * @param string|null $encoding
  208. * @return static
  209. */
  210. public function convertCase(int $mode = MB_CASE_FOLD, ?string $encoding = 'UTF-8')
  211. {
  212. return new static(Str::convertCase($this->value, $mode, $encoding));
  213. }
  214. /**
  215. * Get the parent directory's path.
  216. *
  217. * @param int $levels
  218. * @return static
  219. */
  220. public function dirname($levels = 1)
  221. {
  222. return new static(dirname($this->value, $levels));
  223. }
  224. /**
  225. * Determine if a given string ends with a given substring.
  226. *
  227. * @param string|iterable<string> $needles
  228. * @return bool
  229. */
  230. public function endsWith($needles)
  231. {
  232. return Str::endsWith($this->value, $needles);
  233. }
  234. /**
  235. * Determine if the string is an exact match with the given value.
  236. *
  237. * @param \Illuminate\Support\Stringable|string $value
  238. * @return bool
  239. */
  240. public function exactly($value)
  241. {
  242. if ($value instanceof Stringable) {
  243. $value = $value->toString();
  244. }
  245. return $this->value === $value;
  246. }
  247. /**
  248. * Extracts an excerpt from text that matches the first instance of a phrase.
  249. *
  250. * @param string $phrase
  251. * @param array $options
  252. * @return string|null
  253. */
  254. public function excerpt($phrase = '', $options = [])
  255. {
  256. return Str::excerpt($this->value, $phrase, $options);
  257. }
  258. /**
  259. * Explode the string into an array.
  260. *
  261. * @param string $delimiter
  262. * @param int $limit
  263. * @return \Illuminate\Support\Collection<int, string>
  264. */
  265. public function explode($delimiter, $limit = PHP_INT_MAX)
  266. {
  267. return collect(explode($delimiter, $this->value, $limit));
  268. }
  269. /**
  270. * Split a string using a regular expression or by length.
  271. *
  272. * @param string|int $pattern
  273. * @param int $limit
  274. * @param int $flags
  275. * @return \Illuminate\Support\Collection<int, string>
  276. */
  277. public function split($pattern, $limit = -1, $flags = 0)
  278. {
  279. if (filter_var($pattern, FILTER_VALIDATE_INT) !== false) {
  280. return collect(mb_str_split($this->value, $pattern));
  281. }
  282. $segments = preg_split($pattern, $this->value, $limit, $flags);
  283. return ! empty($segments) ? collect($segments) : collect();
  284. }
  285. /**
  286. * Cap a string with a single instance of a given value.
  287. *
  288. * @param string $cap
  289. * @return static
  290. */
  291. public function finish($cap)
  292. {
  293. return new static(Str::finish($this->value, $cap));
  294. }
  295. /**
  296. * Determine if a given string matches a given pattern.
  297. *
  298. * @param string|iterable<string> $pattern
  299. * @return bool
  300. */
  301. public function is($pattern)
  302. {
  303. return Str::is($pattern, $this->value);
  304. }
  305. /**
  306. * Determine if a given string is 7 bit ASCII.
  307. *
  308. * @return bool
  309. */
  310. public function isAscii()
  311. {
  312. return Str::isAscii($this->value);
  313. }
  314. /**
  315. * Determine if a given string is valid JSON.
  316. *
  317. * @return bool
  318. */
  319. public function isJson()
  320. {
  321. return Str::isJson($this->value);
  322. }
  323. /**
  324. * Determine if a given value is a valid URL.
  325. *
  326. * @return bool
  327. */
  328. public function isUrl()
  329. {
  330. return Str::isUrl($this->value);
  331. }
  332. /**
  333. * Determine if a given string is a valid UUID.
  334. *
  335. * @return bool
  336. */
  337. public function isUuid()
  338. {
  339. return Str::isUuid($this->value);
  340. }
  341. /**
  342. * Determine if a given string is a valid ULID.
  343. *
  344. * @return bool
  345. */
  346. public function isUlid()
  347. {
  348. return Str::isUlid($this->value);
  349. }
  350. /**
  351. * Determine if the given string is empty.
  352. *
  353. * @return bool
  354. */
  355. public function isEmpty()
  356. {
  357. return $this->value === '';
  358. }
  359. /**
  360. * Determine if the given string is not empty.
  361. *
  362. * @return bool
  363. */
  364. public function isNotEmpty()
  365. {
  366. return ! $this->isEmpty();
  367. }
  368. /**
  369. * Convert a string to kebab case.
  370. *
  371. * @return static
  372. */
  373. public function kebab()
  374. {
  375. return new static(Str::kebab($this->value));
  376. }
  377. /**
  378. * Return the length of the given string.
  379. *
  380. * @param string|null $encoding
  381. * @return int
  382. */
  383. public function length($encoding = null)
  384. {
  385. return Str::length($this->value, $encoding);
  386. }
  387. /**
  388. * Limit the number of characters in a string.
  389. *
  390. * @param int $limit
  391. * @param string $end
  392. * @return static
  393. */
  394. public function limit($limit = 100, $end = '...')
  395. {
  396. return new static(Str::limit($this->value, $limit, $end));
  397. }
  398. /**
  399. * Convert the given string to lower-case.
  400. *
  401. * @return static
  402. */
  403. public function lower()
  404. {
  405. return new static(Str::lower($this->value));
  406. }
  407. /**
  408. * Convert GitHub flavored Markdown into HTML.
  409. *
  410. * @param array $options
  411. * @param array $extensions
  412. * @return static
  413. */
  414. public function markdown(array $options = [], array $extensions = [])
  415. {
  416. return new static(Str::markdown($this->value, $options, $extensions));
  417. }
  418. /**
  419. * Convert inline Markdown into HTML.
  420. *
  421. * @param array $options
  422. * @return static
  423. */
  424. public function inlineMarkdown(array $options = [])
  425. {
  426. return new static(Str::inlineMarkdown($this->value, $options));
  427. }
  428. /**
  429. * Masks a portion of a string with a repeated character.
  430. *
  431. * @param string $character
  432. * @param int $index
  433. * @param int|null $length
  434. * @param string $encoding
  435. * @return static
  436. */
  437. public function mask($character, $index, $length = null, $encoding = 'UTF-8')
  438. {
  439. return new static(Str::mask($this->value, $character, $index, $length, $encoding));
  440. }
  441. /**
  442. * Get the string matching the given pattern.
  443. *
  444. * @param string $pattern
  445. * @return static
  446. */
  447. public function match($pattern)
  448. {
  449. return new static(Str::match($pattern, $this->value));
  450. }
  451. /**
  452. * Determine if a given string matches a given pattern.
  453. *
  454. * @param string|iterable<string> $pattern
  455. * @return bool
  456. */
  457. public function isMatch($pattern)
  458. {
  459. return Str::isMatch($pattern, $this->value);
  460. }
  461. /**
  462. * Get the string matching the given pattern.
  463. *
  464. * @param string $pattern
  465. * @return \Illuminate\Support\Collection
  466. */
  467. public function matchAll($pattern)
  468. {
  469. return Str::matchAll($pattern, $this->value);
  470. }
  471. /**
  472. * Determine if the string matches the given pattern.
  473. *
  474. * @param string $pattern
  475. * @return bool
  476. */
  477. public function test($pattern)
  478. {
  479. return $this->isMatch($pattern);
  480. }
  481. /**
  482. * Remove all non-numeric characters from a string.
  483. *
  484. * @return static
  485. */
  486. public function numbers()
  487. {
  488. return new static(Str::numbers($this->value));
  489. }
  490. /**
  491. * Pad both sides of the string with another.
  492. *
  493. * @param int $length
  494. * @param string $pad
  495. * @return static
  496. */
  497. public function padBoth($length, $pad = ' ')
  498. {
  499. return new static(Str::padBoth($this->value, $length, $pad));
  500. }
  501. /**
  502. * Pad the left side of the string with another.
  503. *
  504. * @param int $length
  505. * @param string $pad
  506. * @return static
  507. */
  508. public function padLeft($length, $pad = ' ')
  509. {
  510. return new static(Str::padLeft($this->value, $length, $pad));
  511. }
  512. /**
  513. * Pad the right side of the string with another.
  514. *
  515. * @param int $length
  516. * @param string $pad
  517. * @return static
  518. */
  519. public function padRight($length, $pad = ' ')
  520. {
  521. return new static(Str::padRight($this->value, $length, $pad));
  522. }
  523. /**
  524. * Parse a Class@method style callback into class and method.
  525. *
  526. * @param string|null $default
  527. * @return array<int, string|null>
  528. */
  529. public function parseCallback($default = null)
  530. {
  531. return Str::parseCallback($this->value, $default);
  532. }
  533. /**
  534. * Call the given callback and return a new string.
  535. *
  536. * @param callable $callback
  537. * @return static
  538. */
  539. public function pipe(callable $callback)
  540. {
  541. return new static($callback($this));
  542. }
  543. /**
  544. * Get the plural form of an English word.
  545. *
  546. * @param int|array|\Countable $count
  547. * @return static
  548. */
  549. public function plural($count = 2)
  550. {
  551. return new static(Str::plural($this->value, $count));
  552. }
  553. /**
  554. * Pluralize the last word of an English, studly caps case string.
  555. *
  556. * @param int|array|\Countable $count
  557. * @return static
  558. */
  559. public function pluralStudly($count = 2)
  560. {
  561. return new static(Str::pluralStudly($this->value, $count));
  562. }
  563. /**
  564. * Find the multi-byte safe position of the first occurrence of the given substring.
  565. *
  566. * @param string $needle
  567. * @param int $offset
  568. * @param string|null $encoding
  569. * @return int|false
  570. */
  571. public function position($needle, $offset = 0, $encoding = null)
  572. {
  573. return Str::position($this->value, $needle, $offset, $encoding);
  574. }
  575. /**
  576. * Prepend the given values to the string.
  577. *
  578. * @param string ...$values
  579. * @return static
  580. */
  581. public function prepend(...$values)
  582. {
  583. return new static(implode('', $values).$this->value);
  584. }
  585. /**
  586. * Remove any occurrence of the given string in the subject.
  587. *
  588. * @param string|iterable<string> $search
  589. * @param bool $caseSensitive
  590. * @return static
  591. */
  592. public function remove($search, $caseSensitive = true)
  593. {
  594. return new static(Str::remove($search, $this->value, $caseSensitive));
  595. }
  596. /**
  597. * Reverse the string.
  598. *
  599. * @return static
  600. */
  601. public function reverse()
  602. {
  603. return new static(Str::reverse($this->value));
  604. }
  605. /**
  606. * Repeat the string.
  607. *
  608. * @param int $times
  609. * @return static
  610. */
  611. public function repeat(int $times)
  612. {
  613. return new static(str_repeat($this->value, $times));
  614. }
  615. /**
  616. * Replace the given value in the given string.
  617. *
  618. * @param string|iterable<string> $search
  619. * @param string|iterable<string> $replace
  620. * @param bool $caseSensitive
  621. * @return static
  622. */
  623. public function replace($search, $replace, $caseSensitive = true)
  624. {
  625. return new static(Str::replace($search, $replace, $this->value, $caseSensitive));
  626. }
  627. /**
  628. * Replace a given value in the string sequentially with an array.
  629. *
  630. * @param string $search
  631. * @param iterable<string> $replace
  632. * @return static
  633. */
  634. public function replaceArray($search, $replace)
  635. {
  636. return new static(Str::replaceArray($search, $replace, $this->value));
  637. }
  638. /**
  639. * Replace the first occurrence of a given value in the string.
  640. *
  641. * @param string $search
  642. * @param string $replace
  643. * @return static
  644. */
  645. public function replaceFirst($search, $replace)
  646. {
  647. return new static(Str::replaceFirst($search, $replace, $this->value));
  648. }
  649. /**
  650. * Replace the first occurrence of the given value if it appears at the start of the string.
  651. *
  652. * @param string $search
  653. * @param string $replace
  654. * @return static
  655. */
  656. public function replaceStart($search, $replace)
  657. {
  658. return new static(Str::replaceStart($search, $replace, $this->value));
  659. }
  660. /**
  661. * Replace the last occurrence of a given value in the string.
  662. *
  663. * @param string $search
  664. * @param string $replace
  665. * @return static
  666. */
  667. public function replaceLast($search, $replace)
  668. {
  669. return new static(Str::replaceLast($search, $replace, $this->value));
  670. }
  671. /**
  672. * Replace the last occurrence of a given value if it appears at the end of the string.
  673. *
  674. * @param string $search
  675. * @param string $replace
  676. * @return static
  677. */
  678. public function replaceEnd($search, $replace)
  679. {
  680. return new static(Str::replaceEnd($search, $replace, $this->value));
  681. }
  682. /**
  683. * Replace the patterns matching the given regular expression.
  684. *
  685. * @param array|string $pattern
  686. * @param \Closure|string[]|string $replace
  687. * @param int $limit
  688. * @return static
  689. */
  690. public function replaceMatches($pattern, $replace, $limit = -1)
  691. {
  692. if ($replace instanceof Closure) {
  693. return new static(preg_replace_callback($pattern, $replace, $this->value, $limit));
  694. }
  695. return new static(preg_replace($pattern, $replace, $this->value, $limit));
  696. }
  697. /**
  698. * Parse input from a string to a collection, according to a format.
  699. *
  700. * @param string $format
  701. * @return \Illuminate\Support\Collection
  702. */
  703. public function scan($format)
  704. {
  705. return collect(sscanf($this->value, $format));
  706. }
  707. /**
  708. * Remove all "extra" blank space from the given string.
  709. *
  710. * @return static
  711. */
  712. public function squish()
  713. {
  714. return new static(Str::squish($this->value));
  715. }
  716. /**
  717. * Begin a string with a single instance of a given value.
  718. *
  719. * @param string $prefix
  720. * @return static
  721. */
  722. public function start($prefix)
  723. {
  724. return new static(Str::start($this->value, $prefix));
  725. }
  726. /**
  727. * Strip HTML and PHP tags from the given string.
  728. *
  729. * @param string[]|string|null $allowedTags
  730. * @return static
  731. */
  732. public function stripTags($allowedTags = null)
  733. {
  734. return new static(strip_tags($this->value, $allowedTags));
  735. }
  736. /**
  737. * Convert the given string to upper-case.
  738. *
  739. * @return static
  740. */
  741. public function upper()
  742. {
  743. return new static(Str::upper($this->value));
  744. }
  745. /**
  746. * Convert the given string to proper case.
  747. *
  748. * @return static
  749. */
  750. public function title()
  751. {
  752. return new static(Str::title($this->value));
  753. }
  754. /**
  755. * Convert the given string to proper case for each word.
  756. *
  757. * @return static
  758. */
  759. public function headline()
  760. {
  761. return new static(Str::headline($this->value));
  762. }
  763. /**
  764. * Convert the given string to APA-style title case.
  765. *
  766. * @return static
  767. */
  768. public function apa()
  769. {
  770. return new static(Str::apa($this->value));
  771. }
  772. /**
  773. * Transliterate a string to its closest ASCII representation.
  774. *
  775. * @param string|null $unknown
  776. * @param bool|null $strict
  777. * @return static
  778. */
  779. public function transliterate($unknown = '?', $strict = false)
  780. {
  781. return new static(Str::transliterate($this->value, $unknown, $strict));
  782. }
  783. /**
  784. * Get the singular form of an English word.
  785. *
  786. * @return static
  787. */
  788. public function singular()
  789. {
  790. return new static(Str::singular($this->value));
  791. }
  792. /**
  793. * Generate a URL friendly "slug" from a given string.
  794. *
  795. * @param string $separator
  796. * @param string|null $language
  797. * @param array<string, string> $dictionary
  798. * @return static
  799. */
  800. public function slug($separator = '-', $language = 'en', $dictionary = ['@' => 'at'])
  801. {
  802. return new static(Str::slug($this->value, $separator, $language, $dictionary));
  803. }
  804. /**
  805. * Convert a string to snake case.
  806. *
  807. * @param string $delimiter
  808. * @return static
  809. */
  810. public function snake($delimiter = '_')
  811. {
  812. return new static(Str::snake($this->value, $delimiter));
  813. }
  814. /**
  815. * Determine if a given string starts with a given substring.
  816. *
  817. * @param string|iterable<string> $needles
  818. * @return bool
  819. */
  820. public function startsWith($needles)
  821. {
  822. return Str::startsWith($this->value, $needles);
  823. }
  824. /**
  825. * Convert a value to studly caps case.
  826. *
  827. * @return static
  828. */
  829. public function studly()
  830. {
  831. return new static(Str::studly($this->value));
  832. }
  833. /**
  834. * Returns the portion of the string specified by the start and length parameters.
  835. *
  836. * @param int $start
  837. * @param int|null $length
  838. * @param string $encoding
  839. * @return static
  840. */
  841. public function substr($start, $length = null, $encoding = 'UTF-8')
  842. {
  843. return new static(Str::substr($this->value, $start, $length, $encoding));
  844. }
  845. /**
  846. * Returns the number of substring occurrences.
  847. *
  848. * @param string $needle
  849. * @param int $offset
  850. * @param int|null $length
  851. * @return int
  852. */
  853. public function substrCount($needle, $offset = 0, $length = null)
  854. {
  855. return Str::substrCount($this->value, $needle, $offset, $length);
  856. }
  857. /**
  858. * Replace text within a portion of a string.
  859. *
  860. * @param string|string[] $replace
  861. * @param int|int[] $offset
  862. * @param int|int[]|null $length
  863. * @return static
  864. */
  865. public function substrReplace($replace, $offset = 0, $length = null)
  866. {
  867. return new static(Str::substrReplace($this->value, $replace, $offset, $length));
  868. }
  869. /**
  870. * Swap multiple keywords in a string with other keywords.
  871. *
  872. * @param array $map
  873. * @return static
  874. */
  875. public function swap(array $map)
  876. {
  877. return new static(strtr($this->value, $map));
  878. }
  879. /**
  880. * Take the first or last {$limit} characters.
  881. *
  882. * @param int $limit
  883. * @return static
  884. */
  885. public function take(int $limit)
  886. {
  887. if ($limit < 0) {
  888. return $this->substr($limit);
  889. }
  890. return $this->substr(0, $limit);
  891. }
  892. /**
  893. * Trim the string of the given characters.
  894. *
  895. * @param string $characters
  896. * @return static
  897. */
  898. public function trim($characters = null)
  899. {
  900. return new static(Str::trim(...array_merge([$this->value], func_get_args())));
  901. }
  902. /**
  903. * Left trim the string of the given characters.
  904. *
  905. * @param string $characters
  906. * @return static
  907. */
  908. public function ltrim($characters = null)
  909. {
  910. return new static(Str::ltrim(...array_merge([$this->value], func_get_args())));
  911. }
  912. /**
  913. * Right trim the string of the given characters.
  914. *
  915. * @param string $characters
  916. * @return static
  917. */
  918. public function rtrim($characters = null)
  919. {
  920. return new static(Str::rtrim(...array_merge([$this->value], func_get_args())));
  921. }
  922. /**
  923. * Make a string's first character lowercase.
  924. *
  925. * @return static
  926. */
  927. public function lcfirst()
  928. {
  929. return new static(Str::lcfirst($this->value));
  930. }
  931. /**
  932. * Make a string's first character uppercase.
  933. *
  934. * @return static
  935. */
  936. public function ucfirst()
  937. {
  938. return new static(Str::ucfirst($this->value));
  939. }
  940. /**
  941. * Split a string by uppercase characters.
  942. *
  943. * @return \Illuminate\Support\Collection<int, string>
  944. */
  945. public function ucsplit()
  946. {
  947. return collect(Str::ucsplit($this->value));
  948. }
  949. /**
  950. * Execute the given callback if the string contains a given substring.
  951. *
  952. * @param string|iterable<string> $needles
  953. * @param callable $callback
  954. * @param callable|null $default
  955. * @return static
  956. */
  957. public function whenContains($needles, $callback, $default = null)
  958. {
  959. return $this->when($this->contains($needles), $callback, $default);
  960. }
  961. /**
  962. * Execute the given callback if the string contains all array values.
  963. *
  964. * @param iterable<string> $needles
  965. * @param callable $callback
  966. * @param callable|null $default
  967. * @return static
  968. */
  969. public function whenContainsAll(array $needles, $callback, $default = null)
  970. {
  971. return $this->when($this->containsAll($needles), $callback, $default);
  972. }
  973. /**
  974. * Execute the given callback if the string is empty.
  975. *
  976. * @param callable $callback
  977. * @param callable|null $default
  978. * @return static
  979. */
  980. public function whenEmpty($callback, $default = null)
  981. {
  982. return $this->when($this->isEmpty(), $callback, $default);
  983. }
  984. /**
  985. * Execute the given callback if the string is not empty.
  986. *
  987. * @param callable $callback
  988. * @param callable|null $default
  989. * @return static
  990. */
  991. public function whenNotEmpty($callback, $default = null)
  992. {
  993. return $this->when($this->isNotEmpty(), $callback, $default);
  994. }
  995. /**
  996. * Execute the given callback if the string ends with a given substring.
  997. *
  998. * @param string|iterable<string> $needles
  999. * @param callable $callback
  1000. * @param callable|null $default
  1001. * @return static
  1002. */
  1003. public function whenEndsWith($needles, $callback, $default = null)
  1004. {
  1005. return $this->when($this->endsWith($needles), $callback, $default);
  1006. }
  1007. /**
  1008. * Execute the given callback if the string is an exact match with the given value.
  1009. *
  1010. * @param string $value
  1011. * @param callable $callback
  1012. * @param callable|null $default
  1013. * @return static
  1014. */
  1015. public function whenExactly($value, $callback, $default = null)
  1016. {
  1017. return $this->when($this->exactly($value), $callback, $default);
  1018. }
  1019. /**
  1020. * Execute the given callback if the string is not an exact match with the given value.
  1021. *
  1022. * @param string $value
  1023. * @param callable $callback
  1024. * @param callable|null $default
  1025. * @return static
  1026. */
  1027. public function whenNotExactly($value, $callback, $default = null)
  1028. {
  1029. return $this->when(! $this->exactly($value), $callback, $default);
  1030. }
  1031. /**
  1032. * Execute the given callback if the string matches a given pattern.
  1033. *
  1034. * @param string|iterable<string> $pattern
  1035. * @param callable $callback
  1036. * @param callable|null $default
  1037. * @return static
  1038. */
  1039. public function whenIs($pattern, $callback, $default = null)
  1040. {
  1041. return $this->when($this->is($pattern), $callback, $default);
  1042. }
  1043. /**
  1044. * Execute the given callback if the string is 7 bit ASCII.
  1045. *
  1046. * @param callable $callback
  1047. * @param callable|null $default
  1048. * @return static
  1049. */
  1050. public function whenIsAscii($callback, $default = null)
  1051. {
  1052. return $this->when($this->isAscii(), $callback, $default);
  1053. }
  1054. /**
  1055. * Execute the given callback if the string is a valid UUID.
  1056. *
  1057. * @param callable $callback
  1058. * @param callable|null $default
  1059. * @return static
  1060. */
  1061. public function whenIsUuid($callback, $default = null)
  1062. {
  1063. return $this->when($this->isUuid(), $callback, $default);
  1064. }
  1065. /**
  1066. * Execute the given callback if the string is a valid ULID.
  1067. *
  1068. * @param callable $callback
  1069. * @param callable|null $default
  1070. * @return static
  1071. */
  1072. public function whenIsUlid($callback, $default = null)
  1073. {
  1074. return $this->when($this->isUlid(), $callback, $default);
  1075. }
  1076. /**
  1077. * Execute the given callback if the string starts with a given substring.
  1078. *
  1079. * @param string|iterable<string> $needles
  1080. * @param callable $callback
  1081. * @param callable|null $default
  1082. * @return static
  1083. */
  1084. public function whenStartsWith($needles, $callback, $default = null)
  1085. {
  1086. return $this->when($this->startsWith($needles), $callback, $default);
  1087. }
  1088. /**
  1089. * Execute the given callback if the string matches the given pattern.
  1090. *
  1091. * @param string $pattern
  1092. * @param callable $callback
  1093. * @param callable|null $default
  1094. * @return static
  1095. */
  1096. public function whenTest($pattern, $callback, $default = null)
  1097. {
  1098. return $this->when($this->test($pattern), $callback, $default);
  1099. }
  1100. /**
  1101. * Limit the number of words in a string.
  1102. *
  1103. * @param int $words
  1104. * @param string $end
  1105. * @return static
  1106. */
  1107. public function words($words = 100, $end = '...')
  1108. {
  1109. return new static(Str::words($this->value, $words, $end));
  1110. }
  1111. /**
  1112. * Get the number of words a string contains.
  1113. *
  1114. * @param string|null $characters
  1115. * @return int
  1116. */
  1117. public function wordCount($characters = null)
  1118. {
  1119. return Str::wordCount($this->value, $characters);
  1120. }
  1121. /**
  1122. * Wrap a string to a given number of characters.
  1123. *
  1124. * @param int $characters
  1125. * @param string $break
  1126. * @param bool $cutLongWords
  1127. * @return static
  1128. */
  1129. public function wordWrap($characters = 75, $break = "\n", $cutLongWords = false)
  1130. {
  1131. return new static(Str::wordWrap($this->value, $characters, $break, $cutLongWords));
  1132. }
  1133. /**
  1134. * Wrap the string with the given strings.
  1135. *
  1136. * @param string $before
  1137. * @param string|null $after
  1138. * @return static
  1139. */
  1140. public function wrap($before, $after = null)
  1141. {
  1142. return new static(Str::wrap($this->value, $before, $after));
  1143. }
  1144. /**
  1145. * Unwrap the string with the given strings.
  1146. *
  1147. * @param string $before
  1148. * @param string|null $after
  1149. * @return static
  1150. */
  1151. public function unwrap($before, $after = null)
  1152. {
  1153. return new static(Str::unwrap($this->value, $before, $after));
  1154. }
  1155. /**
  1156. * Convert the string into a `HtmlString` instance.
  1157. *
  1158. * @return \Illuminate\Support\HtmlString
  1159. */
  1160. public function toHtmlString()
  1161. {
  1162. return new HtmlString($this->value);
  1163. }
  1164. /**
  1165. * Convert the string to Base64 encoding.
  1166. *
  1167. * @return static
  1168. */
  1169. public function toBase64()
  1170. {
  1171. return new static(base64_encode($this->value));
  1172. }
  1173. /**
  1174. * Decode the Base64 encoded string.
  1175. *
  1176. * @param bool $strict
  1177. * @return static
  1178. */
  1179. public function fromBase64($strict = false)
  1180. {
  1181. return new static(base64_decode($this->value, $strict));
  1182. }
  1183. /**
  1184. * Dump the string.
  1185. *
  1186. * @param mixed ...$args
  1187. * @return $this
  1188. */
  1189. public function dump(...$args)
  1190. {
  1191. dump($this->value, ...$args);
  1192. return $this;
  1193. }
  1194. /**
  1195. * Get the underlying string value.
  1196. *
  1197. * @return string
  1198. */
  1199. public function value()
  1200. {
  1201. return $this->toString();
  1202. }
  1203. /**
  1204. * Get the underlying string value.
  1205. *
  1206. * @return string
  1207. */
  1208. public function toString()
  1209. {
  1210. return $this->value;
  1211. }
  1212. /**
  1213. * Get the underlying string value as an integer.
  1214. *
  1215. * @param int $base
  1216. * @return int
  1217. */
  1218. public function toInteger($base = 10)
  1219. {
  1220. return intval($this->value, $base);
  1221. }
  1222. /**
  1223. * Get the underlying string value as a float.
  1224. *
  1225. * @return float
  1226. */
  1227. public function toFloat()
  1228. {
  1229. return floatval($this->value);
  1230. }
  1231. /**
  1232. * Get the underlying string value as a boolean.
  1233. *
  1234. * Returns true when value is "1", "true", "on", and "yes". Otherwise, returns false.
  1235. *
  1236. * @return bool
  1237. */
  1238. public function toBoolean()
  1239. {
  1240. return filter_var($this->value, FILTER_VALIDATE_BOOLEAN);
  1241. }
  1242. /**
  1243. * Get the underlying string value as a Carbon instance.
  1244. *
  1245. * @param string|null $format
  1246. * @param string|null $tz
  1247. * @return \Illuminate\Support\Carbon
  1248. *
  1249. * @throws \Carbon\Exceptions\InvalidFormatException
  1250. */
  1251. public function toDate($format = null, $tz = null)
  1252. {
  1253. if (is_null($format)) {
  1254. return Date::parse($this->value, $tz);
  1255. }
  1256. return Date::createFromFormat($format, $this->value, $tz);
  1257. }
  1258. /**
  1259. * Convert the object to a string when JSON encoded.
  1260. *
  1261. * @return string
  1262. */
  1263. public function jsonSerialize(): string
  1264. {
  1265. return $this->__toString();
  1266. }
  1267. /**
  1268. * Determine if the given offset exists.
  1269. *
  1270. * @param mixed $offset
  1271. * @return bool
  1272. */
  1273. public function offsetExists(mixed $offset): bool
  1274. {
  1275. return isset($this->value[$offset]);
  1276. }
  1277. /**
  1278. * Get the value at the given offset.
  1279. *
  1280. * @param mixed $offset
  1281. * @return string
  1282. */
  1283. public function offsetGet(mixed $offset): string
  1284. {
  1285. return $this->value[$offset];
  1286. }
  1287. /**
  1288. * Set the value at the given offset.
  1289. *
  1290. * @param mixed $offset
  1291. * @return void
  1292. */
  1293. public function offsetSet(mixed $offset, mixed $value): void
  1294. {
  1295. $this->value[$offset] = $value;
  1296. }
  1297. /**
  1298. * Unset the value at the given offset.
  1299. *
  1300. * @param mixed $offset
  1301. * @return void
  1302. */
  1303. public function offsetUnset(mixed $offset): void
  1304. {
  1305. unset($this->value[$offset]);
  1306. }
  1307. /**
  1308. * Proxy dynamic properties onto methods.
  1309. *
  1310. * @param string $key
  1311. * @return mixed
  1312. */
  1313. public function __get($key)
  1314. {
  1315. return $this->{$key}();
  1316. }
  1317. /**
  1318. * Get the raw string value.
  1319. *
  1320. * @return string
  1321. */
  1322. public function __toString()
  1323. {
  1324. return (string) $this->value;
  1325. }
  1326. }