generated from farzai/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from farzai/add-helper-functions
Add simple helper functions
- Loading branch information
Showing
7 changed files
with
141 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
// | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |