generated from farzai/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
77 lines (68 loc) · 2.21 KB
/
functions.php
File metadata and controls
77 lines (68 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
declare(strict_types=1);
namespace Farzai\Support;
use DateTimeZone;
/**
* Call the given Closure with the given value then return the value.
*
* This function allows you to "tap" into a method chain, perform some action,
* and continue the chain. When called without a callback, it returns a
* HigherOrderTapProxy for fluent method chaining.
*
* @param mixed $value The value to tap into
* @param callable|null $callback Optional callback to execute with the value
* @return mixed The original value or a HigherOrderTapProxy if no callback provided
*
* @example
* // With callback
* $user = tap($user, function ($u) {
* $u->update(['last_login' => now()]);
* });
*
* @example
* // Without callback (higher-order proxy)
* $user = tap($user)->update(['last_login' => now()])->save();
*/
function tap(mixed $value, ?callable $callback = null): mixed
{
if (is_null($callback)) {
return is_object($value) ? new HigherOrderTapProxy($value) : $value;
}
$callback($value);
return $value;
}
/**
* Get the current date and time.
*
* Returns a Carbon instance representing the current date/time in the specified timezone.
*
* @param \DateTimeZone|string|null $timezone Optional timezone (defaults to app timezone)
* @return Carbon A Carbon instance representing now
*
* @example
* now(); // Returns: Carbon instance of current time
* now('America/New_York'); // Returns: Carbon instance in NY timezone
* now()->addDays(5); // Returns: Carbon instance 5 days from now
*/
function now(DateTimeZone|string|null $timezone = null): Carbon
{
return Carbon::now($timezone);
}
/**
* Get the class "basename" of the given object or class.
*
* Returns the class name without the namespace.
*
* @param object|string $class The object instance or fully-qualified class name
* @return string The class basename (without namespace)
*
* @example
* class_basename('App\Models\User'); // Returns: 'User'
* class_basename(new \App\Models\User); // Returns: 'User'
* class_basename('\Foo\Bar\Baz'); // Returns: 'Baz'
*/
function class_basename(object|string $class): string
{
$class = is_object($class) ? get_class($class) : $class;
return basename(str_replace('\\', '/', $class));
}