Skip to content

Commit 3113880

Browse files
authored
Merge pull request #6 from farzai/add-helper-functions
Add simple helper functions
2 parents c5fe644 + 5b1c5b4 commit 3113880

File tree

7 files changed

+141
-12
lines changed

7 files changed

+141
-12
lines changed

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
],
1616
"require": {
1717
"php": "^8.0",
18-
"carbondate/carbon": "^1.33"
18+
"nesbot/carbon": "^2.72"
1919
},
2020
"require-dev": {
2121
"pestphp/pest": "^1.20",
@@ -25,7 +25,10 @@
2525
"autoload": {
2626
"psr-4": {
2727
"Farzai\\Support\\": "src"
28-
}
28+
},
29+
"files": [
30+
"functions.php"
31+
]
2932
},
3033
"autoload-dev": {
3134
"psr-4": {

functions.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Farzai\Support;
4+
5+
/**
6+
* Call the given Closure with the given value then return the value.
7+
*
8+
* @param mixed $value
9+
* @param callable|null $callback
10+
* @return mixed
11+
*/
12+
function tap($value, $callback = null)
13+
{
14+
if (is_null($callback)) {
15+
return new HigherOrderTapProxy($value);
16+
}
17+
18+
$callback($value);
19+
20+
return $value;
21+
}
22+
23+
/**
24+
* Get current date time.
25+
*
26+
* @param string|null $timezone
27+
*/
28+
function now($timezone = null)
29+
{
30+
return Carbon::now($timezone);
31+
}
32+
33+
/**
34+
* Get the class "basename" of the given object / class.
35+
*
36+
* @param string|object $class
37+
* @return string
38+
*/
39+
function class_basename($class)
40+
{
41+
$class = is_object($class) ? get_class($class) : $class;
42+
43+
return basename(str_replace('\\', '/', $class));
44+
}

src/Carbon.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Farzai\Support;
4+
5+
use Carbon\Carbon as BaseCarbon;
6+
7+
class Carbon extends BaseCarbon
8+
{
9+
//
10+
}

src/DateTime.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/HigherOrderTapProxy.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Farzai\Support;
4+
5+
class HigherOrderTapProxy
6+
{
7+
/**
8+
* The target being tapped.
9+
*
10+
* @var mixed
11+
*/
12+
protected $target;
13+
14+
/**
15+
* Create a new tap proxy instance.
16+
*
17+
* @param mixed $target
18+
* @return void
19+
*/
20+
public function __construct($target)
21+
{
22+
$this->target = $target;
23+
}
24+
25+
/**
26+
* Dynamically pass method calls to the target.
27+
*
28+
* @param string $method
29+
* @param array $parameters
30+
* @return mixed
31+
*/
32+
public function __call($method, $parameters)
33+
{
34+
$this->target->{$method}(...$parameters);
35+
36+
return $this->target;
37+
}
38+
}

tests/FunctionsTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use function Farzai\Support\class_basename;
4+
use function Farzai\Support\now;
5+
use function Farzai\Support\tap;
6+
7+
it('can tap value', function () {
8+
$value = tap('foo', function ($value) {
9+
expect($value)->toBe('foo');
10+
});
11+
12+
expect($value)->toBe('foo');
13+
});
14+
15+
it('can get current date time', function () {
16+
$now = now();
17+
18+
expect($now)->toBeInstanceOf(\DateTimeInterface::class);
19+
});
20+
21+
it('can get class basename', function () {
22+
expect(class_basename('Farzai\Support\Str'))->toBe('Str');
23+
});

tests/HigherOrderTapProxyTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Farzai\Support\HigherOrderTapProxy;
4+
5+
it('calls the method on the target', function () {
6+
$target = new class
7+
{
8+
public $called = false;
9+
10+
public function foo()
11+
{
12+
$this->called = true;
13+
}
14+
};
15+
16+
$proxy = new HigherOrderTapProxy($target);
17+
18+
$proxy->foo();
19+
20+
expect($target->called)->toBeTrue();
21+
});

0 commit comments

Comments
 (0)