Skip to content

Commit

Permalink
Merge pull request #6 from farzai/add-helper-functions
Browse files Browse the repository at this point in the history
Add simple helper functions
  • Loading branch information
parsilver authored Dec 12, 2023
2 parents c5fe644 + 5b1c5b4 commit 3113880
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 12 deletions.
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"require": {
"php": "^8.0",
"carbondate/carbon": "^1.33"
"nesbot/carbon": "^2.72"
},
"require-dev": {
"pestphp/pest": "^1.20",
Expand All @@ -25,7 +25,10 @@
"autoload": {
"psr-4": {
"Farzai\\Support\\": "src"
}
},
"files": [
"functions.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
44 changes: 44 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Farzai\Support;

/**
* Call the given Closure with the given value then return the value.
*
* @param mixed $value
* @param callable|null $callback
* @return mixed
*/
function tap($value, $callback = null)
{
if (is_null($callback)) {
return new HigherOrderTapProxy($value);
}

$callback($value);

return $value;
}

/**
* Get current date time.
*
* @param string|null $timezone
*/
function now($timezone = null)
{
return Carbon::now($timezone);
}

/**
* Get the class "basename" of the given object / class.
*
* @param string|object $class
* @return string
*/
function class_basename($class)
{
$class = is_object($class) ? get_class($class) : $class;

return basename(str_replace('\\', '/', $class));
}
10 changes: 10 additions & 0 deletions src/Carbon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Farzai\Support;

use Carbon\Carbon as BaseCarbon;

class Carbon extends BaseCarbon
{
//
}
10 changes: 0 additions & 10 deletions src/DateTime.php

This file was deleted.

38 changes: 38 additions & 0 deletions src/HigherOrderTapProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Farzai\Support;

class HigherOrderTapProxy
{
/**
* The target being tapped.
*
* @var mixed
*/
protected $target;

/**
* Create a new tap proxy instance.
*
* @param mixed $target
* @return void
*/
public function __construct($target)
{
$this->target = $target;
}

/**
* Dynamically pass method calls to the target.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
$this->target->{$method}(...$parameters);

return $this->target;
}
}
23 changes: 23 additions & 0 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use function Farzai\Support\class_basename;
use function Farzai\Support\now;
use function Farzai\Support\tap;

it('can tap value', function () {
$value = tap('foo', function ($value) {
expect($value)->toBe('foo');
});

expect($value)->toBe('foo');
});

it('can get current date time', function () {
$now = now();

expect($now)->toBeInstanceOf(\DateTimeInterface::class);
});

it('can get class basename', function () {
expect(class_basename('Farzai\Support\Str'))->toBe('Str');
});
21 changes: 21 additions & 0 deletions tests/HigherOrderTapProxyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Farzai\Support\HigherOrderTapProxy;

it('calls the method on the target', function () {
$target = new class
{
public $called = false;

public function foo()
{
$this->called = true;
}
};

$proxy = new HigherOrderTapProxy($target);

$proxy->foo();

expect($target->called)->toBeTrue();
});

0 comments on commit 3113880

Please sign in to comment.