AbstractString.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\String;
  11. use Symfony\Component\String\Exception\ExceptionInterface;
  12. use Symfony\Component\String\Exception\InvalidArgumentException;
  13. use Symfony\Component\String\Exception\RuntimeException;
  14. /**
  15. * Represents a string of abstract characters.
  16. *
  17. * Unicode defines 3 types of "characters" (bytes, code points and grapheme clusters).
  18. * This class is the abstract type to use as a type-hint when the logic you want to
  19. * implement doesn't care about the exact variant it deals with.
  20. *
  21. * @author Nicolas Grekas <p@tchwork.com>
  22. * @author Hugo Hamon <hugohamon@neuf.fr>
  23. *
  24. * @throws ExceptionInterface
  25. */
  26. abstract class AbstractString implements \Stringable, \JsonSerializable
  27. {
  28. public const PREG_PATTERN_ORDER = \PREG_PATTERN_ORDER;
  29. public const PREG_SET_ORDER = \PREG_SET_ORDER;
  30. public const PREG_OFFSET_CAPTURE = \PREG_OFFSET_CAPTURE;
  31. public const PREG_UNMATCHED_AS_NULL = \PREG_UNMATCHED_AS_NULL;
  32. public const PREG_SPLIT = 0;
  33. public const PREG_SPLIT_NO_EMPTY = \PREG_SPLIT_NO_EMPTY;
  34. public const PREG_SPLIT_DELIM_CAPTURE = \PREG_SPLIT_DELIM_CAPTURE;
  35. public const PREG_SPLIT_OFFSET_CAPTURE = \PREG_SPLIT_OFFSET_CAPTURE;
  36. protected $string = '';
  37. protected $ignoreCase = false;
  38. abstract public function __construct(string $string = '');
  39. /**
  40. * Unwraps instances of AbstractString back to strings.
  41. *
  42. * @return string[]|array
  43. */
  44. public static function unwrap(array $values): array
  45. {
  46. foreach ($values as $k => $v) {
  47. if ($v instanceof self) {
  48. $values[$k] = $v->__toString();
  49. } elseif (\is_array($v) && $values[$k] !== $v = static::unwrap($v)) {
  50. $values[$k] = $v;
  51. }
  52. }
  53. return $values;
  54. }
  55. /**
  56. * Wraps (and normalizes) strings in instances of AbstractString.
  57. *
  58. * @return static[]|array
  59. */
  60. public static function wrap(array $values): array
  61. {
  62. $i = 0;
  63. $keys = null;
  64. foreach ($values as $k => $v) {
  65. if (\is_string($k) && '' !== $k && $k !== $j = (string) new static($k)) {
  66. $keys = $keys ?? array_keys($values);
  67. $keys[$i] = $j;
  68. }
  69. if (\is_string($v)) {
  70. $values[$k] = new static($v);
  71. } elseif (\is_array($v) && $values[$k] !== $v = static::wrap($v)) {
  72. $values[$k] = $v;
  73. }
  74. ++$i;
  75. }
  76. return null !== $keys ? array_combine($keys, $values) : $values;
  77. }
  78. /**
  79. * @param string|string[] $needle
  80. *
  81. * @return static
  82. */
  83. public function after($needle, bool $includeNeedle = false, int $offset = 0): self
  84. {
  85. $str = clone $this;
  86. $i = \PHP_INT_MAX;
  87. foreach ((array) $needle as $n) {
  88. $n = (string) $n;
  89. $j = $this->indexOf($n, $offset);
  90. if (null !== $j && $j < $i) {
  91. $i = $j;
  92. $str->string = $n;
  93. }
  94. }
  95. if (\PHP_INT_MAX === $i) {
  96. return $str;
  97. }
  98. if (!$includeNeedle) {
  99. $i += $str->length();
  100. }
  101. return $this->slice($i);
  102. }
  103. /**
  104. * @param string|string[] $needle
  105. *
  106. * @return static
  107. */
  108. public function afterLast($needle, bool $includeNeedle = false, int $offset = 0): self
  109. {
  110. $str = clone $this;
  111. $i = null;
  112. foreach ((array) $needle as $n) {
  113. $n = (string) $n;
  114. $j = $this->indexOfLast($n, $offset);
  115. if (null !== $j && $j >= $i) {
  116. $i = $offset = $j;
  117. $str->string = $n;
  118. }
  119. }
  120. if (null === $i) {
  121. return $str;
  122. }
  123. if (!$includeNeedle) {
  124. $i += $str->length();
  125. }
  126. return $this->slice($i);
  127. }
  128. /**
  129. * @return static
  130. */
  131. abstract public function append(string ...$suffix): self;
  132. /**
  133. * @param string|string[] $needle
  134. *
  135. * @return static
  136. */
  137. public function before($needle, bool $includeNeedle = false, int $offset = 0): self
  138. {
  139. $str = clone $this;
  140. $i = \PHP_INT_MAX;
  141. foreach ((array) $needle as $n) {
  142. $n = (string) $n;
  143. $j = $this->indexOf($n, $offset);
  144. if (null !== $j && $j < $i) {
  145. $i = $j;
  146. $str->string = $n;
  147. }
  148. }
  149. if (\PHP_INT_MAX === $i) {
  150. return $str;
  151. }
  152. if ($includeNeedle) {
  153. $i += $str->length();
  154. }
  155. return $this->slice(0, $i);
  156. }
  157. /**
  158. * @param string|string[] $needle
  159. *
  160. * @return static
  161. */
  162. public function beforeLast($needle, bool $includeNeedle = false, int $offset = 0): self
  163. {
  164. $str = clone $this;
  165. $i = null;
  166. foreach ((array) $needle as $n) {
  167. $n = (string) $n;
  168. $j = $this->indexOfLast($n, $offset);
  169. if (null !== $j && $j >= $i) {
  170. $i = $offset = $j;
  171. $str->string = $n;
  172. }
  173. }
  174. if (null === $i) {
  175. return $str;
  176. }
  177. if ($includeNeedle) {
  178. $i += $str->length();
  179. }
  180. return $this->slice(0, $i);
  181. }
  182. /**
  183. * @return int[]
  184. */
  185. public function bytesAt(int $offset): array
  186. {
  187. $str = $this->slice($offset, 1);
  188. return '' === $str->string ? [] : array_values(unpack('C*', $str->string));
  189. }
  190. /**
  191. * @return static
  192. */
  193. abstract public function camel(): self;
  194. /**
  195. * @return static[]
  196. */
  197. abstract public function chunk(int $length = 1): array;
  198. /**
  199. * @return static
  200. */
  201. public function collapseWhitespace(): self
  202. {
  203. $str = clone $this;
  204. $str->string = trim(preg_replace("/(?:[ \n\r\t\x0C]{2,}+|[\n\r\t\x0C])/", ' ', $str->string), " \n\r\t\x0C");
  205. return $str;
  206. }
  207. /**
  208. * @param string|string[] $needle
  209. */
  210. public function containsAny($needle): bool
  211. {
  212. return null !== $this->indexOf($needle);
  213. }
  214. /**
  215. * @param string|string[] $suffix
  216. */
  217. public function endsWith($suffix): bool
  218. {
  219. if (!\is_array($suffix) && !$suffix instanceof \Traversable) {
  220. throw new \TypeError(sprintf('Method "%s()" must be overridden by class "%s" to deal with non-iterable values.', __FUNCTION__, static::class));
  221. }
  222. foreach ($suffix as $s) {
  223. if ($this->endsWith((string) $s)) {
  224. return true;
  225. }
  226. }
  227. return false;
  228. }
  229. /**
  230. * @return static
  231. */
  232. public function ensureEnd(string $suffix): self
  233. {
  234. if (!$this->endsWith($suffix)) {
  235. return $this->append($suffix);
  236. }
  237. $suffix = preg_quote($suffix);
  238. $regex = '{('.$suffix.')(?:'.$suffix.')++$}D';
  239. return $this->replaceMatches($regex.($this->ignoreCase ? 'i' : ''), '$1');
  240. }
  241. /**
  242. * @return static
  243. */
  244. public function ensureStart(string $prefix): self
  245. {
  246. $prefix = new static($prefix);
  247. if (!$this->startsWith($prefix)) {
  248. return $this->prepend($prefix);
  249. }
  250. $str = clone $this;
  251. $i = $prefixLen = $prefix->length();
  252. while ($this->indexOf($prefix, $i) === $i) {
  253. $str = $str->slice($prefixLen);
  254. $i += $prefixLen;
  255. }
  256. return $str;
  257. }
  258. /**
  259. * @param string|string[] $string
  260. */
  261. public function equalsTo($string): bool
  262. {
  263. if (!\is_array($string) && !$string instanceof \Traversable) {
  264. throw new \TypeError(sprintf('Method "%s()" must be overridden by class "%s" to deal with non-iterable values.', __FUNCTION__, static::class));
  265. }
  266. foreach ($string as $s) {
  267. if ($this->equalsTo((string) $s)) {
  268. return true;
  269. }
  270. }
  271. return false;
  272. }
  273. /**
  274. * @return static
  275. */
  276. abstract public function folded(): self;
  277. /**
  278. * @return static
  279. */
  280. public function ignoreCase(): self
  281. {
  282. $str = clone $this;
  283. $str->ignoreCase = true;
  284. return $str;
  285. }
  286. /**
  287. * @param string|string[] $needle
  288. */
  289. public function indexOf($needle, int $offset = 0): ?int
  290. {
  291. if (!\is_array($needle) && !$needle instanceof \Traversable) {
  292. throw new \TypeError(sprintf('Method "%s()" must be overridden by class "%s" to deal with non-iterable values.', __FUNCTION__, static::class));
  293. }
  294. $i = \PHP_INT_MAX;
  295. foreach ($needle as $n) {
  296. $j = $this->indexOf((string) $n, $offset);
  297. if (null !== $j && $j < $i) {
  298. $i = $j;
  299. }
  300. }
  301. return \PHP_INT_MAX === $i ? null : $i;
  302. }
  303. /**
  304. * @param string|string[] $needle
  305. */
  306. public function indexOfLast($needle, int $offset = 0): ?int
  307. {
  308. if (!\is_array($needle) && !$needle instanceof \Traversable) {
  309. throw new \TypeError(sprintf('Method "%s()" must be overridden by class "%s" to deal with non-iterable values.', __FUNCTION__, static::class));
  310. }
  311. $i = null;
  312. foreach ($needle as $n) {
  313. $j = $this->indexOfLast((string) $n, $offset);
  314. if (null !== $j && $j >= $i) {
  315. $i = $offset = $j;
  316. }
  317. }
  318. return $i;
  319. }
  320. public function isEmpty(): bool
  321. {
  322. return '' === $this->string;
  323. }
  324. /**
  325. * @return static
  326. */
  327. abstract public function join(array $strings, string $lastGlue = null): self;
  328. public function jsonSerialize(): string
  329. {
  330. return $this->string;
  331. }
  332. abstract public function length(): int;
  333. /**
  334. * @return static
  335. */
  336. abstract public function lower(): self;
  337. /**
  338. * Matches the string using a regular expression.
  339. *
  340. * Pass PREG_PATTERN_ORDER or PREG_SET_ORDER as $flags to get all occurrences matching the regular expression.
  341. *
  342. * @return array All matches in a multi-dimensional array ordered according to flags
  343. */
  344. abstract public function match(string $regexp, int $flags = 0, int $offset = 0): array;
  345. /**
  346. * @return static
  347. */
  348. abstract public function padBoth(int $length, string $padStr = ' '): self;
  349. /**
  350. * @return static
  351. */
  352. abstract public function padEnd(int $length, string $padStr = ' '): self;
  353. /**
  354. * @return static
  355. */
  356. abstract public function padStart(int $length, string $padStr = ' '): self;
  357. /**
  358. * @return static
  359. */
  360. abstract public function prepend(string ...$prefix): self;
  361. /**
  362. * @return static
  363. */
  364. public function repeat(int $multiplier): self
  365. {
  366. if (0 > $multiplier) {
  367. throw new InvalidArgumentException(sprintf('Multiplier must be positive, %d given.', $multiplier));
  368. }
  369. $str = clone $this;
  370. $str->string = str_repeat($str->string, $multiplier);
  371. return $str;
  372. }
  373. /**
  374. * @return static
  375. */
  376. abstract public function replace(string $from, string $to): self;
  377. /**
  378. * @param string|callable $to
  379. *
  380. * @return static
  381. */
  382. abstract public function replaceMatches(string $fromRegexp, $to): self;
  383. /**
  384. * @return static
  385. */
  386. abstract public function reverse(): self;
  387. /**
  388. * @return static
  389. */
  390. abstract public function slice(int $start = 0, int $length = null): self;
  391. /**
  392. * @return static
  393. */
  394. abstract public function snake(): self;
  395. /**
  396. * @return static
  397. */
  398. abstract public function splice(string $replacement, int $start = 0, int $length = null): self;
  399. /**
  400. * @return static[]
  401. */
  402. public function split(string $delimiter, int $limit = null, int $flags = null): array
  403. {
  404. if (null === $flags) {
  405. throw new \TypeError('Split behavior when $flags is null must be implemented by child classes.');
  406. }
  407. if ($this->ignoreCase) {
  408. $delimiter .= 'i';
  409. }
  410. set_error_handler(static function ($t, $m) { throw new InvalidArgumentException($m); });
  411. try {
  412. if (false === $chunks = preg_split($delimiter, $this->string, $limit, $flags)) {
  413. $lastError = preg_last_error();
  414. foreach (get_defined_constants(true)['pcre'] as $k => $v) {
  415. if ($lastError === $v && '_ERROR' === substr($k, -6)) {
  416. throw new RuntimeException('Splitting failed with '.$k.'.');
  417. }
  418. }
  419. throw new RuntimeException('Splitting failed with unknown error code.');
  420. }
  421. } finally {
  422. restore_error_handler();
  423. }
  424. $str = clone $this;
  425. if (self::PREG_SPLIT_OFFSET_CAPTURE & $flags) {
  426. foreach ($chunks as &$chunk) {
  427. $str->string = $chunk[0];
  428. $chunk[0] = clone $str;
  429. }
  430. } else {
  431. foreach ($chunks as &$chunk) {
  432. $str->string = $chunk;
  433. $chunk = clone $str;
  434. }
  435. }
  436. return $chunks;
  437. }
  438. /**
  439. * @param string|string[] $prefix
  440. */
  441. public function startsWith($prefix): bool
  442. {
  443. if (!\is_array($prefix) && !$prefix instanceof \Traversable) {
  444. throw new \TypeError(sprintf('Method "%s()" must be overridden by class "%s" to deal with non-iterable values.', __FUNCTION__, static::class));
  445. }
  446. foreach ($prefix as $prefix) {
  447. if ($this->startsWith((string) $prefix)) {
  448. return true;
  449. }
  450. }
  451. return false;
  452. }
  453. /**
  454. * @return static
  455. */
  456. abstract public function title(bool $allWords = false): self;
  457. public function toByteString(string $toEncoding = null): ByteString
  458. {
  459. $b = new ByteString();
  460. $toEncoding = \in_array($toEncoding, ['utf8', 'utf-8', 'UTF8'], true) ? 'UTF-8' : $toEncoding;
  461. if (null === $toEncoding || $toEncoding === $fromEncoding = $this instanceof AbstractUnicodeString || preg_match('//u', $b->string) ? 'UTF-8' : 'Windows-1252') {
  462. $b->string = $this->string;
  463. return $b;
  464. }
  465. set_error_handler(static function ($t, $m) { throw new InvalidArgumentException($m); });
  466. try {
  467. try {
  468. $b->string = mb_convert_encoding($this->string, $toEncoding, 'UTF-8');
  469. } catch (InvalidArgumentException|\ValueError $e) {
  470. if (!\function_exists('iconv')) {
  471. if ($e instanceof \ValueError) {
  472. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  473. }
  474. throw $e;
  475. }
  476. $b->string = iconv('UTF-8', $toEncoding, $this->string);
  477. }
  478. } finally {
  479. restore_error_handler();
  480. }
  481. return $b;
  482. }
  483. public function toCodePointString(): CodePointString
  484. {
  485. return new CodePointString($this->string);
  486. }
  487. public function toString(): string
  488. {
  489. return $this->string;
  490. }
  491. public function toUnicodeString(): UnicodeString
  492. {
  493. return new UnicodeString($this->string);
  494. }
  495. /**
  496. * @return static
  497. */
  498. abstract public function trim(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): self;
  499. /**
  500. * @return static
  501. */
  502. abstract public function trimEnd(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): self;
  503. /**
  504. * @param string|string[] $prefix
  505. *
  506. * @return static
  507. */
  508. public function trimPrefix($prefix): self
  509. {
  510. if (\is_array($prefix) || $prefix instanceof \Traversable) {
  511. foreach ($prefix as $s) {
  512. $t = $this->trimPrefix($s);
  513. if ($t->string !== $this->string) {
  514. return $t;
  515. }
  516. }
  517. return clone $this;
  518. }
  519. $str = clone $this;
  520. if ($prefix instanceof self) {
  521. $prefix = $prefix->string;
  522. } else {
  523. $prefix = (string) $prefix;
  524. }
  525. if ('' !== $prefix && \strlen($this->string) >= \strlen($prefix) && 0 === substr_compare($this->string, $prefix, 0, \strlen($prefix), $this->ignoreCase)) {
  526. $str->string = substr($this->string, \strlen($prefix));
  527. }
  528. return $str;
  529. }
  530. /**
  531. * @return static
  532. */
  533. abstract public function trimStart(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): self;
  534. /**
  535. * @param string|string[] $suffix
  536. *
  537. * @return static
  538. */
  539. public function trimSuffix($suffix): self
  540. {
  541. if (\is_array($suffix) || $suffix instanceof \Traversable) {
  542. foreach ($suffix as $s) {
  543. $t = $this->trimSuffix($s);
  544. if ($t->string !== $this->string) {
  545. return $t;
  546. }
  547. }
  548. return clone $this;
  549. }
  550. $str = clone $this;
  551. if ($suffix instanceof self) {
  552. $suffix = $suffix->string;
  553. } else {
  554. $suffix = (string) $suffix;
  555. }
  556. if ('' !== $suffix && \strlen($this->string) >= \strlen($suffix) && 0 === substr_compare($this->string, $suffix, -\strlen($suffix), null, $this->ignoreCase)) {
  557. $str->string = substr($this->string, 0, -\strlen($suffix));
  558. }
  559. return $str;
  560. }
  561. /**
  562. * @return static
  563. */
  564. public function truncate(int $length, string $ellipsis = '', bool $cut = true): self
  565. {
  566. $stringLength = $this->length();
  567. if ($stringLength <= $length) {
  568. return clone $this;
  569. }
  570. $ellipsisLength = '' !== $ellipsis ? (new static($ellipsis))->length() : 0;
  571. if ($length < $ellipsisLength) {
  572. $ellipsisLength = 0;
  573. }
  574. if (!$cut) {
  575. if (null === $length = $this->indexOf([' ', "\r", "\n", "\t"], ($length ?: 1) - 1)) {
  576. return clone $this;
  577. }
  578. $length += $ellipsisLength;
  579. }
  580. $str = $this->slice(0, $length - $ellipsisLength);
  581. return $ellipsisLength ? $str->trimEnd()->append($ellipsis) : $str;
  582. }
  583. /**
  584. * @return static
  585. */
  586. abstract public function upper(): self;
  587. /**
  588. * Returns the printable length on a terminal.
  589. */
  590. abstract public function width(bool $ignoreAnsiDecoration = true): int;
  591. /**
  592. * @return static
  593. */
  594. public function wordwrap(int $width = 75, string $break = "\n", bool $cut = false): self
  595. {
  596. $lines = '' !== $break ? $this->split($break) : [clone $this];
  597. $chars = [];
  598. $mask = '';
  599. if (1 === \count($lines) && '' === $lines[0]->string) {
  600. return $lines[0];
  601. }
  602. foreach ($lines as $i => $line) {
  603. if ($i) {
  604. $chars[] = $break;
  605. $mask .= '#';
  606. }
  607. foreach ($line->chunk() as $char) {
  608. $chars[] = $char->string;
  609. $mask .= ' ' === $char->string ? ' ' : '?';
  610. }
  611. }
  612. $string = '';
  613. $j = 0;
  614. $b = $i = -1;
  615. $mask = wordwrap($mask, $width, '#', $cut);
  616. while (false !== $b = strpos($mask, '#', $b + 1)) {
  617. for (++$i; $i < $b; ++$i) {
  618. $string .= $chars[$j];
  619. unset($chars[$j++]);
  620. }
  621. if ($break === $chars[$j] || ' ' === $chars[$j]) {
  622. unset($chars[$j++]);
  623. }
  624. $string .= $break;
  625. }
  626. $str = clone $this;
  627. $str->string = $string.implode('', $chars);
  628. return $str;
  629. }
  630. public function __sleep(): array
  631. {
  632. return ['string'];
  633. }
  634. public function __clone()
  635. {
  636. $this->ignoreCase = false;
  637. }
  638. public function __toString(): string
  639. {
  640. return $this->string;
  641. }
  642. }