helpers.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <?php
  2. use Illuminate\Contracts\Support\DeferringDisplayableValue;
  3. use Illuminate\Contracts\Support\Htmlable;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Support\Env;
  6. use Illuminate\Support\Fluent;
  7. use Illuminate\Support\HigherOrderTapProxy;
  8. use Illuminate\Support\Once;
  9. use Illuminate\Support\Onceable;
  10. use Illuminate\Support\Optional;
  11. use Illuminate\Support\Sleep;
  12. use Illuminate\Support\Str;
  13. if (! function_exists('append_config')) {
  14. /**
  15. * Assign high numeric IDs to a config item to force appending.
  16. *
  17. * @param array $array
  18. * @return array
  19. */
  20. function append_config(array $array)
  21. {
  22. $start = 9999;
  23. foreach ($array as $key => $value) {
  24. if (is_numeric($key)) {
  25. $start++;
  26. $array[$start] = Arr::pull($array, $key);
  27. }
  28. }
  29. return $array;
  30. }
  31. }
  32. if (! function_exists('blank')) {
  33. /**
  34. * Determine if the given value is "blank".
  35. *
  36. * @phpstan-assert-if-false !=null|'' $value
  37. *
  38. * @phpstan-assert-if-true !=numeric|bool $value
  39. *
  40. * @param mixed $value
  41. * @return bool
  42. */
  43. function blank($value)
  44. {
  45. if (is_null($value)) {
  46. return true;
  47. }
  48. if (is_string($value)) {
  49. return trim($value) === '';
  50. }
  51. if (is_numeric($value) || is_bool($value)) {
  52. return false;
  53. }
  54. if ($value instanceof Countable) {
  55. return count($value) === 0;
  56. }
  57. if ($value instanceof Stringable) {
  58. return trim((string) $value) === '';
  59. }
  60. return empty($value);
  61. }
  62. }
  63. if (! function_exists('class_basename')) {
  64. /**
  65. * Get the class "basename" of the given object / class.
  66. *
  67. * @param string|object $class
  68. * @return string
  69. */
  70. function class_basename($class)
  71. {
  72. $class = is_object($class) ? get_class($class) : $class;
  73. return basename(str_replace('\\', '/', $class));
  74. }
  75. }
  76. if (! function_exists('class_uses_recursive')) {
  77. /**
  78. * Returns all traits used by a class, its parent classes and trait of their traits.
  79. *
  80. * @param object|string $class
  81. * @return array
  82. */
  83. function class_uses_recursive($class)
  84. {
  85. if (is_object($class)) {
  86. $class = get_class($class);
  87. }
  88. $results = [];
  89. foreach (array_reverse(class_parents($class) ?: []) + [$class => $class] as $class) {
  90. $results += trait_uses_recursive($class);
  91. }
  92. return array_unique($results);
  93. }
  94. }
  95. if (! function_exists('e')) {
  96. /**
  97. * Encode HTML special characters in a string.
  98. *
  99. * @param \Illuminate\Contracts\Support\DeferringDisplayableValue|\Illuminate\Contracts\Support\Htmlable|\BackedEnum|string|int|float|null $value
  100. * @param bool $doubleEncode
  101. * @return string
  102. */
  103. function e($value, $doubleEncode = true)
  104. {
  105. if ($value instanceof DeferringDisplayableValue) {
  106. $value = $value->resolveDisplayableValue();
  107. }
  108. if ($value instanceof Htmlable) {
  109. return $value->toHtml();
  110. }
  111. if ($value instanceof BackedEnum) {
  112. $value = $value->value;
  113. }
  114. return htmlspecialchars($value ?? '', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', $doubleEncode);
  115. }
  116. }
  117. if (! function_exists('env')) {
  118. /**
  119. * Gets the value of an environment variable.
  120. *
  121. * @param string $key
  122. * @param mixed $default
  123. * @return mixed
  124. */
  125. function env($key, $default = null)
  126. {
  127. return Env::get($key, $default);
  128. }
  129. }
  130. if (! function_exists('filled')) {
  131. /**
  132. * Determine if a value is "filled".
  133. *
  134. * @phpstan-assert-if-true !=null|'' $value
  135. *
  136. * @phpstan-assert-if-false !=numeric|bool $value
  137. *
  138. * @param mixed $value
  139. * @return bool
  140. */
  141. function filled($value)
  142. {
  143. return ! blank($value);
  144. }
  145. }
  146. if (! function_exists('fluent')) {
  147. /**
  148. * Create an Fluent object from the given value.
  149. *
  150. * @param object|array $value
  151. * @return \Illuminate\Support\Fluent
  152. */
  153. function fluent($value)
  154. {
  155. return new Fluent($value);
  156. }
  157. }
  158. if (! function_exists('literal')) {
  159. /**
  160. * Return a new literal or anonymous object using named arguments.
  161. *
  162. * @return \stdClass
  163. */
  164. function literal(...$arguments)
  165. {
  166. if (count($arguments) === 1 && array_is_list($arguments)) {
  167. return $arguments[0];
  168. }
  169. return (object) $arguments;
  170. }
  171. }
  172. if (! function_exists('object_get')) {
  173. /**
  174. * Get an item from an object using "dot" notation.
  175. *
  176. * @template TValue of object
  177. *
  178. * @param TValue $object
  179. * @param string|null $key
  180. * @param mixed $default
  181. * @return ($key is empty ? TValue : mixed)
  182. */
  183. function object_get($object, $key, $default = null)
  184. {
  185. if (is_null($key) || trim($key) === '') {
  186. return $object;
  187. }
  188. foreach (explode('.', $key) as $segment) {
  189. if (! is_object($object) || ! isset($object->{$segment})) {
  190. return value($default);
  191. }
  192. $object = $object->{$segment};
  193. }
  194. return $object;
  195. }
  196. }
  197. if (! function_exists('once')) {
  198. /**
  199. * Ensures a callable is only called once, and returns the result on subsequent calls.
  200. *
  201. * @template TReturnType
  202. *
  203. * @param callable(): TReturnType $callback
  204. * @return TReturnType
  205. */
  206. function once(callable $callback)
  207. {
  208. $onceable = Onceable::tryFromTrace(
  209. debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2),
  210. $callback,
  211. );
  212. return $onceable ? Once::instance()->value($onceable) : call_user_func($callback);
  213. }
  214. }
  215. if (! function_exists('optional')) {
  216. /**
  217. * Provide access to optional objects.
  218. *
  219. * @template TValue
  220. * @template TReturn
  221. *
  222. * @param TValue $value
  223. * @param (callable(TValue): TReturn)|null $callback
  224. * @return ($callback is null ? \Illuminate\Support\Optional : ($value is null ? null : TReturn))
  225. */
  226. function optional($value = null, ?callable $callback = null)
  227. {
  228. if (is_null($callback)) {
  229. return new Optional($value);
  230. } elseif (! is_null($value)) {
  231. return $callback($value);
  232. }
  233. }
  234. }
  235. if (! function_exists('preg_replace_array')) {
  236. /**
  237. * Replace a given pattern with each value in the array in sequentially.
  238. *
  239. * @param string $pattern
  240. * @param array $replacements
  241. * @param string $subject
  242. * @return string
  243. */
  244. function preg_replace_array($pattern, array $replacements, $subject)
  245. {
  246. return preg_replace_callback($pattern, function () use (&$replacements) {
  247. foreach ($replacements as $value) {
  248. return array_shift($replacements);
  249. }
  250. }, $subject);
  251. }
  252. }
  253. if (! function_exists('retry')) {
  254. /**
  255. * Retry an operation a given number of times.
  256. *
  257. * @template TValue
  258. *
  259. * @param int|array<int, int> $times
  260. * @param callable(int): TValue $callback
  261. * @param int|\Closure(int, \Throwable): int $sleepMilliseconds
  262. * @param (callable(\Throwable): bool)|null $when
  263. * @return TValue
  264. *
  265. * @throws \Throwable
  266. */
  267. function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null)
  268. {
  269. $attempts = 0;
  270. $backoff = [];
  271. if (is_array($times)) {
  272. $backoff = $times;
  273. $times = count($times) + 1;
  274. }
  275. beginning:
  276. $attempts++;
  277. $times--;
  278. try {
  279. return $callback($attempts);
  280. } catch (Throwable $e) {
  281. if ($times < 1 || ($when && ! $when($e))) {
  282. throw $e;
  283. }
  284. $sleepMilliseconds = $backoff[$attempts - 1] ?? $sleepMilliseconds;
  285. if ($sleepMilliseconds) {
  286. Sleep::usleep(value($sleepMilliseconds, $attempts, $e) * 1000);
  287. }
  288. goto beginning;
  289. }
  290. }
  291. }
  292. if (! function_exists('str')) {
  293. /**
  294. * Get a new stringable object from the given string.
  295. *
  296. * @param string|null $string
  297. * @return ($string is null ? object : \Illuminate\Support\Stringable)
  298. */
  299. function str($string = null)
  300. {
  301. if (func_num_args() === 0) {
  302. return new class
  303. {
  304. public function __call($method, $parameters)
  305. {
  306. return Str::$method(...$parameters);
  307. }
  308. public function __toString()
  309. {
  310. return '';
  311. }
  312. };
  313. }
  314. return Str::of($string);
  315. }
  316. }
  317. if (! function_exists('tap')) {
  318. /**
  319. * Call the given Closure with the given value then return the value.
  320. *
  321. * @template TValue
  322. *
  323. * @param TValue $value
  324. * @param (callable(TValue): mixed)|null $callback
  325. * @return ($callback is null ? \Illuminate\Support\HigherOrderTapProxy : TValue)
  326. */
  327. function tap($value, $callback = null)
  328. {
  329. if (is_null($callback)) {
  330. return new HigherOrderTapProxy($value);
  331. }
  332. $callback($value);
  333. return $value;
  334. }
  335. }
  336. if (! function_exists('throw_if')) {
  337. /**
  338. * Throw the given exception if the given condition is true.
  339. *
  340. * @template TValue
  341. * @template TException of \Throwable
  342. *
  343. * @param TValue $condition
  344. * @param TException|class-string<TException>|string $exception
  345. * @param mixed ...$parameters
  346. * @return TValue
  347. *
  348. * @throws TException
  349. */
  350. function throw_if($condition, $exception = 'RuntimeException', ...$parameters)
  351. {
  352. if ($condition) {
  353. if (is_string($exception) && class_exists($exception)) {
  354. $exception = new $exception(...$parameters);
  355. }
  356. throw is_string($exception) ? new RuntimeException($exception) : $exception;
  357. }
  358. return $condition;
  359. }
  360. }
  361. if (! function_exists('throw_unless')) {
  362. /**
  363. * Throw the given exception unless the given condition is true.
  364. *
  365. * @template TValue
  366. * @template TException of \Throwable
  367. *
  368. * @param TValue $condition
  369. * @param TException|class-string<TException>|string $exception
  370. * @param mixed ...$parameters
  371. * @return TValue
  372. *
  373. * @throws TException
  374. */
  375. function throw_unless($condition, $exception = 'RuntimeException', ...$parameters)
  376. {
  377. throw_if(! $condition, $exception, ...$parameters);
  378. return $condition;
  379. }
  380. }
  381. if (! function_exists('trait_uses_recursive')) {
  382. /**
  383. * Returns all traits used by a trait and its traits.
  384. *
  385. * @param object|string $trait
  386. * @return array
  387. */
  388. function trait_uses_recursive($trait)
  389. {
  390. $traits = class_uses($trait) ?: [];
  391. foreach ($traits as $trait) {
  392. $traits += trait_uses_recursive($trait);
  393. }
  394. return $traits;
  395. }
  396. }
  397. if (! function_exists('transform')) {
  398. /**
  399. * Transform the given value if it is present.
  400. *
  401. * @template TValue
  402. * @template TReturn
  403. * @template TDefault
  404. *
  405. * @param TValue $value
  406. * @param callable(TValue): TReturn $callback
  407. * @param TDefault|callable(TValue): TDefault $default
  408. * @return ($value is empty ? TDefault : TReturn)
  409. */
  410. function transform($value, callable $callback, $default = null)
  411. {
  412. if (filled($value)) {
  413. return $callback($value);
  414. }
  415. if (is_callable($default)) {
  416. return $default($value);
  417. }
  418. return $default;
  419. }
  420. }
  421. if (! function_exists('windows_os')) {
  422. /**
  423. * Determine whether the current environment is Windows based.
  424. *
  425. * @return bool
  426. */
  427. function windows_os()
  428. {
  429. return PHP_OS_FAMILY === 'Windows';
  430. }
  431. }
  432. if (! function_exists('with')) {
  433. /**
  434. * Return the given value, optionally passed through the given callback.
  435. *
  436. * @template TValue
  437. * @template TReturn
  438. *
  439. * @param TValue $value
  440. * @param (callable(TValue): (TReturn))|null $callback
  441. * @return ($callback is null ? TValue : TReturn)
  442. */
  443. function with($value, ?callable $callback = null)
  444. {
  445. return is_null($callback) ? $value : $callback($value);
  446. }
  447. }