Str.php 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119
  1. <?php
  2. namespace Illuminate\Support;
  3. use Closure;
  4. use Illuminate\Support\Traits\Macroable;
  5. use League\CommonMark\Environment\Environment;
  6. use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
  7. use League\CommonMark\Extension\InlinesOnly\InlinesOnlyExtension;
  8. use League\CommonMark\GithubFlavoredMarkdownConverter;
  9. use League\CommonMark\MarkdownConverter;
  10. use Ramsey\Uuid\Codec\TimestampFirstCombCodec;
  11. use Ramsey\Uuid\Exception\InvalidUuidStringException;
  12. use Ramsey\Uuid\Generator\CombGenerator;
  13. use Ramsey\Uuid\Rfc4122\FieldsInterface;
  14. use Ramsey\Uuid\Uuid;
  15. use Ramsey\Uuid\UuidFactory;
  16. use Symfony\Component\Uid\Ulid;
  17. use Throwable;
  18. use Traversable;
  19. use voku\helper\ASCII;
  20. class Str
  21. {
  22. use Macroable;
  23. /**
  24. * The list of characters that are considered "invisible" in strings.
  25. *
  26. * @var string
  27. */
  28. const INVISIBLE_CHARACTERS = '\x{0009}\x{0020}\x{00A0}\x{00AD}\x{034F}\x{061C}\x{115F}\x{1160}\x{17B4}\x{17B5}\x{180E}\x{2000}\x{2001}\x{2002}\x{2003}\x{2004}\x{2005}\x{2006}\x{2007}\x{2008}\x{2009}\x{200A}\x{200B}\x{200C}\x{200D}\x{200E}\x{200F}\x{202F}\x{205F}\x{2060}\x{2061}\x{2062}\x{2063}\x{2064}\x{2065}\x{206A}\x{206B}\x{206C}\x{206D}\x{206E}\x{206F}\x{3000}\x{2800}\x{3164}\x{FEFF}\x{FFA0}\x{1D159}\x{1D173}\x{1D174}\x{1D175}\x{1D176}\x{1D177}\x{1D178}\x{1D179}\x{1D17A}\x{E0020}';
  29. /**
  30. * The cache of snake-cased words.
  31. *
  32. * @var array<string, string>
  33. */
  34. protected static $snakeCache = [];
  35. /**
  36. * The cache of camel-cased words.
  37. *
  38. * @var array<string, string>
  39. */
  40. protected static $camelCache = [];
  41. /**
  42. * The cache of studly-cased words.
  43. *
  44. * @var array<string, string>
  45. */
  46. protected static $studlyCache = [];
  47. /**
  48. * The callback that should be used to generate UUIDs.
  49. *
  50. * @var (callable(): \Ramsey\Uuid\UuidInterface)|null
  51. */
  52. protected static $uuidFactory;
  53. /**
  54. * The callback that should be used to generate ULIDs.
  55. *
  56. * @var (callable(): \Symfony\Component\Uid\Ulid)|null
  57. */
  58. protected static $ulidFactory;
  59. /**
  60. * The callback that should be used to generate random strings.
  61. *
  62. * @var (callable(int): string)|null
  63. */
  64. protected static $randomStringFactory;
  65. /**
  66. * Get a new stringable object from the given string.
  67. *
  68. * @param string $string
  69. * @return \Illuminate\Support\Stringable
  70. */
  71. public static function of($string)
  72. {
  73. return new Stringable($string);
  74. }
  75. /**
  76. * Return the remainder of a string after the first occurrence of a given value.
  77. *
  78. * @param string $subject
  79. * @param string $search
  80. * @return string
  81. */
  82. public static function after($subject, $search)
  83. {
  84. return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0];
  85. }
  86. /**
  87. * Return the remainder of a string after the last occurrence of a given value.
  88. *
  89. * @param string $subject
  90. * @param string $search
  91. * @return string
  92. */
  93. public static function afterLast($subject, $search)
  94. {
  95. if ($search === '') {
  96. return $subject;
  97. }
  98. $position = strrpos($subject, (string) $search);
  99. if ($position === false) {
  100. return $subject;
  101. }
  102. return substr($subject, $position + strlen($search));
  103. }
  104. /**
  105. * Transliterate a UTF-8 value to ASCII.
  106. *
  107. * @param string $value
  108. * @param string $language
  109. * @return string
  110. */
  111. public static function ascii($value, $language = 'en')
  112. {
  113. return ASCII::to_ascii((string) $value, $language, replace_single_chars_only: false);
  114. }
  115. /**
  116. * Transliterate a string to its closest ASCII representation.
  117. *
  118. * @param string $string
  119. * @param string|null $unknown
  120. * @param bool|null $strict
  121. * @return string
  122. */
  123. public static function transliterate($string, $unknown = '?', $strict = false)
  124. {
  125. return ASCII::to_transliterate($string, $unknown, $strict);
  126. }
  127. /**
  128. * Get the portion of a string before the first occurrence of a given value.
  129. *
  130. * @param string $subject
  131. * @param string $search
  132. * @return string
  133. */
  134. public static function before($subject, $search)
  135. {
  136. if ($search === '') {
  137. return $subject;
  138. }
  139. $result = strstr($subject, (string) $search, true);
  140. return $result === false ? $subject : $result;
  141. }
  142. /**
  143. * Get the portion of a string before the last occurrence of a given value.
  144. *
  145. * @param string $subject
  146. * @param string $search
  147. * @return string
  148. */
  149. public static function beforeLast($subject, $search)
  150. {
  151. if ($search === '') {
  152. return $subject;
  153. }
  154. $pos = mb_strrpos($subject, $search);
  155. if ($pos === false) {
  156. return $subject;
  157. }
  158. return static::substr($subject, 0, $pos);
  159. }
  160. /**
  161. * Get the portion of a string between two given values.
  162. *
  163. * @param string $subject
  164. * @param string $from
  165. * @param string $to
  166. * @return string
  167. */
  168. public static function between($subject, $from, $to)
  169. {
  170. if ($from === '' || $to === '') {
  171. return $subject;
  172. }
  173. return static::beforeLast(static::after($subject, $from), $to);
  174. }
  175. /**
  176. * Get the smallest possible portion of a string between two given values.
  177. *
  178. * @param string $subject
  179. * @param string $from
  180. * @param string $to
  181. * @return string
  182. */
  183. public static function betweenFirst($subject, $from, $to)
  184. {
  185. if ($from === '' || $to === '') {
  186. return $subject;
  187. }
  188. return static::before(static::after($subject, $from), $to);
  189. }
  190. /**
  191. * Convert a value to camel case.
  192. *
  193. * @param string $value
  194. * @return string
  195. */
  196. public static function camel($value)
  197. {
  198. if (isset(static::$camelCache[$value])) {
  199. return static::$camelCache[$value];
  200. }
  201. return static::$camelCache[$value] = lcfirst(static::studly($value));
  202. }
  203. /**
  204. * Get the character at the specified index.
  205. *
  206. * @param string $subject
  207. * @param int $index
  208. * @return string|false
  209. */
  210. public static function charAt($subject, $index)
  211. {
  212. $length = mb_strlen($subject);
  213. if ($index < 0 ? $index < -$length : $index > $length - 1) {
  214. return false;
  215. }
  216. return mb_substr($subject, $index, 1);
  217. }
  218. /**
  219. * Remove the given string(s) if it exists at the start of the haystack.
  220. *
  221. * @param string $subject
  222. * @param string|array $needle
  223. * @return string
  224. */
  225. public static function chopStart($subject, $needle)
  226. {
  227. foreach ((array) $needle as $n) {
  228. if (str_starts_with($subject, $n)) {
  229. return substr($subject, strlen($n));
  230. }
  231. }
  232. return $subject;
  233. }
  234. /**
  235. * Remove the given string(s) if it exists at the end of the haystack.
  236. *
  237. * @param string $subject
  238. * @param string|array $needle
  239. * @return string
  240. */
  241. public static function chopEnd($subject, $needle)
  242. {
  243. foreach ((array) $needle as $n) {
  244. if (str_ends_with($subject, $n)) {
  245. return substr($subject, 0, -strlen($n));
  246. }
  247. }
  248. return $subject;
  249. }
  250. /**
  251. * Determine if a given string contains a given substring.
  252. *
  253. * @param string $haystack
  254. * @param string|iterable<string> $needles
  255. * @param bool $ignoreCase
  256. * @return bool
  257. */
  258. public static function contains($haystack, $needles, $ignoreCase = false)
  259. {
  260. if (is_null($haystack)) {
  261. return false;
  262. }
  263. if ($ignoreCase) {
  264. $haystack = mb_strtolower($haystack);
  265. }
  266. if (! is_iterable($needles)) {
  267. $needles = (array) $needles;
  268. }
  269. foreach ($needles as $needle) {
  270. if ($ignoreCase) {
  271. $needle = mb_strtolower($needle);
  272. }
  273. if ($needle !== '' && str_contains($haystack, $needle)) {
  274. return true;
  275. }
  276. }
  277. return false;
  278. }
  279. /**
  280. * Determine if a given string contains all array values.
  281. *
  282. * @param string $haystack
  283. * @param iterable<string> $needles
  284. * @param bool $ignoreCase
  285. * @return bool
  286. */
  287. public static function containsAll($haystack, $needles, $ignoreCase = false)
  288. {
  289. foreach ($needles as $needle) {
  290. if (! static::contains($haystack, $needle, $ignoreCase)) {
  291. return false;
  292. }
  293. }
  294. return true;
  295. }
  296. /**
  297. * Determine if a given string doesn't contain a given substring.
  298. *
  299. * @param string $haystack
  300. * @param string|iterable<string> $needles
  301. * @param bool $ignoreCase
  302. * @return bool
  303. */
  304. public static function doesntContain($haystack, $needles, $ignoreCase = false)
  305. {
  306. return ! static::contains($haystack, $needles, $ignoreCase);
  307. }
  308. /**
  309. * Convert the case of a string.
  310. *
  311. * @param string $string
  312. * @param int $mode
  313. * @param string|null $encoding
  314. * @return string
  315. */
  316. public static function convertCase(string $string, int $mode = MB_CASE_FOLD, ?string $encoding = 'UTF-8')
  317. {
  318. return mb_convert_case($string, $mode, $encoding);
  319. }
  320. /**
  321. * Replace consecutive instances of a given character with a single character in the given string.
  322. *
  323. * @param string $string
  324. * @param array<string>|string $characters
  325. * @return string
  326. */
  327. public static function deduplicate(string $string, array|string $characters = ' ')
  328. {
  329. if (is_string($characters)) {
  330. return preg_replace('/'.preg_quote($characters, '/').'+/u', $characters, $string);
  331. }
  332. return array_reduce(
  333. $characters,
  334. fn ($carry, $character) => preg_replace('/'.preg_quote($character, '/').'+/u', $character, $carry),
  335. $string
  336. );
  337. }
  338. /**
  339. * Determine if a given string ends with a given substring.
  340. *
  341. * @param string $haystack
  342. * @param string|iterable<string> $needles
  343. * @return bool
  344. */
  345. public static function endsWith($haystack, $needles)
  346. {
  347. if (is_null($haystack)) {
  348. return false;
  349. }
  350. if (! is_iterable($needles)) {
  351. $needles = (array) $needles;
  352. }
  353. foreach ($needles as $needle) {
  354. if ((string) $needle !== '' && str_ends_with($haystack, $needle)) {
  355. return true;
  356. }
  357. }
  358. return false;
  359. }
  360. /**
  361. * Determine if a given string doesn't end with a given substring.
  362. *
  363. * @param string $haystack
  364. * @param string|iterable<string> $needles
  365. * @return bool
  366. */
  367. public static function doesntEndWith($haystack, $needles)
  368. {
  369. return ! static::endsWith($haystack, $needles);
  370. }
  371. /**
  372. * Extracts an excerpt from text that matches the first instance of a phrase.
  373. *
  374. * @param string $text
  375. * @param string $phrase
  376. * @param array $options
  377. * @return string|null
  378. */
  379. public static function excerpt($text, $phrase = '', $options = [])
  380. {
  381. $radius = $options['radius'] ?? 100;
  382. $omission = $options['omission'] ?? '...';
  383. preg_match('/^(.*?)('.preg_quote((string) $phrase, '/').')(.*)$/iu', (string) $text, $matches);
  384. if (empty($matches)) {
  385. return null;
  386. }
  387. $start = ltrim($matches[1]);
  388. $start = Str::of(mb_substr($start, max(mb_strlen($start, 'UTF-8') - $radius, 0), $radius, 'UTF-8'))->ltrim()->unless(
  389. fn ($startWithRadius) => $startWithRadius->exactly($start),
  390. fn ($startWithRadius) => $startWithRadius->prepend($omission),
  391. );
  392. $end = rtrim($matches[3]);
  393. $end = Str::of(mb_substr($end, 0, $radius, 'UTF-8'))->rtrim()->unless(
  394. fn ($endWithRadius) => $endWithRadius->exactly($end),
  395. fn ($endWithRadius) => $endWithRadius->append($omission),
  396. );
  397. return $start->append($matches[2], $end)->toString();
  398. }
  399. /**
  400. * Cap a string with a single instance of a given value.
  401. *
  402. * @param string $value
  403. * @param string $cap
  404. * @return string
  405. */
  406. public static function finish($value, $cap)
  407. {
  408. $quoted = preg_quote($cap, '/');
  409. return preg_replace('/(?:'.$quoted.')+$/u', '', $value).$cap;
  410. }
  411. /**
  412. * Wrap the string with the given strings.
  413. *
  414. * @param string $value
  415. * @param string $before
  416. * @param string|null $after
  417. * @return string
  418. */
  419. public static function wrap($value, $before, $after = null)
  420. {
  421. return $before.$value.($after ?? $before);
  422. }
  423. /**
  424. * Unwrap the string with the given strings.
  425. *
  426. * @param string $value
  427. * @param string $before
  428. * @param string|null $after
  429. * @return string
  430. */
  431. public static function unwrap($value, $before, $after = null)
  432. {
  433. if (static::startsWith($value, $before)) {
  434. $value = static::substr($value, static::length($before));
  435. }
  436. if (static::endsWith($value, $after ??= $before)) {
  437. $value = static::substr($value, 0, -static::length($after));
  438. }
  439. return $value;
  440. }
  441. /**
  442. * Determine if a given string matches a given pattern.
  443. *
  444. * @param string|iterable<string> $pattern
  445. * @param string $value
  446. * @param bool $ignoreCase
  447. * @return bool
  448. */
  449. public static function is($pattern, $value, $ignoreCase = false)
  450. {
  451. $value = (string) $value;
  452. if (! is_iterable($pattern)) {
  453. $pattern = [$pattern];
  454. }
  455. foreach ($pattern as $pattern) {
  456. $pattern = (string) $pattern;
  457. // If the given value is an exact match we can of course return true right
  458. // from the beginning. Otherwise, we will translate asterisks and do an
  459. // actual pattern match against the two strings to see if they match.
  460. if ($pattern === '*' || $pattern === $value) {
  461. return true;
  462. }
  463. if ($ignoreCase && mb_strtolower($pattern) === mb_strtolower($value)) {
  464. return true;
  465. }
  466. $pattern = preg_quote($pattern, '#');
  467. // Asterisks are translated into zero-or-more regular expression wildcards
  468. // to make it convenient to check if the strings starts with the given
  469. // pattern such as "library/*", making any string check convenient.
  470. $pattern = str_replace('\*', '.*', $pattern);
  471. if (preg_match('#^'.$pattern.'\z#'.($ignoreCase ? 'isu' : 'su'), $value) === 1) {
  472. return true;
  473. }
  474. }
  475. return false;
  476. }
  477. /**
  478. * Determine if a given string is 7 bit ASCII.
  479. *
  480. * @param string $value
  481. * @return bool
  482. */
  483. public static function isAscii($value)
  484. {
  485. return ASCII::is_ascii((string) $value);
  486. }
  487. /**
  488. * Determine if a given value is valid JSON.
  489. *
  490. * @param mixed $value
  491. * @return bool
  492. *
  493. * @phpstan-assert-if-true =non-empty-string $value
  494. */
  495. public static function isJson($value)
  496. {
  497. if (! is_string($value)) {
  498. return false;
  499. }
  500. return json_validate($value, 512);
  501. }
  502. /**
  503. * Determine if a given value is a valid URL.
  504. *
  505. * @param mixed $value
  506. * @param array $protocols
  507. * @return bool
  508. *
  509. * @phpstan-assert-if-true =non-empty-string $value
  510. */
  511. public static function isUrl($value, array $protocols = [])
  512. {
  513. if (! is_string($value)) {
  514. return false;
  515. }
  516. $protocolList = empty($protocols)
  517. ? 'aaa|aaas|about|acap|acct|acd|acr|adiumxtra|adt|afp|afs|aim|amss|android|appdata|apt|ark|attachment|aw|barion|beshare|bitcoin|bitcoincash|blob|bolo|browserext|calculator|callto|cap|cast|casts|chrome|chrome-extension|cid|coap|coap\+tcp|coap\+ws|coaps|coaps\+tcp|coaps\+ws|com-eventbrite-attendee|content|conti|crid|cvs|dab|data|dav|diaspora|dict|did|dis|dlna-playcontainer|dlna-playsingle|dns|dntp|dpp|drm|drop|dtn|dvb|ed2k|elsi|example|facetime|fax|feed|feedready|file|filesystem|finger|first-run-pen-experience|fish|fm|ftp|fuchsia-pkg|geo|gg|git|gizmoproject|go|gopher|graph|gtalk|h323|ham|hcap|hcp|http|https|hxxp|hxxps|hydrazone|iax|icap|icon|im|imap|info|iotdisco|ipn|ipp|ipps|irc|irc6|ircs|iris|iris\.beep|iris\.lwz|iris\.xpc|iris\.xpcs|isostore|itms|jabber|jar|jms|keyparc|lastfm|ldap|ldaps|leaptofrogans|lorawan|lvlt|magnet|mailserver|mailto|maps|market|message|mid|mms|modem|mongodb|moz|ms-access|ms-browser-extension|ms-calculator|ms-drive-to|ms-enrollment|ms-excel|ms-eyecontrolspeech|ms-gamebarservices|ms-gamingoverlay|ms-getoffice|ms-help|ms-infopath|ms-inputapp|ms-lockscreencomponent-config|ms-media-stream-id|ms-mixedrealitycapture|ms-mobileplans|ms-officeapp|ms-people|ms-project|ms-powerpoint|ms-publisher|ms-restoretabcompanion|ms-screenclip|ms-screensketch|ms-search|ms-search-repair|ms-secondary-screen-controller|ms-secondary-screen-setup|ms-settings|ms-settings-airplanemode|ms-settings-bluetooth|ms-settings-camera|ms-settings-cellular|ms-settings-cloudstorage|ms-settings-connectabledevices|ms-settings-displays-topology|ms-settings-emailandaccounts|ms-settings-language|ms-settings-location|ms-settings-lock|ms-settings-nfctransactions|ms-settings-notifications|ms-settings-power|ms-settings-privacy|ms-settings-proximity|ms-settings-screenrotation|ms-settings-wifi|ms-settings-workplace|ms-spd|ms-sttoverlay|ms-transit-to|ms-useractivityset|ms-virtualtouchpad|ms-visio|ms-walk-to|ms-whiteboard|ms-whiteboard-cmd|ms-word|msnim|msrp|msrps|mss|mtqp|mumble|mupdate|mvn|news|nfs|ni|nih|nntp|notes|ocf|oid|onenote|onenote-cmd|opaquelocktoken|openpgp4fpr|pack|palm|paparazzi|payto|pkcs11|platform|pop|pres|prospero|proxy|pwid|psyc|pttp|qb|query|redis|rediss|reload|res|resource|rmi|rsync|rtmfp|rtmp|rtsp|rtsps|rtspu|s3|secondlife|service|session|sftp|sgn|shttp|sieve|simpleledger|sip|sips|skype|smb|sms|smtp|snews|snmp|soap\.beep|soap\.beeps|soldat|spiffe|spotify|ssh|steam|stun|stuns|submit|svn|tag|teamspeak|tel|teliaeid|telnet|tftp|tg|things|thismessage|tip|tn3270|tool|ts3server|turn|turns|tv|udp|unreal|urn|ut2004|v-event|vemmi|ventrilo|videotex|vnc|view-source|wais|webcal|wpid|ws|wss|wtai|wyciwyg|xcon|xcon-userid|xfire|xmlrpc\.beep|xmlrpc\.beeps|xmpp|xri|ymsgr|z39\.50|z39\.50r|z39\.50s'
  518. : implode('|', $protocols);
  519. /*
  520. * This pattern is derived from Symfony\Component\Validator\Constraints\UrlValidator (5.0.7).
  521. *
  522. * (c) Fabien Potencier <fabien@symfony.com> http://symfony.com
  523. */
  524. $pattern = '~^
  525. (LARAVEL_PROTOCOLS):// # protocol
  526. (((?:[\_\.\pL\pN-]|%[0-9A-Fa-f]{2})+:)?((?:[\_\.\pL\pN-]|%[0-9A-Fa-f]{2})+)@)? # basic auth
  527. (
  528. ([\pL\pN\pS\-\_\.])+(\.?([\pL\pN]|xn\-\-[\pL\pN-]+)+\.?) # a domain name
  529. | # or
  530. \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # an IP address
  531. | # or
  532. \[
  533. (?:(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-f]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){4})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,1}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){3})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,2}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){2})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,3}(?:(?:[0-9a-f]{1,4})))?::(?:(?:[0-9a-f]{1,4})):)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,4}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,5}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,6}(?:(?:[0-9a-f]{1,4})))?::))))
  534. \] # an IPv6 address
  535. )
  536. (:[0-9]+)? # a port (optional)
  537. (?:/ (?:[\pL\pN\-._\~!$&\'()*+,;=:@]|%[0-9A-Fa-f]{2})* )* # a path
  538. (?:\? (?:[\pL\pN\-._\~!$&\'\[\]()*+,;=:@/?]|%[0-9A-Fa-f]{2})* )? # a query (optional)
  539. (?:\# (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%[0-9A-Fa-f]{2})* )? # a fragment (optional)
  540. $~ixu';
  541. return preg_match(str_replace('LARAVEL_PROTOCOLS', $protocolList, $pattern), $value) > 0;
  542. }
  543. /**
  544. * Determine if a given value is a valid UUID.
  545. *
  546. * @param mixed $value
  547. * @param int<0, 8>|'nil'|'max'|null $version
  548. * @return bool
  549. *
  550. * @phpstan-assert-if-true =non-empty-string $value
  551. */
  552. public static function isUuid($value, $version = null)
  553. {
  554. if (! is_string($value)) {
  555. return false;
  556. }
  557. if ($version === null) {
  558. return preg_match('/^[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}$/D', $value) > 0;
  559. }
  560. $factory = new UuidFactory;
  561. try {
  562. $factoryUuid = $factory->fromString($value);
  563. } catch (InvalidUuidStringException) {
  564. return false;
  565. }
  566. $fields = $factoryUuid->getFields();
  567. if (! ($fields instanceof FieldsInterface)) {
  568. return false;
  569. }
  570. if ($version === 0 || $version === 'nil') {
  571. return $fields->isNil();
  572. }
  573. if ($version === 'max') {
  574. return $fields->isMax();
  575. }
  576. return $fields->getVersion() === $version;
  577. }
  578. /**
  579. * Determine if a given value is a valid ULID.
  580. *
  581. * @param mixed $value
  582. * @return bool
  583. *
  584. * @phpstan-assert-if-true =non-empty-string $value
  585. */
  586. public static function isUlid($value)
  587. {
  588. if (! is_string($value)) {
  589. return false;
  590. }
  591. return Ulid::isValid($value);
  592. }
  593. /**
  594. * Convert a string to kebab case.
  595. *
  596. * @param string $value
  597. * @return string
  598. */
  599. public static function kebab($value)
  600. {
  601. return static::snake($value, '-');
  602. }
  603. /**
  604. * Return the length of the given string.
  605. *
  606. * @param string $value
  607. * @param string|null $encoding
  608. * @return int
  609. */
  610. public static function length($value, $encoding = null)
  611. {
  612. return mb_strlen($value, $encoding);
  613. }
  614. /**
  615. * Limit the number of characters in a string.
  616. *
  617. * @param string $value
  618. * @param int $limit
  619. * @param string $end
  620. * @param bool $preserveWords
  621. * @return string
  622. */
  623. public static function limit($value, $limit = 100, $end = '...', $preserveWords = false)
  624. {
  625. if (mb_strwidth($value, 'UTF-8') <= $limit) {
  626. return $value;
  627. }
  628. if (! $preserveWords) {
  629. return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;
  630. }
  631. $value = trim(preg_replace('/[\n\r]+/', ' ', strip_tags($value)));
  632. $trimmed = rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8'));
  633. if (mb_substr($value, $limit, 1, 'UTF-8') === ' ') {
  634. return $trimmed.$end;
  635. }
  636. return preg_replace("/(.*)\s.*/", '$1', $trimmed).$end;
  637. }
  638. /**
  639. * Convert the given string to lower-case.
  640. *
  641. * @param string $value
  642. * @return string
  643. */
  644. public static function lower($value)
  645. {
  646. return mb_strtolower($value, 'UTF-8');
  647. }
  648. /**
  649. * Limit the number of words in a string.
  650. *
  651. * @param string $value
  652. * @param int $words
  653. * @param string $end
  654. * @return string
  655. */
  656. public static function words($value, $words = 100, $end = '...')
  657. {
  658. preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $value, $matches);
  659. if (! isset($matches[0]) || static::length($value) === static::length($matches[0])) {
  660. return $value;
  661. }
  662. return rtrim($matches[0]).$end;
  663. }
  664. /**
  665. * Converts GitHub flavored Markdown into HTML.
  666. *
  667. * @param string $string
  668. * @param array $options
  669. * @param array $extensions
  670. * @return string
  671. */
  672. public static function markdown($string, array $options = [], array $extensions = [])
  673. {
  674. $converter = new GithubFlavoredMarkdownConverter($options);
  675. $environment = $converter->getEnvironment();
  676. foreach ($extensions as $extension) {
  677. $environment->addExtension($extension);
  678. }
  679. return (string) $converter->convert($string);
  680. }
  681. /**
  682. * Converts inline Markdown into HTML.
  683. *
  684. * @param string $string
  685. * @param array $options
  686. * @param array $extensions
  687. * @return string
  688. */
  689. public static function inlineMarkdown($string, array $options = [], array $extensions = [])
  690. {
  691. $environment = new Environment($options);
  692. $environment->addExtension(new GithubFlavoredMarkdownExtension());
  693. $environment->addExtension(new InlinesOnlyExtension());
  694. foreach ($extensions as $extension) {
  695. $environment->addExtension($extension);
  696. }
  697. $converter = new MarkdownConverter($environment);
  698. return (string) $converter->convert($string);
  699. }
  700. /**
  701. * Masks a portion of a string with a repeated character.
  702. *
  703. * @param string $string
  704. * @param string $character
  705. * @param int $index
  706. * @param int|null $length
  707. * @param string $encoding
  708. * @return string
  709. */
  710. public static function mask($string, $character, $index, $length = null, $encoding = 'UTF-8')
  711. {
  712. if ($character === '') {
  713. return $string;
  714. }
  715. $segment = mb_substr($string, $index, $length, $encoding);
  716. if ($segment === '') {
  717. return $string;
  718. }
  719. $strlen = mb_strlen($string, $encoding);
  720. $startIndex = $index;
  721. if ($index < 0) {
  722. $startIndex = $index < -$strlen ? 0 : $strlen + $index;
  723. }
  724. $start = mb_substr($string, 0, $startIndex, $encoding);
  725. $segmentLen = mb_strlen($segment, $encoding);
  726. $end = mb_substr($string, $startIndex + $segmentLen);
  727. return $start.str_repeat(mb_substr($character, 0, 1, $encoding), $segmentLen).$end;
  728. }
  729. /**
  730. * Get the string matching the given pattern.
  731. *
  732. * @param string $pattern
  733. * @param string $subject
  734. * @return string
  735. */
  736. public static function match($pattern, $subject)
  737. {
  738. preg_match($pattern, $subject, $matches);
  739. if (! $matches) {
  740. return '';
  741. }
  742. return $matches[1] ?? $matches[0];
  743. }
  744. /**
  745. * Determine if a given string matches a given pattern.
  746. *
  747. * @param string|iterable<string> $pattern
  748. * @param string $value
  749. * @return bool
  750. */
  751. public static function isMatch($pattern, $value)
  752. {
  753. $value = (string) $value;
  754. if (! is_iterable($pattern)) {
  755. $pattern = [$pattern];
  756. }
  757. foreach ($pattern as $pattern) {
  758. $pattern = (string) $pattern;
  759. if (preg_match($pattern, $value) === 1) {
  760. return true;
  761. }
  762. }
  763. return false;
  764. }
  765. /**
  766. * Get the string matching the given pattern.
  767. *
  768. * @param string $pattern
  769. * @param string $subject
  770. * @return \Illuminate\Support\Collection
  771. */
  772. public static function matchAll($pattern, $subject)
  773. {
  774. preg_match_all($pattern, $subject, $matches);
  775. if (empty($matches[0])) {
  776. return new Collection;
  777. }
  778. return new Collection($matches[1] ?? $matches[0]);
  779. }
  780. /**
  781. * Remove all non-numeric characters from a string.
  782. *
  783. * @param string $value
  784. * @return string
  785. */
  786. public static function numbers($value)
  787. {
  788. return preg_replace('/[^0-9]/', '', $value);
  789. }
  790. /**
  791. * Pad both sides of a string with another.
  792. *
  793. * @param string $value
  794. * @param int $length
  795. * @param string $pad
  796. * @return string
  797. */
  798. public static function padBoth($value, $length, $pad = ' ')
  799. {
  800. return mb_str_pad($value, $length, $pad, STR_PAD_BOTH);
  801. }
  802. /**
  803. * Pad the left side of a string with another.
  804. *
  805. * @param string $value
  806. * @param int $length
  807. * @param string $pad
  808. * @return string
  809. */
  810. public static function padLeft($value, $length, $pad = ' ')
  811. {
  812. return mb_str_pad($value, $length, $pad, STR_PAD_LEFT);
  813. }
  814. /**
  815. * Pad the right side of a string with another.
  816. *
  817. * @param string $value
  818. * @param int $length
  819. * @param string $pad
  820. * @return string
  821. */
  822. public static function padRight($value, $length, $pad = ' ')
  823. {
  824. return mb_str_pad($value, $length, $pad, STR_PAD_RIGHT);
  825. }
  826. /**
  827. * Parse a Class[@]method style callback into class and method.
  828. *
  829. * @param string $callback
  830. * @param string|null $default
  831. * @return array<int, string|null>
  832. */
  833. public static function parseCallback($callback, $default = null)
  834. {
  835. if (static::contains($callback, "@anonymous\0")) {
  836. if (static::substrCount($callback, '@') > 1) {
  837. return [
  838. static::beforeLast($callback, '@'),
  839. static::afterLast($callback, '@'),
  840. ];
  841. }
  842. return [$callback, $default];
  843. }
  844. return static::contains($callback, '@') ? explode('@', $callback, 2) : [$callback, $default];
  845. }
  846. /**
  847. * Get the plural form of an English word.
  848. *
  849. * @param string $value
  850. * @param int|array|\Countable $count
  851. * @param bool $prependCount
  852. * @return string
  853. */
  854. public static function plural($value, $count = 2, $prependCount = false)
  855. {
  856. if (is_countable($count)) {
  857. $count = count($count);
  858. }
  859. return ($prependCount ? Number::format($count).' ' : '').Pluralizer::plural($value, $count);
  860. }
  861. /**
  862. * Pluralize the last word of an English, studly caps case string.
  863. *
  864. * @param string $value
  865. * @param int|array|\Countable $count
  866. * @return string
  867. */
  868. public static function pluralStudly($value, $count = 2)
  869. {
  870. $parts = preg_split('/(.)(?=[A-Z])/u', $value, -1, PREG_SPLIT_DELIM_CAPTURE);
  871. $lastWord = array_pop($parts);
  872. return implode('', $parts).self::plural($lastWord, $count);
  873. }
  874. /**
  875. * Pluralize the last word of an English, Pascal caps case string.
  876. *
  877. * @param string $value
  878. * @param int|array|\Countable $count
  879. * @return string
  880. */
  881. public static function pluralPascal($value, $count = 2)
  882. {
  883. return static::pluralStudly($value, $count);
  884. }
  885. /**
  886. * Generate a random, secure password.
  887. *
  888. * @param int $length
  889. * @param bool $letters
  890. * @param bool $numbers
  891. * @param bool $symbols
  892. * @param bool $spaces
  893. * @return string
  894. */
  895. public static function password($length = 32, $letters = true, $numbers = true, $symbols = true, $spaces = false)
  896. {
  897. $password = new Collection();
  898. $options = (new Collection([
  899. 'letters' => $letters === true ? [
  900. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
  901. 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  902. 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
  903. 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
  904. 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  905. ] : null,
  906. 'numbers' => $numbers === true ? [
  907. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  908. ] : null,
  909. 'symbols' => $symbols === true ? [
  910. '~', '!', '#', '$', '%', '^', '&', '*', '(', ')', '-',
  911. '_', '.', ',', '<', '>', '?', '/', '\\', '{', '}', '[',
  912. ']', '|', ':', ';',
  913. ] : null,
  914. 'spaces' => $spaces === true ? [' '] : null,
  915. ]))
  916. ->filter()
  917. ->each(fn ($c) => $password->push($c[random_int(0, count($c) - 1)]))
  918. ->flatten();
  919. $length = $length - $password->count();
  920. return $password->merge($options->pipe(
  921. fn ($c) => Collection::times($length, fn () => $c[random_int(0, $c->count() - 1)])
  922. ))->shuffle()->implode('');
  923. }
  924. /**
  925. * Find the multi-byte safe position of the first occurrence of a given substring in a string.
  926. *
  927. * @param string $haystack
  928. * @param string $needle
  929. * @param int $offset
  930. * @param string|null $encoding
  931. * @return int|false
  932. */
  933. public static function position($haystack, $needle, $offset = 0, $encoding = null)
  934. {
  935. return mb_strpos($haystack, (string) $needle, $offset, $encoding);
  936. }
  937. /**
  938. * Generate a more truly "random" alpha-numeric string.
  939. *
  940. * @param int $length
  941. * @return string
  942. */
  943. public static function random($length = 16)
  944. {
  945. return (static::$randomStringFactory ?? function ($length) {
  946. $string = '';
  947. while (($len = strlen($string)) < $length) {
  948. $size = $length - $len;
  949. $bytesSize = (int) ceil($size / 3) * 3;
  950. $bytes = random_bytes($bytesSize);
  951. $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
  952. }
  953. return $string;
  954. })($length);
  955. }
  956. /**
  957. * Set the callable that will be used to generate random strings.
  958. *
  959. * @param (callable(int): string)|null $factory
  960. * @return void
  961. */
  962. public static function createRandomStringsUsing(?callable $factory = null)
  963. {
  964. static::$randomStringFactory = $factory;
  965. }
  966. /**
  967. * Set the sequence that will be used to generate random strings.
  968. *
  969. * @param array $sequence
  970. * @param callable|null $whenMissing
  971. * @return void
  972. */
  973. public static function createRandomStringsUsingSequence(array $sequence, $whenMissing = null)
  974. {
  975. $next = 0;
  976. $whenMissing ??= function ($length) use (&$next) {
  977. $factoryCache = static::$randomStringFactory;
  978. static::$randomStringFactory = null;
  979. $randomString = static::random($length);
  980. static::$randomStringFactory = $factoryCache;
  981. $next++;
  982. return $randomString;
  983. };
  984. static::createRandomStringsUsing(function ($length) use (&$next, $sequence, $whenMissing) {
  985. if (array_key_exists($next, $sequence)) {
  986. return $sequence[$next++];
  987. }
  988. return $whenMissing($length);
  989. });
  990. }
  991. /**
  992. * Indicate that random strings should be created normally and not using a custom factory.
  993. *
  994. * @return void
  995. */
  996. public static function createRandomStringsNormally()
  997. {
  998. static::$randomStringFactory = null;
  999. }
  1000. /**
  1001. * Repeat the given string.
  1002. *
  1003. * @param string $string
  1004. * @param int $times
  1005. * @return string
  1006. */
  1007. public static function repeat(string $string, int $times)
  1008. {
  1009. return str_repeat($string, $times);
  1010. }
  1011. /**
  1012. * Replace a given value in the string sequentially with an array.
  1013. *
  1014. * @param string $search
  1015. * @param iterable<string> $replace
  1016. * @param string $subject
  1017. * @return string
  1018. */
  1019. public static function replaceArray($search, $replace, $subject)
  1020. {
  1021. if ($replace instanceof Traversable) {
  1022. $replace = Arr::from($replace);
  1023. }
  1024. $segments = explode($search, $subject);
  1025. $result = array_shift($segments);
  1026. foreach ($segments as $segment) {
  1027. $result .= self::toStringOr(array_shift($replace) ?? $search, $search).$segment;
  1028. }
  1029. return $result;
  1030. }
  1031. /**
  1032. * Convert the given value to a string or return the given fallback on failure.
  1033. *
  1034. * @param mixed $value
  1035. * @param string $fallback
  1036. * @return string
  1037. */
  1038. private static function toStringOr($value, $fallback)
  1039. {
  1040. try {
  1041. return (string) $value;
  1042. } catch (Throwable $e) {
  1043. return $fallback;
  1044. }
  1045. }
  1046. /**
  1047. * Replace the given value in the given string.
  1048. *
  1049. * @param string|iterable<string> $search
  1050. * @param string|iterable<string> $replace
  1051. * @param string|iterable<string> $subject
  1052. * @param bool $caseSensitive
  1053. * @return string|string[]
  1054. */
  1055. public static function replace($search, $replace, $subject, $caseSensitive = true)
  1056. {
  1057. if ($search instanceof Traversable) {
  1058. $search = Arr::from($search);
  1059. }
  1060. if ($replace instanceof Traversable) {
  1061. $replace = Arr::from($replace);
  1062. }
  1063. if ($subject instanceof Traversable) {
  1064. $subject = Arr::from($subject);
  1065. }
  1066. return $caseSensitive
  1067. ? str_replace($search, $replace, $subject)
  1068. : str_ireplace($search, $replace, $subject);
  1069. }
  1070. /**
  1071. * Replace the first occurrence of a given value in the string.
  1072. *
  1073. * @param string $search
  1074. * @param string $replace
  1075. * @param string $subject
  1076. * @return string
  1077. */
  1078. public static function replaceFirst($search, $replace, $subject)
  1079. {
  1080. $search = (string) $search;
  1081. if ($search === '') {
  1082. return $subject;
  1083. }
  1084. $position = strpos($subject, $search);
  1085. if ($position !== false) {
  1086. return substr_replace($subject, $replace, $position, strlen($search));
  1087. }
  1088. return $subject;
  1089. }
  1090. /**
  1091. * Replace the first occurrence of the given value if it appears at the start of the string.
  1092. *
  1093. * @param string $search
  1094. * @param string $replace
  1095. * @param string $subject
  1096. * @return string
  1097. */
  1098. public static function replaceStart($search, $replace, $subject)
  1099. {
  1100. $search = (string) $search;
  1101. if ($search === '') {
  1102. return $subject;
  1103. }
  1104. if (static::startsWith($subject, $search)) {
  1105. return static::replaceFirst($search, $replace, $subject);
  1106. }
  1107. return $subject;
  1108. }
  1109. /**
  1110. * Replace the last occurrence of a given value in the string.
  1111. *
  1112. * @param string $search
  1113. * @param string $replace
  1114. * @param string $subject
  1115. * @return string
  1116. */
  1117. public static function replaceLast($search, $replace, $subject)
  1118. {
  1119. $search = (string) $search;
  1120. if ($search === '') {
  1121. return $subject;
  1122. }
  1123. $position = strrpos($subject, $search);
  1124. if ($position !== false) {
  1125. return substr_replace($subject, $replace, $position, strlen($search));
  1126. }
  1127. return $subject;
  1128. }
  1129. /**
  1130. * Replace the last occurrence of a given value if it appears at the end of the string.
  1131. *
  1132. * @param string $search
  1133. * @param string $replace
  1134. * @param string $subject
  1135. * @return string
  1136. */
  1137. public static function replaceEnd($search, $replace, $subject)
  1138. {
  1139. $search = (string) $search;
  1140. if ($search === '') {
  1141. return $subject;
  1142. }
  1143. if (static::endsWith($subject, $search)) {
  1144. return static::replaceLast($search, $replace, $subject);
  1145. }
  1146. return $subject;
  1147. }
  1148. /**
  1149. * Replace the patterns matching the given regular expression.
  1150. *
  1151. * @param array|string $pattern
  1152. * @param \Closure|string[]|string $replace
  1153. * @param array|string $subject
  1154. * @param int $limit
  1155. * @return string|string[]|null
  1156. */
  1157. public static function replaceMatches($pattern, $replace, $subject, $limit = -1)
  1158. {
  1159. if ($replace instanceof Closure) {
  1160. return preg_replace_callback($pattern, $replace, $subject, $limit);
  1161. }
  1162. return preg_replace($pattern, $replace, $subject, $limit);
  1163. }
  1164. /**
  1165. * Remove any occurrence of the given string in the subject.
  1166. *
  1167. * @param string|iterable<string> $search
  1168. * @param string|iterable<string> $subject
  1169. * @param bool $caseSensitive
  1170. * @return string
  1171. */
  1172. public static function remove($search, $subject, $caseSensitive = true)
  1173. {
  1174. if ($search instanceof Traversable) {
  1175. $search = Arr::from($search);
  1176. }
  1177. return $caseSensitive
  1178. ? str_replace($search, '', $subject)
  1179. : str_ireplace($search, '', $subject);
  1180. }
  1181. /**
  1182. * Reverse the given string.
  1183. *
  1184. * @param string $value
  1185. * @return string
  1186. */
  1187. public static function reverse(string $value)
  1188. {
  1189. return implode(array_reverse(mb_str_split($value)));
  1190. }
  1191. /**
  1192. * Begin a string with a single instance of a given value.
  1193. *
  1194. * @param string $value
  1195. * @param string $prefix
  1196. * @return string
  1197. */
  1198. public static function start($value, $prefix)
  1199. {
  1200. $quoted = preg_quote($prefix, '/');
  1201. return $prefix.preg_replace('/^(?:'.$quoted.')+/u', '', $value);
  1202. }
  1203. /**
  1204. * Convert the given string to upper-case.
  1205. *
  1206. * @param string $value
  1207. * @return string
  1208. */
  1209. public static function upper($value)
  1210. {
  1211. return mb_strtoupper($value, 'UTF-8');
  1212. }
  1213. /**
  1214. * Convert the given string to proper case.
  1215. *
  1216. * @param string $value
  1217. * @return string
  1218. */
  1219. public static function title($value)
  1220. {
  1221. return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
  1222. }
  1223. /**
  1224. * Convert the given string to proper case for each word.
  1225. *
  1226. * @param string $value
  1227. * @return string
  1228. */
  1229. public static function headline($value)
  1230. {
  1231. $parts = mb_split('\s+', $value);
  1232. $parts = count($parts) > 1
  1233. ? array_map(static::title(...), $parts)
  1234. : array_map(static::title(...), static::ucsplit(implode('_', $parts)));
  1235. $collapsed = static::replace(['-', '_', ' '], '_', implode('_', $parts));
  1236. return implode(' ', array_filter(explode('_', $collapsed)));
  1237. }
  1238. /**
  1239. * Convert the given string to APA-style title case.
  1240. *
  1241. * See: https://apastyle.apa.org/style-grammar-guidelines/capitalization/title-case
  1242. *
  1243. * @param string $value
  1244. * @return string
  1245. */
  1246. public static function apa($value)
  1247. {
  1248. if (trim($value) === '') {
  1249. return $value;
  1250. }
  1251. $minorWords = [
  1252. 'and', 'as', 'but', 'for', 'if', 'nor', 'or', 'so', 'yet', 'a', 'an',
  1253. 'the', 'at', 'by', 'in', 'of', 'off', 'on', 'per', 'to', 'up', 'via',
  1254. 'et', 'ou', 'un', 'une', 'la', 'le', 'les', 'de', 'du', 'des', 'par', 'à',
  1255. ];
  1256. $endPunctuation = ['.', '!', '?', ':', '—', ','];
  1257. $words = mb_split('\s+', $value);
  1258. $wordCount = count($words);
  1259. for ($i = 0; $i < $wordCount; $i++) {
  1260. $lowercaseWord = mb_strtolower($words[$i]);
  1261. if (str_contains($lowercaseWord, '-')) {
  1262. $hyphenatedWords = explode('-', $lowercaseWord);
  1263. $hyphenatedWords = array_map(function ($part) use ($minorWords) {
  1264. return (in_array($part, $minorWords) && mb_strlen($part) <= 3)
  1265. ? $part
  1266. : mb_strtoupper(mb_substr($part, 0, 1)).mb_substr($part, 1);
  1267. }, $hyphenatedWords);
  1268. $words[$i] = implode('-', $hyphenatedWords);
  1269. } else {
  1270. if (in_array($lowercaseWord, $minorWords) &&
  1271. mb_strlen($lowercaseWord) <= 3 &&
  1272. ! ($i === 0 || in_array(mb_substr($words[$i - 1], -1), $endPunctuation))) {
  1273. $words[$i] = $lowercaseWord;
  1274. } else {
  1275. $words[$i] = mb_strtoupper(mb_substr($lowercaseWord, 0, 1)).mb_substr($lowercaseWord, 1);
  1276. }
  1277. }
  1278. }
  1279. return implode(' ', $words);
  1280. }
  1281. /**
  1282. * Get the singular form of an English word.
  1283. *
  1284. * @param string $value
  1285. * @return string
  1286. */
  1287. public static function singular($value)
  1288. {
  1289. return Pluralizer::singular($value);
  1290. }
  1291. /**
  1292. * Generate a URL friendly "slug" from a given string.
  1293. *
  1294. * @param string $title
  1295. * @param string $separator
  1296. * @param string|null $language
  1297. * @param array<string, string> $dictionary
  1298. * @return string
  1299. */
  1300. public static function slug($title, $separator = '-', $language = 'en', $dictionary = ['@' => 'at'])
  1301. {
  1302. $title = $language ? static::ascii($title, $language) : $title;
  1303. // Convert all dashes/underscores into separator
  1304. $flip = $separator === '-' ? '_' : '-';
  1305. $title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);
  1306. // Replace dictionary words
  1307. foreach ($dictionary as $key => $value) {
  1308. $dictionary[$key] = $separator.$value.$separator;
  1309. }
  1310. $title = str_replace(array_keys($dictionary), array_values($dictionary), $title);
  1311. // Remove all characters that are not the separator, letters, numbers, or whitespace
  1312. $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', static::lower($title));
  1313. // Replace all separator characters and whitespace by a single separator
  1314. $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
  1315. return trim($title, $separator);
  1316. }
  1317. /**
  1318. * Convert a string to snake case.
  1319. *
  1320. * @param string $value
  1321. * @param string $delimiter
  1322. * @return string
  1323. */
  1324. public static function snake($value, $delimiter = '_')
  1325. {
  1326. $key = $value;
  1327. if (isset(static::$snakeCache[$key][$delimiter])) {
  1328. return static::$snakeCache[$key][$delimiter];
  1329. }
  1330. if (! ctype_lower($value)) {
  1331. $value = preg_replace('/\s+/u', '', ucwords($value));
  1332. $value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value));
  1333. }
  1334. return static::$snakeCache[$key][$delimiter] = $value;
  1335. }
  1336. /**
  1337. * Remove all whitespace from both ends of a string.
  1338. *
  1339. * @param string $value
  1340. * @param string|null $charlist
  1341. * @return string
  1342. */
  1343. public static function trim($value, $charlist = null)
  1344. {
  1345. if ($charlist === null) {
  1346. $trimDefaultCharacters = " \n\r\t\v\0";
  1347. return preg_replace('~^[\s'.self::INVISIBLE_CHARACTERS.$trimDefaultCharacters.']+|[\s'.self::INVISIBLE_CHARACTERS.$trimDefaultCharacters.']+$~u', '', $value) ?? trim($value);
  1348. }
  1349. return trim($value, $charlist);
  1350. }
  1351. /**
  1352. * Remove all whitespace from the beginning of a string.
  1353. *
  1354. * @param string $value
  1355. * @param string|null $charlist
  1356. * @return string
  1357. */
  1358. public static function ltrim($value, $charlist = null)
  1359. {
  1360. if ($charlist === null) {
  1361. $ltrimDefaultCharacters = " \n\r\t\v\0";
  1362. return preg_replace('~^[\s'.self::INVISIBLE_CHARACTERS.$ltrimDefaultCharacters.']+~u', '', $value) ?? ltrim($value);
  1363. }
  1364. return ltrim($value, $charlist);
  1365. }
  1366. /**
  1367. * Remove all whitespace from the end of a string.
  1368. *
  1369. * @param string $value
  1370. * @param string|null $charlist
  1371. * @return string
  1372. */
  1373. public static function rtrim($value, $charlist = null)
  1374. {
  1375. if ($charlist === null) {
  1376. $rtrimDefaultCharacters = " \n\r\t\v\0";
  1377. return preg_replace('~[\s'.self::INVISIBLE_CHARACTERS.$rtrimDefaultCharacters.']+$~u', '', $value) ?? rtrim($value);
  1378. }
  1379. return rtrim($value, $charlist);
  1380. }
  1381. /**
  1382. * Remove all "extra" blank space from the given string.
  1383. *
  1384. * @param string $value
  1385. * @return string
  1386. */
  1387. public static function squish($value)
  1388. {
  1389. return preg_replace('~(\s|\x{3164}|\x{1160})+~u', ' ', static::trim($value));
  1390. }
  1391. /**
  1392. * Determine if a given string starts with a given substring.
  1393. *
  1394. * @param string $haystack
  1395. * @param string|iterable<string> $needles
  1396. * @return bool
  1397. */
  1398. public static function startsWith($haystack, $needles)
  1399. {
  1400. if (is_null($haystack)) {
  1401. return false;
  1402. }
  1403. if (! is_iterable($needles)) {
  1404. $needles = [$needles];
  1405. }
  1406. foreach ($needles as $needle) {
  1407. if ((string) $needle !== '' && str_starts_with($haystack, $needle)) {
  1408. return true;
  1409. }
  1410. }
  1411. return false;
  1412. }
  1413. /**
  1414. * Determine if a given string doesn't start with a given substring.
  1415. *
  1416. * @param string $haystack
  1417. * @param string|iterable<string> $needles
  1418. * @return bool
  1419. */
  1420. public static function doesntStartWith($haystack, $needles)
  1421. {
  1422. return ! static::startsWith($haystack, $needles);
  1423. }
  1424. /**
  1425. * Convert a value to studly caps case.
  1426. *
  1427. * @param string $value
  1428. * @return string
  1429. */
  1430. public static function studly($value)
  1431. {
  1432. $key = $value;
  1433. if (isset(static::$studlyCache[$key])) {
  1434. return static::$studlyCache[$key];
  1435. }
  1436. $words = mb_split('\s+', static::replace(['-', '_'], ' ', $value));
  1437. $studlyWords = array_map(fn ($word) => static::ucfirst($word), $words);
  1438. return static::$studlyCache[$key] = implode($studlyWords);
  1439. }
  1440. /**
  1441. * Convert a value to Pascal case.
  1442. *
  1443. * @param string $value
  1444. * @return string
  1445. */
  1446. public static function pascal($value)
  1447. {
  1448. return static::studly($value);
  1449. }
  1450. /**
  1451. * Returns the portion of the string specified by the start and length parameters.
  1452. *
  1453. * @param string $string
  1454. * @param int $start
  1455. * @param int|null $length
  1456. * @param string $encoding
  1457. * @return string
  1458. */
  1459. public static function substr($string, $start, $length = null, $encoding = 'UTF-8')
  1460. {
  1461. return mb_substr($string, $start, $length, $encoding);
  1462. }
  1463. /**
  1464. * Returns the number of substring occurrences.
  1465. *
  1466. * @param string $haystack
  1467. * @param string $needle
  1468. * @param int $offset
  1469. * @param int|null $length
  1470. * @return int
  1471. */
  1472. public static function substrCount($haystack, $needle, $offset = 0, $length = null)
  1473. {
  1474. if (! is_null($length)) {
  1475. return substr_count($haystack, $needle, $offset, $length);
  1476. }
  1477. return substr_count($haystack, $needle, $offset);
  1478. }
  1479. /**
  1480. * Replace text within a portion of a string.
  1481. *
  1482. * @param string|string[] $string
  1483. * @param string|string[] $replace
  1484. * @param int|int[] $offset
  1485. * @param int|int[]|null $length
  1486. * @return string|string[]
  1487. */
  1488. public static function substrReplace($string, $replace, $offset = 0, $length = null)
  1489. {
  1490. if ($length === null) {
  1491. $length = strlen($string);
  1492. }
  1493. return substr_replace($string, $replace, $offset, $length);
  1494. }
  1495. /**
  1496. * Swap multiple keywords in a string with other keywords.
  1497. *
  1498. * @param array $map
  1499. * @param string $subject
  1500. * @return string
  1501. */
  1502. public static function swap(array $map, $subject)
  1503. {
  1504. return strtr($subject, $map);
  1505. }
  1506. /**
  1507. * Take the first or last {$limit} characters of a string.
  1508. *
  1509. * @param string $string
  1510. * @param int $limit
  1511. * @return string
  1512. */
  1513. public static function take($string, int $limit): string
  1514. {
  1515. if ($limit < 0) {
  1516. return static::substr($string, $limit);
  1517. }
  1518. return static::substr($string, 0, $limit);
  1519. }
  1520. /**
  1521. * Convert the given string to Base64 encoding.
  1522. *
  1523. * @param string $string
  1524. * @return string
  1525. */
  1526. public static function toBase64($string): string
  1527. {
  1528. return base64_encode($string);
  1529. }
  1530. /**
  1531. * Decode the given Base64 encoded string.
  1532. *
  1533. * @param string $string
  1534. * @param bool $strict
  1535. * @return string|false
  1536. */
  1537. public static function fromBase64($string, $strict = false)
  1538. {
  1539. return base64_decode($string, $strict);
  1540. }
  1541. /**
  1542. * Make a string's first character lowercase.
  1543. *
  1544. * @param string $string
  1545. * @return string
  1546. */
  1547. public static function lcfirst($string)
  1548. {
  1549. return static::lower(static::substr($string, 0, 1)).static::substr($string, 1);
  1550. }
  1551. /**
  1552. * Make a string's first character uppercase.
  1553. *
  1554. * @param string $string
  1555. * @return string
  1556. */
  1557. public static function ucfirst($string)
  1558. {
  1559. return static::upper(static::substr($string, 0, 1)).static::substr($string, 1);
  1560. }
  1561. /**
  1562. * Capitalize the first character of each word in a string.
  1563. *
  1564. * @param string $string
  1565. * @param string $separators
  1566. * @return string
  1567. */
  1568. public static function ucwords($string, $separators = " \t\r\n\f\v")
  1569. {
  1570. $pattern = '/(^|['.preg_quote($separators, '/').'])(\p{Ll})/u';
  1571. return preg_replace_callback($pattern, function ($matches) {
  1572. return $matches[1].mb_strtoupper($matches[2]);
  1573. }, $string);
  1574. }
  1575. /**
  1576. * Split a string into pieces by uppercase characters.
  1577. *
  1578. * @param string $string
  1579. * @return string[]
  1580. */
  1581. public static function ucsplit($string)
  1582. {
  1583. return preg_split('/(?=\p{Lu})/u', $string, -1, PREG_SPLIT_NO_EMPTY);
  1584. }
  1585. /**
  1586. * Get the number of words a string contains.
  1587. *
  1588. * @param string $string
  1589. * @param string|null $characters
  1590. * @return int
  1591. */
  1592. public static function wordCount($string, $characters = null)
  1593. {
  1594. return str_word_count($string, 0, $characters);
  1595. }
  1596. /**
  1597. * Wrap a string to a given number of characters.
  1598. *
  1599. * @param string $string
  1600. * @param int $characters
  1601. * @param string $break
  1602. * @param bool $cutLongWords
  1603. * @return string
  1604. */
  1605. public static function wordWrap($string, $characters = 75, $break = "\n", $cutLongWords = false)
  1606. {
  1607. return wordwrap($string, $characters, $break, $cutLongWords);
  1608. }
  1609. /**
  1610. * Generate a UUID (version 4).
  1611. *
  1612. * @return \Ramsey\Uuid\UuidInterface
  1613. */
  1614. public static function uuid()
  1615. {
  1616. return static::$uuidFactory
  1617. ? call_user_func(static::$uuidFactory)
  1618. : Uuid::uuid4();
  1619. }
  1620. /**
  1621. * Generate a UUID (version 7).
  1622. *
  1623. * @param \DateTimeInterface|null $time
  1624. * @return \Ramsey\Uuid\UuidInterface
  1625. */
  1626. public static function uuid7($time = null)
  1627. {
  1628. return static::$uuidFactory
  1629. ? call_user_func(static::$uuidFactory)
  1630. : Uuid::uuid7($time);
  1631. }
  1632. /**
  1633. * Generate a time-ordered UUID.
  1634. *
  1635. * @return \Ramsey\Uuid\UuidInterface
  1636. */
  1637. public static function orderedUuid()
  1638. {
  1639. if (static::$uuidFactory) {
  1640. return call_user_func(static::$uuidFactory);
  1641. }
  1642. $factory = new UuidFactory;
  1643. $factory->setRandomGenerator(new CombGenerator(
  1644. $factory->getRandomGenerator(),
  1645. $factory->getNumberConverter()
  1646. ));
  1647. $factory->setCodec(new TimestampFirstCombCodec(
  1648. $factory->getUuidBuilder()
  1649. ));
  1650. return $factory->uuid4();
  1651. }
  1652. /**
  1653. * Set the callable that will be used to generate UUIDs.
  1654. *
  1655. * @param (callable(): \Ramsey\Uuid\UuidInterface)|null $factory
  1656. * @return void
  1657. */
  1658. public static function createUuidsUsing(?callable $factory = null)
  1659. {
  1660. static::$uuidFactory = $factory;
  1661. }
  1662. /**
  1663. * Set the sequence that will be used to generate UUIDs.
  1664. *
  1665. * @param array $sequence
  1666. * @param (callable(): \Ramsey\Uuid\UuidInterface)|null $whenMissing
  1667. * @return void
  1668. */
  1669. public static function createUuidsUsingSequence(array $sequence, $whenMissing = null)
  1670. {
  1671. $next = 0;
  1672. $whenMissing ??= function () use (&$next) {
  1673. $factoryCache = static::$uuidFactory;
  1674. static::$uuidFactory = null;
  1675. $uuid = static::uuid();
  1676. static::$uuidFactory = $factoryCache;
  1677. $next++;
  1678. return $uuid;
  1679. };
  1680. static::createUuidsUsing(function () use (&$next, $sequence, $whenMissing) {
  1681. if (array_key_exists($next, $sequence)) {
  1682. return $sequence[$next++];
  1683. }
  1684. return $whenMissing();
  1685. });
  1686. }
  1687. /**
  1688. * Always return the same UUID when generating new UUIDs.
  1689. *
  1690. * @param \Closure|null $callback
  1691. * @return \Ramsey\Uuid\UuidInterface
  1692. */
  1693. public static function freezeUuids(?Closure $callback = null)
  1694. {
  1695. $uuid = Str::uuid();
  1696. Str::createUuidsUsing(fn () => $uuid);
  1697. if ($callback !== null) {
  1698. try {
  1699. $callback($uuid);
  1700. } finally {
  1701. Str::createUuidsNormally();
  1702. }
  1703. }
  1704. return $uuid;
  1705. }
  1706. /**
  1707. * Indicate that UUIDs should be created normally and not using a custom factory.
  1708. *
  1709. * @return void
  1710. */
  1711. public static function createUuidsNormally()
  1712. {
  1713. static::$uuidFactory = null;
  1714. }
  1715. /**
  1716. * Generate a ULID.
  1717. *
  1718. * @param \DateTimeInterface|null $time
  1719. * @return \Symfony\Component\Uid\Ulid
  1720. */
  1721. public static function ulid($time = null)
  1722. {
  1723. if (static::$ulidFactory) {
  1724. return call_user_func(static::$ulidFactory);
  1725. }
  1726. if ($time === null) {
  1727. return new Ulid();
  1728. }
  1729. return new Ulid(Ulid::generate($time));
  1730. }
  1731. /**
  1732. * Indicate that ULIDs should be created normally and not using a custom factory.
  1733. *
  1734. * @return void
  1735. */
  1736. public static function createUlidsNormally()
  1737. {
  1738. static::$ulidFactory = null;
  1739. }
  1740. /**
  1741. * Set the callable that will be used to generate ULIDs.
  1742. *
  1743. * @param (callable(): \Symfony\Component\Uid\Ulid)|null $factory
  1744. * @return void
  1745. */
  1746. public static function createUlidsUsing(?callable $factory = null)
  1747. {
  1748. static::$ulidFactory = $factory;
  1749. }
  1750. /**
  1751. * Set the sequence that will be used to generate ULIDs.
  1752. *
  1753. * @param array $sequence
  1754. * @param (callable(): \Symfony\Component\Uid\Ulid)|null $whenMissing
  1755. * @return void
  1756. */
  1757. public static function createUlidsUsingSequence(array $sequence, $whenMissing = null)
  1758. {
  1759. $next = 0;
  1760. $whenMissing ??= function () use (&$next) {
  1761. $factoryCache = static::$ulidFactory;
  1762. static::$ulidFactory = null;
  1763. $ulid = static::ulid();
  1764. static::$ulidFactory = $factoryCache;
  1765. $next++;
  1766. return $ulid;
  1767. };
  1768. static::createUlidsUsing(function () use (&$next, $sequence, $whenMissing) {
  1769. if (array_key_exists($next, $sequence)) {
  1770. return $sequence[$next++];
  1771. }
  1772. return $whenMissing();
  1773. });
  1774. }
  1775. /**
  1776. * Always return the same ULID when generating new ULIDs.
  1777. *
  1778. * @param Closure|null $callback
  1779. * @return Ulid
  1780. */
  1781. public static function freezeUlids(?Closure $callback = null)
  1782. {
  1783. $ulid = Str::ulid();
  1784. Str::createUlidsUsing(fn () => $ulid);
  1785. if ($callback !== null) {
  1786. try {
  1787. $callback($ulid);
  1788. } finally {
  1789. Str::createUlidsNormally();
  1790. }
  1791. }
  1792. return $ulid;
  1793. }
  1794. /**
  1795. * Remove all strings from the casing caches.
  1796. *
  1797. * @return void
  1798. */
  1799. public static function flushCache()
  1800. {
  1801. static::$snakeCache = [];
  1802. static::$camelCache = [];
  1803. static::$studlyCache = [];
  1804. }
  1805. }