Stringable.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  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 string $character
  228. * @return static
  229. */
  230. public function deduplicate(string $character = ' ')
  231. {
  232. return new static(Str::deduplicate($this->value, $character));
  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 APA-style title case.
  811. *
  812. * @return static
  813. */
  814. public function apa()
  815. {
  816. return new static(Str::apa($this->value));
  817. }
  818. /**
  819. * Transliterate a string to its closest ASCII representation.
  820. *
  821. * @param string|null $unknown
  822. * @param bool|null $strict
  823. * @return static
  824. */
  825. public function transliterate($unknown = '?', $strict = false)
  826. {
  827. return new static(Str::transliterate($this->value, $unknown, $strict));
  828. }
  829. /**
  830. * Get the singular form of an English word.
  831. *
  832. * @return static
  833. */
  834. public function singular()
  835. {
  836. return new static(Str::singular($this->value));
  837. }
  838. /**
  839. * Generate a URL friendly "slug" from a given string.
  840. *
  841. * @param string $separator
  842. * @param string|null $language
  843. * @param array<string, string> $dictionary
  844. * @return static
  845. */
  846. public function slug($separator = '-', $language = 'en', $dictionary = ['@' => 'at'])
  847. {
  848. return new static(Str::slug($this->value, $separator, $language, $dictionary));
  849. }
  850. /**
  851. * Convert a string to snake case.
  852. *
  853. * @param string $delimiter
  854. * @return static
  855. */
  856. public function snake($delimiter = '_')
  857. {
  858. return new static(Str::snake($this->value, $delimiter));
  859. }
  860. /**
  861. * Determine if a given string starts with a given substring.
  862. *
  863. * @param string|iterable<string> $needles
  864. * @return bool
  865. */
  866. public function startsWith($needles)
  867. {
  868. return Str::startsWith($this->value, $needles);
  869. }
  870. /**
  871. * Determine if a given string doesn't start with a given substring.
  872. *
  873. * @param string|iterable<string> $needles
  874. * @return bool
  875. */
  876. public function doesntStartWith($needles)
  877. {
  878. return Str::doesntStartWith($this->value, $needles);
  879. }
  880. /**
  881. * Convert a value to studly caps case.
  882. *
  883. * @return static
  884. */
  885. public function studly()
  886. {
  887. return new static(Str::studly($this->value));
  888. }
  889. /**
  890. * Convert the string to Pascal case.
  891. *
  892. * @return static
  893. */
  894. public function pascal()
  895. {
  896. return new static(Str::pascal($this->value));
  897. }
  898. /**
  899. * Returns the portion of the string specified by the start and length parameters.
  900. *
  901. * @param int $start
  902. * @param int|null $length
  903. * @param string $encoding
  904. * @return static
  905. */
  906. public function substr($start, $length = null, $encoding = 'UTF-8')
  907. {
  908. return new static(Str::substr($this->value, $start, $length, $encoding));
  909. }
  910. /**
  911. * Returns the number of substring occurrences.
  912. *
  913. * @param string $needle
  914. * @param int $offset
  915. * @param int|null $length
  916. * @return int
  917. */
  918. public function substrCount($needle, $offset = 0, $length = null)
  919. {
  920. return Str::substrCount($this->value, $needle, $offset, $length);
  921. }
  922. /**
  923. * Replace text within a portion of a string.
  924. *
  925. * @param string|string[] $replace
  926. * @param int|int[] $offset
  927. * @param int|int[]|null $length
  928. * @return static
  929. */
  930. public function substrReplace($replace, $offset = 0, $length = null)
  931. {
  932. return new static(Str::substrReplace($this->value, $replace, $offset, $length));
  933. }
  934. /**
  935. * Swap multiple keywords in a string with other keywords.
  936. *
  937. * @param array $map
  938. * @return static
  939. */
  940. public function swap(array $map)
  941. {
  942. return new static(strtr($this->value, $map));
  943. }
  944. /**
  945. * Take the first or last {$limit} characters.
  946. *
  947. * @param int $limit
  948. * @return static
  949. */
  950. public function take(int $limit)
  951. {
  952. if ($limit < 0) {
  953. return $this->substr($limit);
  954. }
  955. return $this->substr(0, $limit);
  956. }
  957. /**
  958. * Trim the string of the given characters.
  959. *
  960. * @param string|null $characters
  961. * @return static
  962. */
  963. public function trim($characters = null)
  964. {
  965. return new static(Str::trim(...array_merge([$this->value], func_get_args())));
  966. }
  967. /**
  968. * Left trim the string of the given characters.
  969. *
  970. * @param string|null $characters
  971. * @return static
  972. */
  973. public function ltrim($characters = null)
  974. {
  975. return new static(Str::ltrim(...array_merge([$this->value], func_get_args())));
  976. }
  977. /**
  978. * Right trim the string of the given characters.
  979. *
  980. * @param string|null $characters
  981. * @return static
  982. */
  983. public function rtrim($characters = null)
  984. {
  985. return new static(Str::rtrim(...array_merge([$this->value], func_get_args())));
  986. }
  987. /**
  988. * Make a string's first character lowercase.
  989. *
  990. * @return static
  991. */
  992. public function lcfirst()
  993. {
  994. return new static(Str::lcfirst($this->value));
  995. }
  996. /**
  997. * Make a string's first character uppercase.
  998. *
  999. * @return static
  1000. */
  1001. public function ucfirst()
  1002. {
  1003. return new static(Str::ucfirst($this->value));
  1004. }
  1005. /**
  1006. * Capitalize the first character of each word in a string.
  1007. *
  1008. * @param string $separators
  1009. * @return static
  1010. */
  1011. public function ucwords($separators = " \t\r\n\f\v")
  1012. {
  1013. return new static(Str::ucwords($this->value, $separators));
  1014. }
  1015. /**
  1016. * Split a string by uppercase characters.
  1017. *
  1018. * @return \Illuminate\Support\Collection<int, string>
  1019. */
  1020. public function ucsplit()
  1021. {
  1022. return new Collection(Str::ucsplit($this->value));
  1023. }
  1024. /**
  1025. * Execute the given callback if the string contains a given substring.
  1026. *
  1027. * @param string|iterable<string> $needles
  1028. * @param callable $callback
  1029. * @param callable|null $default
  1030. * @return static
  1031. */
  1032. public function whenContains($needles, $callback, $default = null)
  1033. {
  1034. return $this->when($this->contains($needles), $callback, $default);
  1035. }
  1036. /**
  1037. * Execute the given callback if the string contains all array values.
  1038. *
  1039. * @param iterable<string> $needles
  1040. * @param callable $callback
  1041. * @param callable|null $default
  1042. * @return static
  1043. */
  1044. public function whenContainsAll(array $needles, $callback, $default = null)
  1045. {
  1046. return $this->when($this->containsAll($needles), $callback, $default);
  1047. }
  1048. /**
  1049. * Execute the given callback if the string is empty.
  1050. *
  1051. * @param callable $callback
  1052. * @param callable|null $default
  1053. * @return static
  1054. */
  1055. public function whenEmpty($callback, $default = null)
  1056. {
  1057. return $this->when($this->isEmpty(), $callback, $default);
  1058. }
  1059. /**
  1060. * Execute the given callback if the string is not empty.
  1061. *
  1062. * @param callable $callback
  1063. * @param callable|null $default
  1064. * @return static
  1065. */
  1066. public function whenNotEmpty($callback, $default = null)
  1067. {
  1068. return $this->when($this->isNotEmpty(), $callback, $default);
  1069. }
  1070. /**
  1071. * Execute the given callback if the string ends with a given substring.
  1072. *
  1073. * @param string|iterable<string> $needles
  1074. * @param callable $callback
  1075. * @param callable|null $default
  1076. * @return static
  1077. */
  1078. public function whenEndsWith($needles, $callback, $default = null)
  1079. {
  1080. return $this->when($this->endsWith($needles), $callback, $default);
  1081. }
  1082. /**
  1083. * Execute the given callback if the string doesn't end with a given substring.
  1084. *
  1085. * @param string|iterable<string> $needles
  1086. * @param callable $callback
  1087. * @param callable|null $default
  1088. * @return static
  1089. */
  1090. public function whenDoesntEndWith($needles, $callback, $default = null)
  1091. {
  1092. return $this->when($this->doesntEndWith($needles), $callback, $default);
  1093. }
  1094. /**
  1095. * Execute the given callback if the string is an exact match with the given value.
  1096. *
  1097. * @param string $value
  1098. * @param callable $callback
  1099. * @param callable|null $default
  1100. * @return static
  1101. */
  1102. public function whenExactly($value, $callback, $default = null)
  1103. {
  1104. return $this->when($this->exactly($value), $callback, $default);
  1105. }
  1106. /**
  1107. * Execute the given callback if the string is not an exact match with the given value.
  1108. *
  1109. * @param string $value
  1110. * @param callable $callback
  1111. * @param callable|null $default
  1112. * @return static
  1113. */
  1114. public function whenNotExactly($value, $callback, $default = null)
  1115. {
  1116. return $this->when(! $this->exactly($value), $callback, $default);
  1117. }
  1118. /**
  1119. * Execute the given callback if the string matches a given pattern.
  1120. *
  1121. * @param string|iterable<string> $pattern
  1122. * @param callable $callback
  1123. * @param callable|null $default
  1124. * @return static
  1125. */
  1126. public function whenIs($pattern, $callback, $default = null)
  1127. {
  1128. return $this->when($this->is($pattern), $callback, $default);
  1129. }
  1130. /**
  1131. * Execute the given callback if the string is 7 bit ASCII.
  1132. *
  1133. * @param callable $callback
  1134. * @param callable|null $default
  1135. * @return static
  1136. */
  1137. public function whenIsAscii($callback, $default = null)
  1138. {
  1139. return $this->when($this->isAscii(), $callback, $default);
  1140. }
  1141. /**
  1142. * Execute the given callback if the string is a valid UUID.
  1143. *
  1144. * @param callable $callback
  1145. * @param callable|null $default
  1146. * @return static
  1147. */
  1148. public function whenIsUuid($callback, $default = null)
  1149. {
  1150. return $this->when($this->isUuid(), $callback, $default);
  1151. }
  1152. /**
  1153. * Execute the given callback if the string is a valid ULID.
  1154. *
  1155. * @param callable $callback
  1156. * @param callable|null $default
  1157. * @return static
  1158. */
  1159. public function whenIsUlid($callback, $default = null)
  1160. {
  1161. return $this->when($this->isUlid(), $callback, $default);
  1162. }
  1163. /**
  1164. * Execute the given callback if the string starts with a given substring.
  1165. *
  1166. * @param string|iterable<string> $needles
  1167. * @param callable $callback
  1168. * @param callable|null $default
  1169. * @return static
  1170. */
  1171. public function whenStartsWith($needles, $callback, $default = null)
  1172. {
  1173. return $this->when($this->startsWith($needles), $callback, $default);
  1174. }
  1175. /**
  1176. * Execute the given callback if the string doesn't start with a given substring.
  1177. *
  1178. * @param string|iterable<string> $needles
  1179. * @param callable $callback
  1180. * @param callable|null $default
  1181. * @return static
  1182. */
  1183. public function whenDoesntStartWith($needles, $callback, $default = null)
  1184. {
  1185. return $this->when($this->doesntStartWith($needles), $callback, $default);
  1186. }
  1187. /**
  1188. * Execute the given callback if the string matches the given pattern.
  1189. *
  1190. * @param string $pattern
  1191. * @param callable $callback
  1192. * @param callable|null $default
  1193. * @return static
  1194. */
  1195. public function whenTest($pattern, $callback, $default = null)
  1196. {
  1197. return $this->when($this->test($pattern), $callback, $default);
  1198. }
  1199. /**
  1200. * Limit the number of words in a string.
  1201. *
  1202. * @param int $words
  1203. * @param string $end
  1204. * @return static
  1205. */
  1206. public function words($words = 100, $end = '...')
  1207. {
  1208. return new static(Str::words($this->value, $words, $end));
  1209. }
  1210. /**
  1211. * Get the number of words a string contains.
  1212. *
  1213. * @param string|null $characters
  1214. * @return int
  1215. */
  1216. public function wordCount($characters = null)
  1217. {
  1218. return Str::wordCount($this->value, $characters);
  1219. }
  1220. /**
  1221. * Wrap a string to a given number of characters.
  1222. *
  1223. * @param int $characters
  1224. * @param string $break
  1225. * @param bool $cutLongWords
  1226. * @return static
  1227. */
  1228. public function wordWrap($characters = 75, $break = "\n", $cutLongWords = false)
  1229. {
  1230. return new static(Str::wordWrap($this->value, $characters, $break, $cutLongWords));
  1231. }
  1232. /**
  1233. * Wrap the string with the given strings.
  1234. *
  1235. * @param string $before
  1236. * @param string|null $after
  1237. * @return static
  1238. */
  1239. public function wrap($before, $after = null)
  1240. {
  1241. return new static(Str::wrap($this->value, $before, $after));
  1242. }
  1243. /**
  1244. * Unwrap the string with the given strings.
  1245. *
  1246. * @param string $before
  1247. * @param string|null $after
  1248. * @return static
  1249. */
  1250. public function unwrap($before, $after = null)
  1251. {
  1252. return new static(Str::unwrap($this->value, $before, $after));
  1253. }
  1254. /**
  1255. * Convert the string into a `HtmlString` instance.
  1256. *
  1257. * @return \Illuminate\Support\HtmlString
  1258. */
  1259. public function toHtmlString()
  1260. {
  1261. return new HtmlString($this->value);
  1262. }
  1263. /**
  1264. * Convert the string to Base64 encoding.
  1265. *
  1266. * @return static
  1267. */
  1268. public function toBase64()
  1269. {
  1270. return new static(base64_encode($this->value));
  1271. }
  1272. /**
  1273. * Decode the Base64 encoded string.
  1274. *
  1275. * @param bool $strict
  1276. * @return static
  1277. */
  1278. public function fromBase64($strict = false)
  1279. {
  1280. return new static(base64_decode($this->value, $strict));
  1281. }
  1282. /**
  1283. * Hash the string using the given algorithm.
  1284. *
  1285. * @param string $algorithm
  1286. * @return static
  1287. */
  1288. public function hash(string $algorithm)
  1289. {
  1290. return new static(hash($algorithm, $this->value));
  1291. }
  1292. /**
  1293. * Encrypt the string.
  1294. *
  1295. * @param bool $serialize
  1296. * @return static
  1297. */
  1298. public function encrypt(bool $serialize = false)
  1299. {
  1300. return new static(encrypt($this->value, $serialize));
  1301. }
  1302. /**
  1303. * Decrypt the string.
  1304. *
  1305. * @param bool $serialize
  1306. * @return static
  1307. */
  1308. public function decrypt(bool $serialize = false)
  1309. {
  1310. return new static(decrypt($this->value, $serialize));
  1311. }
  1312. /**
  1313. * Dump the string.
  1314. *
  1315. * @param mixed ...$args
  1316. * @return $this
  1317. */
  1318. public function dump(...$args)
  1319. {
  1320. dump($this->value, ...$args);
  1321. return $this;
  1322. }
  1323. /**
  1324. * Get the underlying string value.
  1325. *
  1326. * @return string
  1327. */
  1328. public function value()
  1329. {
  1330. return $this->toString();
  1331. }
  1332. /**
  1333. * Get the underlying string value.
  1334. *
  1335. * @return string
  1336. */
  1337. public function toString()
  1338. {
  1339. return $this->value;
  1340. }
  1341. /**
  1342. * Get the underlying string value as an integer.
  1343. *
  1344. * @param int $base
  1345. * @return int
  1346. */
  1347. public function toInteger($base = 10)
  1348. {
  1349. return intval($this->value, $base);
  1350. }
  1351. /**
  1352. * Get the underlying string value as a float.
  1353. *
  1354. * @return float
  1355. */
  1356. public function toFloat()
  1357. {
  1358. return floatval($this->value);
  1359. }
  1360. /**
  1361. * Get the underlying string value as a boolean.
  1362. *
  1363. * Returns true when value is "1", "true", "on", and "yes". Otherwise, returns false.
  1364. *
  1365. * @return bool
  1366. */
  1367. public function toBoolean()
  1368. {
  1369. return filter_var($this->value, FILTER_VALIDATE_BOOLEAN);
  1370. }
  1371. /**
  1372. * Get the underlying string value as a Carbon instance.
  1373. *
  1374. * @param string|null $format
  1375. * @param string|null $tz
  1376. * @return \Illuminate\Support\Carbon
  1377. *
  1378. * @throws \Carbon\Exceptions\InvalidFormatException
  1379. */
  1380. public function toDate($format = null, $tz = null)
  1381. {
  1382. if (is_null($format)) {
  1383. return Date::parse($this->value, $tz);
  1384. }
  1385. return Date::createFromFormat($format, $this->value, $tz);
  1386. }
  1387. /**
  1388. * Get the underlying string value as a Uri instance.
  1389. *
  1390. * @return \Illuminate\Support\Uri
  1391. */
  1392. public function toUri()
  1393. {
  1394. return Uri::of($this->value);
  1395. }
  1396. /**
  1397. * Convert the object to a string when JSON encoded.
  1398. *
  1399. * @return string
  1400. */
  1401. public function jsonSerialize(): string
  1402. {
  1403. return $this->__toString();
  1404. }
  1405. /**
  1406. * Determine if the given offset exists.
  1407. *
  1408. * @param mixed $offset
  1409. * @return bool
  1410. */
  1411. public function offsetExists(mixed $offset): bool
  1412. {
  1413. return isset($this->value[$offset]);
  1414. }
  1415. /**
  1416. * Get the value at the given offset.
  1417. *
  1418. * @param mixed $offset
  1419. * @return string
  1420. */
  1421. public function offsetGet(mixed $offset): string
  1422. {
  1423. return $this->value[$offset];
  1424. }
  1425. /**
  1426. * Set the value at the given offset.
  1427. *
  1428. * @param mixed $offset
  1429. * @return void
  1430. */
  1431. public function offsetSet(mixed $offset, mixed $value): void
  1432. {
  1433. $this->value[$offset] = $value;
  1434. }
  1435. /**
  1436. * Unset the value at the given offset.
  1437. *
  1438. * @param mixed $offset
  1439. * @return void
  1440. */
  1441. public function offsetUnset(mixed $offset): void
  1442. {
  1443. unset($this->value[$offset]);
  1444. }
  1445. /**
  1446. * Proxy dynamic properties onto methods.
  1447. *
  1448. * @param string $key
  1449. * @return mixed
  1450. */
  1451. public function __get($key)
  1452. {
  1453. return $this->{$key}();
  1454. }
  1455. /**
  1456. * Get the raw string value.
  1457. *
  1458. * @return string
  1459. */
  1460. public function __toString()
  1461. {
  1462. return (string) $this->value;
  1463. }
  1464. }