Stringable.php 33 KB

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