Stringable.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Utils;
  12. use Closure;
  13. use Hyperf\Macroable\Macroable;
  14. use JsonSerializable;
  15. class Stringable implements JsonSerializable
  16. {
  17. use Traits\Conditionable;
  18. use Macroable;
  19. use Traits\Tappable;
  20. /**
  21. * The underlying string value.
  22. *
  23. * @var string
  24. */
  25. protected $value;
  26. /**
  27. * Create a new instance of the class.
  28. *
  29. * @param string $value
  30. */
  31. public function __construct($value = '')
  32. {
  33. $this->value = (string) $value;
  34. }
  35. /**
  36. * Proxy dynamic properties onto methods.
  37. *
  38. * @param string $key
  39. * @return mixed
  40. */
  41. public function __get($key)
  42. {
  43. return $this->{$key}();
  44. }
  45. /**
  46. * Get the raw string value.
  47. *
  48. * @return string
  49. */
  50. public function __toString()
  51. {
  52. return (string) $this->value;
  53. }
  54. /**
  55. * Return the remainder of a string after the first occurrence of a given value.
  56. *
  57. * @param string $search
  58. * @return static
  59. */
  60. public function after($search)
  61. {
  62. return new static(Str::after($this->value, $search));
  63. }
  64. /**
  65. * Return the remainder of a string after the last occurrence of a given value.
  66. *
  67. * @param string $search
  68. * @return static
  69. */
  70. public function afterLast($search)
  71. {
  72. return new static(Str::afterLast($this->value, $search));
  73. }
  74. /**
  75. * Append the given values to the string.
  76. *
  77. * @param array $values
  78. * @return static
  79. */
  80. public function append(...$values)
  81. {
  82. return new static($this->value . implode('', $values));
  83. }
  84. /**
  85. * Transliterate a UTF-8 value to ASCII.
  86. *
  87. * @param string $language
  88. * @return static
  89. */
  90. public function ascii($language = 'en')
  91. {
  92. return new static(Str::ascii($this->value, $language));
  93. }
  94. /**
  95. * Get the trailing name component of the path.
  96. *
  97. * @param string $suffix
  98. * @return static
  99. */
  100. public function basename($suffix = '')
  101. {
  102. return new static(basename($this->value, $suffix));
  103. }
  104. /**
  105. * Get the basename of the class path.
  106. *
  107. * @return static
  108. */
  109. public function classBasename()
  110. {
  111. return new static(class_basename($this->value));
  112. }
  113. /**
  114. * Get the portion of a string before the first occurrence of a given value.
  115. *
  116. * @param string $search
  117. * @return static
  118. */
  119. public function before($search)
  120. {
  121. return new static(Str::before($this->value, $search));
  122. }
  123. /**
  124. * Get the portion of a string before the last occurrence of a given value.
  125. *
  126. * @param string $search
  127. * @return static
  128. */
  129. public function beforeLast($search)
  130. {
  131. return new static(Str::beforeLast($this->value, $search));
  132. }
  133. /**
  134. * Get the portion of a string between two given values.
  135. *
  136. * @param string $from
  137. * @param string $to
  138. * @return static
  139. */
  140. public function between($from, $to)
  141. {
  142. return new static(Str::between($this->value, $from, $to));
  143. }
  144. /**
  145. * Convert a value to camel case.
  146. *
  147. * @return static
  148. */
  149. public function camel()
  150. {
  151. return new static(Str::camel($this->value));
  152. }
  153. /**
  154. * Determine if a given string contains a given substring.
  155. *
  156. * @param array|string $needles
  157. * @return bool
  158. */
  159. public function contains($needles)
  160. {
  161. return Str::contains($this->value, $needles);
  162. }
  163. /**
  164. * Determine if a given string contains all array values.
  165. *
  166. * @return bool
  167. */
  168. public function containsAll(array $needles)
  169. {
  170. return Str::containsAll($this->value, $needles);
  171. }
  172. /**
  173. * Get the parent directory's path.
  174. *
  175. * @param int $levels
  176. * @return static
  177. */
  178. public function dirname($levels = 1)
  179. {
  180. return new static(dirname($this->value, $levels));
  181. }
  182. /**
  183. * Determine if a given string ends with a given substring.
  184. *
  185. * @param array|string $needles
  186. * @return bool
  187. */
  188. public function endsWith($needles)
  189. {
  190. return Str::endsWith($this->value, $needles);
  191. }
  192. /**
  193. * Determine if the string is an exact match with the given value.
  194. *
  195. * @param string $value
  196. * @return bool
  197. */
  198. public function exactly($value)
  199. {
  200. return $this->value === $value;
  201. }
  202. /**
  203. * Explode the string into an array.
  204. *
  205. * @param string $delimiter
  206. * @param int $limit
  207. * @return \Hyperf\Utils\Collection
  208. */
  209. public function explode($delimiter, $limit = PHP_INT_MAX)
  210. {
  211. return collect(explode($delimiter, $this->value, $limit));
  212. }
  213. /**
  214. * Split a string using a regular expression or by length.
  215. *
  216. * @param int|string $pattern
  217. * @param int $limit
  218. * @param int $flags
  219. * @return \Hyperf\Utils\Collection
  220. */
  221. public function split($pattern, $limit = -1, $flags = 0)
  222. {
  223. if (filter_var($pattern, FILTER_VALIDATE_INT) !== false) {
  224. return collect(mb_str_split($this->value, $pattern));
  225. }
  226. $segments = preg_split($pattern, $this->value, $limit, $flags);
  227. return ! empty($segments) ? collect($segments) : collect();
  228. }
  229. /**
  230. * Cap a string with a single instance of a given value.
  231. *
  232. * @param string $cap
  233. * @return static
  234. */
  235. public function finish($cap)
  236. {
  237. return new static(Str::finish($this->value, $cap));
  238. }
  239. /**
  240. * Determine if a given string matches a given pattern.
  241. *
  242. * @param array|string $pattern
  243. * @return bool
  244. */
  245. public function is($pattern)
  246. {
  247. return Str::is($pattern, $this->value);
  248. }
  249. /**
  250. * Determine if the given string is empty.
  251. *
  252. * @return bool
  253. */
  254. public function isEmpty()
  255. {
  256. return $this->value === '';
  257. }
  258. /**
  259. * Determine if the given string is not empty.
  260. *
  261. * @return bool
  262. */
  263. public function isNotEmpty()
  264. {
  265. return ! $this->isEmpty();
  266. }
  267. /**
  268. * Convert a string to kebab case.
  269. *
  270. * @return static
  271. */
  272. public function kebab()
  273. {
  274. return new static(Str::kebab($this->value));
  275. }
  276. /**
  277. * Return the length of the given string.
  278. *
  279. * @param string $encoding
  280. * @return int
  281. */
  282. public function length($encoding = null)
  283. {
  284. return Str::length($this->value, $encoding);
  285. }
  286. /**
  287. * Limit the number of characters in a string.
  288. *
  289. * @param int $limit
  290. * @param string $end
  291. * @return static
  292. */
  293. public function limit($limit = 100, $end = '...')
  294. {
  295. return new static(Str::limit($this->value, $limit, $end));
  296. }
  297. /**
  298. * Convert the given string to lower-case.
  299. *
  300. * @return static
  301. */
  302. public function lower()
  303. {
  304. return new static(Str::lower($this->value));
  305. }
  306. /**
  307. * Get the string matching the given pattern.
  308. *
  309. * @param string $pattern
  310. * @return static
  311. */
  312. public function match($pattern)
  313. {
  314. preg_match($pattern, $this->value, $matches);
  315. if (! $matches) {
  316. return new static();
  317. }
  318. return new static($matches[1] ?? $matches[0]);
  319. }
  320. /**
  321. * Get the string matching the given pattern.
  322. *
  323. * @param string $pattern
  324. * @return \Hyperf\Utils\Collection
  325. */
  326. public function matchAll($pattern)
  327. {
  328. preg_match_all($pattern, $this->value, $matches);
  329. if (empty($matches[0])) {
  330. return collect();
  331. }
  332. return collect($matches[1] ?? $matches[0]);
  333. }
  334. /**
  335. * Determine if the string matches the given pattern.
  336. *
  337. * @param string $pattern
  338. * @return bool
  339. */
  340. public function test($pattern)
  341. {
  342. return $this->match($pattern)->isNotEmpty();
  343. }
  344. /**
  345. * Pad both sides of the string with another.
  346. *
  347. * @param int $length
  348. * @param string $pad
  349. * @return static
  350. */
  351. public function padBoth($length, $pad = ' ')
  352. {
  353. return new static(Str::padBoth($this->value, $length, $pad));
  354. }
  355. /**
  356. * Pad the left side of the string with another.
  357. *
  358. * @param int $length
  359. * @param string $pad
  360. * @return static
  361. */
  362. public function padLeft($length, $pad = ' ')
  363. {
  364. return new static(Str::padLeft($this->value, $length, $pad));
  365. }
  366. /**
  367. * Pad the right side of the string with another.
  368. *
  369. * @param int $length
  370. * @param string $pad
  371. * @return static
  372. */
  373. public function padRight($length, $pad = ' ')
  374. {
  375. return new static(Str::padRight($this->value, $length, $pad));
  376. }
  377. /**
  378. * Parse a Class@method style callback into class and method.
  379. *
  380. * @param null|string $default
  381. * @return array
  382. */
  383. public function parseCallback($default = null)
  384. {
  385. return Str::parseCallback($this->value, $default);
  386. }
  387. /**
  388. * Call the given callback and return a new string.
  389. *
  390. * @return static
  391. */
  392. public function pipe(callable $callback)
  393. {
  394. return new static(call_user_func($callback, $this));
  395. }
  396. /**
  397. * Get the plural form of an English word.
  398. *
  399. * @param int $count
  400. * @return static
  401. */
  402. public function plural($count = 2)
  403. {
  404. return new static(Str::plural($this->value, $count));
  405. }
  406. /**
  407. * Pluralize the last word of an English, studly caps case string.
  408. *
  409. * @param int $count
  410. * @return static
  411. */
  412. public function pluralStudly($count = 2)
  413. {
  414. return new static(Str::pluralStudly($this->value, $count));
  415. }
  416. /**
  417. * Prepend the given values to the string.
  418. *
  419. * @param array $values
  420. * @return static
  421. */
  422. public function prepend(...$values)
  423. {
  424. return new static(implode('', $values) . $this->value);
  425. }
  426. /**
  427. * Remove any occurrence of the given string in the subject.
  428. *
  429. * @param array<string>|string $search
  430. * @param bool $caseSensitive
  431. * @return static
  432. */
  433. public function remove($search, $caseSensitive = true)
  434. {
  435. return new static(Str::remove($search, $this->value, $caseSensitive));
  436. }
  437. /**
  438. * Repeat the string.
  439. *
  440. * @return static
  441. */
  442. public function repeat(int $times)
  443. {
  444. return new static(Str::repeat($this->value, $times));
  445. }
  446. /**
  447. * Replace the given value in the given string.
  448. *
  449. * @param string|string[] $search
  450. * @param string|string[] $replace
  451. * @return static
  452. */
  453. public function replace($search, $replace)
  454. {
  455. return new static(Str::replace($search, $replace, $this->value));
  456. }
  457. /**
  458. * Replace a given value in the string sequentially with an array.
  459. *
  460. * @param string $search
  461. * @return static
  462. */
  463. public function replaceArray($search, array $replace)
  464. {
  465. return new static(Str::replaceArray($search, $replace, $this->value));
  466. }
  467. /**
  468. * Replace the first occurrence of a given value in the string.
  469. *
  470. * @param string $search
  471. * @param string $replace
  472. * @return static
  473. */
  474. public function replaceFirst($search, $replace)
  475. {
  476. return new static(Str::replaceFirst($search, $replace, $this->value));
  477. }
  478. /**
  479. * Replace the last occurrence of a given value in the string.
  480. *
  481. * @param string $search
  482. * @param string $replace
  483. * @return static
  484. */
  485. public function replaceLast($search, $replace)
  486. {
  487. return new static(Str::replaceLast($search, $replace, $this->value));
  488. }
  489. /**
  490. * Replace the patterns matching the given regular expression.
  491. *
  492. * @param string $pattern
  493. * @param \Closure|string $replace
  494. * @param int $limit
  495. * @return static
  496. */
  497. public function replaceMatches($pattern, $replace, $limit = -1)
  498. {
  499. if ($replace instanceof Closure) {
  500. return new static(preg_replace_callback($pattern, $replace, $this->value, $limit));
  501. }
  502. return new static(preg_replace($pattern, $replace, $this->value, $limit));
  503. }
  504. /**
  505. * Begin a string with a single instance of a given value.
  506. *
  507. * @param string $prefix
  508. * @return static
  509. */
  510. public function start($prefix)
  511. {
  512. return new static(Str::start($this->value, $prefix));
  513. }
  514. /**
  515. * Strip HTML and PHP tags from the given string.
  516. *
  517. * @param null|string|string[] $allowedTags
  518. * @return static
  519. */
  520. public function stripTags($allowedTags = null)
  521. {
  522. return new static(strip_tags($this->value, $allowedTags));
  523. }
  524. /**
  525. * Convert the given string to upper-case.
  526. *
  527. * @return static
  528. */
  529. public function upper()
  530. {
  531. return new static(Str::upper($this->value));
  532. }
  533. /**
  534. * Convert the given string to title case.
  535. *
  536. * @return static
  537. */
  538. public function title()
  539. {
  540. return new static(Str::title($this->value));
  541. }
  542. /**
  543. * Get the singular form of an English word.
  544. *
  545. * @return static
  546. */
  547. public function singular()
  548. {
  549. return new static(Str::singular($this->value));
  550. }
  551. /**
  552. * Generate a URL friendly "slug" from a given string.
  553. *
  554. * @param string $separator
  555. * @param null|string $language
  556. * @return static
  557. */
  558. public function slug($separator = '-', $language = 'en')
  559. {
  560. return new static(Str::slug($this->value, $separator, $language));
  561. }
  562. /**
  563. * Convert a string to snake case.
  564. *
  565. * @param string $delimiter
  566. * @return static
  567. */
  568. public function snake($delimiter = '_')
  569. {
  570. return new static(Str::snake($this->value, $delimiter));
  571. }
  572. /**
  573. * Determine if a given string starts with a given substring.
  574. *
  575. * @param array|string $needles
  576. * @return bool
  577. */
  578. public function startsWith($needles)
  579. {
  580. return Str::startsWith($this->value, $needles);
  581. }
  582. /**
  583. * Convert a value to studly caps case.
  584. *
  585. * @return static
  586. */
  587. public function studly()
  588. {
  589. return new static(Str::studly($this->value));
  590. }
  591. /**
  592. * Returns the portion of the string specified by the start and length parameters.
  593. *
  594. * @param int $start
  595. * @param null|int $length
  596. * @return static
  597. */
  598. public function substr($start, $length = null)
  599. {
  600. return new static(Str::substr($this->value, $start, $length));
  601. }
  602. /**
  603. * Returns the number of substring occurrences.
  604. *
  605. * @param string $needle
  606. * @param null|int $offset
  607. * @param null|int $length
  608. * @return int
  609. */
  610. public function substrCount($needle, $offset = null, $length = null)
  611. {
  612. return Str::substrCount($this->value, $needle, $offset ?? 0, $length);
  613. }
  614. /**
  615. * Trim the string of the given characters.
  616. *
  617. * @param string $characters
  618. * @return static
  619. */
  620. public function trim($characters = null)
  621. {
  622. return new static(trim(...array_merge([$this->value], func_get_args())));
  623. }
  624. /**
  625. * Left trim the string of the given characters.
  626. *
  627. * @param string $characters
  628. * @return static
  629. */
  630. public function ltrim($characters = null)
  631. {
  632. return new static(ltrim(...array_merge([$this->value], func_get_args())));
  633. }
  634. /**
  635. * Right trim the string of the given characters.
  636. *
  637. * @param string $characters
  638. * @return static
  639. */
  640. public function rtrim($characters = null)
  641. {
  642. return new static(rtrim(...array_merge([$this->value], func_get_args())));
  643. }
  644. /**
  645. * Make a string's first character uppercase.
  646. *
  647. * @return static
  648. */
  649. public function ucfirst()
  650. {
  651. return new static(Str::ucfirst($this->value));
  652. }
  653. /**
  654. * Replaces the first or the last ones chars from a string by a given char.
  655. *
  656. * @param int $offset if is negative it starts from the end
  657. * @param string $replacement default is *
  658. * @return static
  659. */
  660. public function mask(int $offset = 0, int $length = 0, string $replacement = '*')
  661. {
  662. return new static(Str::mask($this->value, $offset, $length, $replacement));
  663. }
  664. /**
  665. * Execute the given callback if the string is empty.
  666. *
  667. * @param callable $callback
  668. * @return static
  669. */
  670. public function whenEmpty($callback)
  671. {
  672. if ($this->isEmpty()) {
  673. $result = $callback($this);
  674. return is_null($result) ? $this : $result;
  675. }
  676. return $this;
  677. }
  678. /**
  679. * Execute the given callback if the string is not empty.
  680. *
  681. * @param callable $callback
  682. * @return static
  683. */
  684. public function whenNotEmpty($callback)
  685. {
  686. if ($this->isNotEmpty()) {
  687. $result = $callback($this);
  688. return is_null($result) ? $this : $result;
  689. }
  690. return $this;
  691. }
  692. /**
  693. * Limit the number of words in a string.
  694. *
  695. * @param int $words
  696. * @param string $end
  697. * @return static
  698. */
  699. public function words($words = 100, $end = '...')
  700. {
  701. return new static(Str::words($this->value, $words, $end));
  702. }
  703. /**
  704. * Get the number of words a string contains.
  705. *
  706. * @return int
  707. */
  708. public function wordCount()
  709. {
  710. return str_word_count($this->value);
  711. }
  712. /**
  713. * Convert the object to a string when JSON encoded.
  714. *
  715. * @return string
  716. */
  717. public function jsonSerialize()
  718. {
  719. return $this->__toString();
  720. }
  721. }