Stringable.php 38 KB

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