From 5575db87a66ab066a54a61772d8ee202158efc20 Mon Sep 17 00:00:00 2001 From: parsilver Date: Tue, 12 Dec 2023 20:32:51 +0700 Subject: [PATCH 1/3] Add simple helper functions --- composer.json | 7 ++++-- functions.php | 44 +++++++++++++++++++++++++++++++++++++ src/Carbon.php | 10 +++++++++ src/DateTime.php | 10 --------- src/HigherOrderTapProxy.php | 35 +++++++++++++++++++++++++++++ tests/FunctionsTest.php | 23 +++++++++++++++++++ 6 files changed, 117 insertions(+), 12 deletions(-) create mode 100644 functions.php create mode 100755 src/Carbon.php delete mode 100755 src/DateTime.php create mode 100644 src/HigherOrderTapProxy.php create mode 100644 tests/FunctionsTest.php diff --git a/composer.json b/composer.json index 22dc68f..dda99bc 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ ], "require": { "php": "^8.0", - "carbondate/carbon": "^1.33" + "nesbot/carbon": "^2.72" }, "require-dev": { "pestphp/pest": "^1.20", @@ -25,7 +25,10 @@ "autoload": { "psr-4": { "Farzai\\Support\\": "src" - } + }, + "files": [ + "functions.php" + ] }, "autoload-dev": { "psr-4": { diff --git a/functions.php b/functions.php new file mode 100644 index 0000000..89abbfb --- /dev/null +++ b/functions.php @@ -0,0 +1,44 @@ +target = $target; + } + + /** + * Dynamically pass method calls to the target. + * + * @param callable $callback + * @return mixed + */ + public function __invoke($callback) + { + return $callback($this->target); + } +} diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php new file mode 100644 index 0000000..e952c8f --- /dev/null +++ b/tests/FunctionsTest.php @@ -0,0 +1,23 @@ +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'); +}); From a32e8596518c5845153ab329658d76bb950f3503 Mon Sep 17 00:00:00 2001 From: parsilver Date: Tue, 12 Dec 2023 20:48:37 +0700 Subject: [PATCH 2/3] Add HigherOrderTapProxy --- src/HigherOrderTapProxy.php | 9 ++++++--- tests/HigherOrderTapProxyTest.php | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 tests/HigherOrderTapProxyTest.php diff --git a/src/HigherOrderTapProxy.php b/src/HigherOrderTapProxy.php index cab1ec6..6f29b4b 100644 --- a/src/HigherOrderTapProxy.php +++ b/src/HigherOrderTapProxy.php @@ -25,11 +25,14 @@ public function __construct($target) /** * Dynamically pass method calls to the target. * - * @param callable $callback + * @param string $method + * @param array $parameters * @return mixed */ - public function __invoke($callback) + public function __call($method, $parameters) { - return $callback($this->target); + $this->target->{$method}(...$parameters); + + return $this->target; } } diff --git a/tests/HigherOrderTapProxyTest.php b/tests/HigherOrderTapProxyTest.php new file mode 100644 index 0000000..015dfe5 --- /dev/null +++ b/tests/HigherOrderTapProxyTest.php @@ -0,0 +1,20 @@ +called = true; + } + }; + + $proxy = new HigherOrderTapProxy($target); + + $proxy->foo(); + + expect($target->called)->toBeTrue(); +}); \ No newline at end of file From 5b1c5b40792cbe8cd0dd4ffc64e997c812c5da57 Mon Sep 17 00:00:00 2001 From: parsilver Date: Tue, 12 Dec 2023 13:49:02 +0000 Subject: [PATCH 3/3] Fix styling --- tests/HigherOrderTapProxyTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/HigherOrderTapProxyTest.php b/tests/HigherOrderTapProxyTest.php index 015dfe5..6f3d61d 100644 --- a/tests/HigherOrderTapProxyTest.php +++ b/tests/HigherOrderTapProxyTest.php @@ -3,7 +3,8 @@ use Farzai\Support\HigherOrderTapProxy; it('calls the method on the target', function () { - $target = new class { + $target = new class + { public $called = false; public function foo() @@ -17,4 +18,4 @@ public function foo() $proxy->foo(); expect($target->called)->toBeTrue(); -}); \ No newline at end of file +});