AbstractString.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. $str->string = '';
  87. $i = \PHP_INT_MAX;
  88. foreach ((array) $needle as $n) {
  89. $n = (string) $n;
  90. $j = $this->indexOf($n, $offset);
  91. if (null !== $j && $j < $i) {
  92. $i = $j;
  93. $str->string = $n;
  94. }
  95. }
  96. if (\PHP_INT_MAX === $i) {
  97. return $str;
  98. }
  99. if (!$includeNeedle) {
  100. $i += $str->length();
  101. }
  102. return $this->slice($i);
  103. }
  104. /**
  105. * @param string|string[] $needle
  106. *
  107. * @return static
  108. */
  109. public function afterLast($needle, bool $includeNeedle = false, int $offset = 0): self
  110. {
  111. $str = clone $this;
  112. $str->string = '';
  113. $i = null;
  114. foreach ((array) $needle as $n) {
  115. $n = (string) $n;
  116. $j = $this->indexOfLast($n, $offset);
  117. if (null !== $j && $j >= $i) {
  118. $i = $offset = $j;
  119. $str->string = $n;
  120. }
  121. }
  122. if (null === $i) {
  123. return $str;
  124. }
  125. if (!$includeNeedle) {
  126. $i += $str->length();
  127. }
  128. return $this->slice($i);
  129. }
  130. /**
  131. * @return static
  132. */
  133. abstract public function append(string ...$suffix): self;
  134. /**
  135. * @param string|string[] $needle
  136. *
  137. * @return static
  138. */
  139. public function before($needle, bool $includeNeedle = false, int $offset = 0): self
  140. {
  141. $str = clone $this;
  142. $str->string = '';
  143. $i = \PHP_INT_MAX;
  144. foreach ((array) $needle as $n) {
  145. $n = (string) $n;
  146. $j = $this->indexOf($n, $offset);
  147. if (null !== $j && $j < $i) {
  148. $i = $j;
  149. $str->string = $n;
  150. }
  151. }
  152. if (\PHP_INT_MAX === $i) {
  153. return $str;
  154. }
  155. if ($includeNeedle) {
  156. $i += $str->length();
  157. }
  158. return $this->slice(0, $i);
  159. }
  160. /**
  161. * @param string|string[] $needle
  162. *
  163. * @return static
  164. */
  165. public function beforeLast($needle, bool $includeNeedle = false, int $offset = 0): self
  166. {
  167. $str = clone $this;
  168. $str->string = '';
  169. $i = null;
  170. foreach ((array) $needle as $n) {
  171. $n = (string) $n;
  172. $j = $this->indexOfLast($n, $offset);
  173. if (null !== $j && $j >= $i) {
  174. $i = $offset = $j;
  175. $str->string = $n;
  176. }
  177. }
  178. if (null === $i) {
  179. return $str;
  180. }
  181. if ($includeNeedle) {
  182. $i += $str->length();
  183. }
  184. return $this->slice(0, $i);
  185. }
  186. /**
  187. * @return int[]
  188. */
  189. public function bytesAt(int $offset): array
  190. {
  191. $str = $this->slice($offset, 1);
  192. return '' === $str->string ? [] : array_values(unpack('C*', $str->string));
  193. }
  194. /**
  195. * @return static
  196. */
  197. abstract public function camel(): self;
  198. /**
  199. * @return static[]
  200. */
  201. abstract public function chunk(int $length = 1): array;
  202. /**
  203. * @return static
  204. */
  205. public function collapseWhitespace(): self
  206. {
  207. $str = clone $this;
  208. $str->string = trim(preg_replace('/(?:\s{2,}+|[^\S ])/', ' ', $str->string));
  209. return $str;
  210. }
  211. /**
  212. * @param string|string[] $needle
  213. */
  214. public function containsAny($needle): bool
  215. {
  216. return null !== $this->indexOf($needle);
  217. }
  218. /**
  219. * @param string|string[] $suffix
  220. */
  221. public function endsWith($suffix): bool
  222. {
  223. if (!\is_array($suffix) && !$suffix instanceof \Traversable) {
  224. throw new \TypeError(sprintf('Method "%s()" must be overridden by class "%s" to deal with non-iterable values.', __FUNCTION__, static::class));
  225. }
  226. foreach ($suffix as $s) {
  227. if ($this->endsWith((string) $s)) {
  228. return true;
  229. }
  230. }
  231. return false;
  232. }
  233. /**
  234. * @return static
  235. */
  236. public function ensureEnd(string $suffix): self
  237. {
  238. if (!$this->endsWith($suffix)) {
  239. return $this->append($suffix);
  240. }
  241. $suffix = preg_quote($suffix);
  242. $regex = '{('.$suffix.')(?:'.$suffix.')++$}D';
  243. return $this->replaceMatches($regex.($this->ignoreCase ? 'i' : ''), '$1');
  244. }
  245. /**
  246. * @return static
  247. */
  248. public function ensureStart(string $prefix): self
  249. {
  250. $prefix = new static($prefix);
  251. if (!$this->startsWith($prefix)) {
  252. return $this->prepend($prefix);
  253. }
  254. $str = clone $this;
  255. $i = $prefixLen = $prefix->length();
  256. while ($this->indexOf($prefix, $i) === $i) {
  257. $str = $str->slice($prefixLen);
  258. $i += $prefixLen;
  259. }
  260. return $str;
  261. }
  262. /**
  263. * @param string|string[] $string
  264. */
  265. public function equalsTo($string): bool
  266. {
  267. if (!\is_array($string) && !$string instanceof \Traversable) {
  268. throw new \TypeError(sprintf('Method "%s()" must be overridden by class "%s" to deal with non-iterable values.', __FUNCTION__, static::class));
  269. }
  270. foreach ($string as $s) {
  271. if ($this->equalsTo((string) $s)) {
  272. return true;
  273. }
  274. }
  275. return false;
  276. }
  277. /**
  278. * @return static
  279. */
  280. abstract public function folded(): self;
  281. /**
  282. * @return static
  283. */
  284. public function ignoreCase(): self
  285. {
  286. $str = clone $this;
  287. $str->ignoreCase = true;
  288. return $str;
  289. }
  290. /**
  291. * @param string|string[] $needle
  292. */
  293. public function indexOf($needle, int $offset = 0): ?int
  294. {
  295. if (!\is_array($needle) && !$needle instanceof \Traversable) {
  296. throw new \TypeError(sprintf('Method "%s()" must be overridden by class "%s" to deal with non-iterable values.', __FUNCTION__, static::class));
  297. }
  298. $i = \PHP_INT_MAX;
  299. foreach ($needle as $n) {
  300. $j = $this->indexOf((string) $n, $offset);
  301. if (null !== $j && $j < $i) {
  302. $i = $j;
  303. }
  304. }
  305. return \PHP_INT_MAX === $i ? null : $i;
  306. }
  307. /**
  308. * @param string|string[] $needle
  309. */
  310. public function indexOfLast($needle, int $offset = 0): ?int
  311. {
  312. if (!\is_array($needle) && !$needle instanceof \Traversable) {
  313. throw new \TypeError(sprintf('Method "%s()" must be overridden by class "%s" to deal with non-iterable values.', __FUNCTION__, static::class));
  314. }
  315. $i = null;
  316. foreach ($needle as $n) {
  317. $j = $this->indexOfLast((string) $n, $offset);
  318. if (null !== $j && $j >= $i) {
  319. $i = $offset = $j;
  320. }
  321. }
  322. return $i;
  323. }
  324. public function isEmpty(): bool
  325. {
  326. return '' === $this->string;
  327. }
  328. /**
  329. * @return static
  330. */
  331. abstract public function join(array $strings, string $lastGlue = null): self;
  332. public function jsonSerialize(): string
  333. {
  334. return $this->string;
  335. }
  336. abstract public function length(): int;
  337. /**
  338. * @return static
  339. */
  340. abstract public function lower(): self;
  341. /**
  342. * Matches the string using a regular expression.
  343. *
  344. * Pass PREG_PATTERN_ORDER or PREG_SET_ORDER as $flags to get all occurrences matching the regular expression.
  345. *
  346. * @return array All matches in a multi-dimensional array ordered according to flags
  347. */
  348. abstract public function match(string $regexp, int $flags = 0, int $offset = 0): array;
  349. /**
  350. * @return static
  351. */
  352. abstract public function padBoth(int $length, string $padStr = ' '): self;
  353. /**
  354. * @return static
  355. */
  356. abstract public function padEnd(int $length, string $padStr = ' '): self;
  357. /**
  358. * @return static
  359. */
  360. abstract public function padStart(int $length, string $padStr = ' '): self;
  361. /**
  362. * @return static
  363. */
  364. abstract public function prepend(string ...$prefix): self;
  365. /**
  366. * @return static
  367. */
  368. public function repeat(int $multiplier): self
  369. {
  370. if (0 > $multiplier) {
  371. throw new InvalidArgumentException(sprintf('Multiplier must be positive, %d given.', $multiplier));
  372. }
  373. $str = clone $this;
  374. $str->string = str_repeat($str->string, $multiplier);
  375. return $str;
  376. }
  377. /**
  378. * @return static
  379. */
  380. abstract public function replace(string $from, string $to): self;
  381. /**
  382. * @param string|callable $to
  383. *
  384. * @return static
  385. */
  386. abstract public function replaceMatches(string $fromRegexp, $to): self;
  387. /**
  388. * @return static
  389. */
  390. abstract public function reverse(): self;
  391. /**
  392. * @return static
  393. */
  394. abstract public function slice(int $start = 0, int $length = null): self;
  395. /**
  396. * @return static
  397. */
  398. abstract public function snake(): self;
  399. /**
  400. * @return static
  401. */
  402. abstract public function splice(string $replacement, int $start = 0, int $length = null): self;
  403. /**
  404. * @return static[]
  405. */
  406. public function split(string $delimiter, int $limit = null, int $flags = null): array
  407. {
  408. if (null === $flags) {
  409. throw new \TypeError('Split behavior when $flags is null must be implemented by child classes.');
  410. }
  411. if ($this->ignoreCase) {
  412. $delimiter .= 'i';
  413. }
  414. set_error_handler(static function ($t, $m) { throw new InvalidArgumentException($m); });
  415. try {
  416. if (false === $chunks = preg_split($delimiter, $this->string, $limit, $flags)) {
  417. $lastError = preg_last_error();
  418. foreach (get_defined_constants(true)['pcre'] as $k => $v) {
  419. if ($lastError === $v && '_ERROR' === substr($k, -6)) {
  420. throw new RuntimeException('Splitting failed with '.$k.'.');
  421. }
  422. }
  423. throw new RuntimeException('Splitting failed with unknown error code.');
  424. }
  425. } finally {
  426. restore_error_handler();
  427. }
  428. $str = clone $this;
  429. if (self::PREG_SPLIT_OFFSET_CAPTURE & $flags) {
  430. foreach ($chunks as &$chunk) {
  431. $str->string = $chunk[0];
  432. $chunk[0] = clone $str;
  433. }
  434. } else {
  435. foreach ($chunks as &$chunk) {
  436. $str->string = $chunk;
  437. $chunk = clone $str;
  438. }
  439. }
  440. return $chunks;
  441. }
  442. /**
  443. * @param string|string[] $prefix
  444. */
  445. public function startsWith($prefix): bool
  446. {
  447. if (!\is_array($prefix) && !$prefix instanceof \Traversable) {
  448. throw new \TypeError(sprintf('Method "%s()" must be overridden by class "%s" to deal with non-iterable values.', __FUNCTION__, static::class));
  449. }
  450. foreach ($prefix as $prefix) {
  451. if ($this->startsWith((string) $prefix)) {
  452. return true;
  453. }
  454. }
  455. return false;
  456. }
  457. /**
  458. * @return static
  459. */
  460. abstract public function title(bool $allWords = false): self;
  461. public function toByteString(string $toEncoding = null): ByteString
  462. {
  463. $b = new ByteString();
  464. $toEncoding = \in_array($toEncoding, ['utf8', 'utf-8', 'UTF8'], true) ? 'UTF-8' : $toEncoding;
  465. if (null === $toEncoding || $toEncoding === $fromEncoding = $this instanceof AbstractUnicodeString || preg_match('//u', $b->string) ? 'UTF-8' : 'Windows-1252') {
  466. $b->string = $this->string;
  467. return $b;
  468. }
  469. set_error_handler(static function ($t, $m) { throw new InvalidArgumentException($m); });
  470. try {
  471. try {
  472. $b->string = mb_convert_encoding($this->string, $toEncoding, 'UTF-8');
  473. } catch (InvalidArgumentException $e) {
  474. if (!\function_exists('iconv')) {
  475. throw $e;
  476. }
  477. $b->string = iconv('UTF-8', $toEncoding, $this->string);
  478. }
  479. } finally {
  480. restore_error_handler();
  481. }
  482. return $b;
  483. }
  484. public function toCodePointString(): CodePointString
  485. {
  486. return new CodePointString($this->string);
  487. }
  488. public function toString(): string
  489. {
  490. return $this->string;
  491. }
  492. public function toUnicodeString(): UnicodeString
  493. {
  494. return new UnicodeString($this->string);
  495. }
  496. /**
  497. * @return static
  498. */
  499. abstract public function trim(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): self;
  500. /**
  501. * @return static
  502. */
  503. abstract public function trimEnd(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): self;
  504. /**
  505. * @return static
  506. */
  507. abstract public function trimStart(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): self;
  508. /**
  509. * @return static
  510. */
  511. public function truncate(int $length, string $ellipsis = '', bool $cut = true): self
  512. {
  513. $stringLength = $this->length();
  514. if ($stringLength <= $length) {
  515. return clone $this;
  516. }
  517. $ellipsisLength = '' !== $ellipsis ? (new static($ellipsis))->length() : 0;
  518. if ($length < $ellipsisLength) {
  519. $ellipsisLength = 0;
  520. }
  521. if (!$cut) {
  522. if (null === $length = $this->indexOf([' ', "\r", "\n", "\t"], ($length ?: 1) - 1)) {
  523. return clone $this;
  524. }
  525. $length += $ellipsisLength;
  526. }
  527. $str = $this->slice(0, $length - $ellipsisLength);
  528. return $ellipsisLength ? $str->trimEnd()->append($ellipsis) : $str;
  529. }
  530. /**
  531. * @return static
  532. */
  533. abstract public function upper(): self;
  534. /**
  535. * Returns the printable length on a terminal.
  536. */
  537. abstract public function width(bool $ignoreAnsiDecoration = true): int;
  538. /**
  539. * @return static
  540. */
  541. public function wordwrap(int $width = 75, string $break = "\n", bool $cut = false): self
  542. {
  543. $lines = '' !== $break ? $this->split($break) : [clone $this];
  544. $chars = [];
  545. $mask = '';
  546. if (1 === \count($lines) && '' === $lines[0]->string) {
  547. return $lines[0];
  548. }
  549. foreach ($lines as $i => $line) {
  550. if ($i) {
  551. $chars[] = $break;
  552. $mask .= '#';
  553. }
  554. foreach ($line->chunk() as $char) {
  555. $chars[] = $char->string;
  556. $mask .= ' ' === $char->string ? ' ' : '?';
  557. }
  558. }
  559. $string = '';
  560. $j = 0;
  561. $b = $i = -1;
  562. $mask = wordwrap($mask, $width, '#', $cut);
  563. while (false !== $b = strpos($mask, '#', $b + 1)) {
  564. for (++$i; $i < $b; ++$i) {
  565. $string .= $chars[$j];
  566. unset($chars[$j++]);
  567. }
  568. if ($break === $chars[$j] || ' ' === $chars[$j]) {
  569. unset($chars[$j++]);
  570. }
  571. $string .= $break;
  572. }
  573. $str = clone $this;
  574. $str->string = $string.implode('', $chars);
  575. return $str;
  576. }
  577. public function __sleep(): array
  578. {
  579. return ['string'];
  580. }
  581. public function __clone()
  582. {
  583. $this->ignoreCase = false;
  584. }
  585. public function __toString(): string
  586. {
  587. return $this->string;
  588. }
  589. }