helpers.php 13 KB

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