From 89c382df0bb2c91c606c948f5d712ff051bc91f2 Mon Sep 17 00:00:00 2001 From: Loris Leiva Date: Sun, 1 Mar 2020 12:41:31 +0000 Subject: [PATCH 1/3] Add make and __callStatic methods --- src/Action.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Action.php b/src/Action.php index ea8a296..37d3a6e 100644 --- a/src/Action.php +++ b/src/Action.php @@ -36,6 +36,11 @@ public function __construct(array $attributes = []) } } + public static function make(array $attributes = []) + { + return new static($attributes); + } + public static function createFrom(Action $action) { return (new static)->fill($action->all()); @@ -123,4 +128,9 @@ public function __call($method, $parameters) 'Method %s::%s does not exist.', static::class, $method )); } + + public static function __callStatic($method, $parameters) + { + return (new static)->$method(...$parameters); + } } From 9948d66fd539b1ed8fd63fbd883869bfff57a9c7 Mon Sep 17 00:00:00 2001 From: Loris Leiva Date: Sun, 1 Mar 2020 12:58:57 +0000 Subject: [PATCH 2/3] Add tests and make it work --- src/Action.php | 10 +++++++++- tests/ActingAsTest.php | 2 +- tests/RunsAsObjectsTest.php | 24 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Action.php b/src/Action.php index 37d3a6e..61a20a5 100644 --- a/src/Action.php +++ b/src/Action.php @@ -10,6 +10,10 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Foundation\Bus\Dispatchable; +/** + * @method mixed run(array $attributes = []) + * @method static mixed run(array $attributes = []) + */ abstract class Action { use Dispatchable; @@ -63,7 +67,7 @@ public function runAs(Action $action) return $this->run(); } - public function run(array $attributes = []) + protected function handleRun(array $attributes = []) { $this->fill($attributes); $this->prepareForValidation(); @@ -124,6 +128,10 @@ public function __invoke(array $attributes = []) public function __call($method, $parameters) { + if ($method === 'run') { + return $this->handleRun(...$parameters); + } + throw new BadMethodCallException(sprintf( 'Method %s::%s does not exist.', static::class, $method )); diff --git a/tests/ActingAsTest.php b/tests/ActingAsTest.php index 02c707b..470a44c 100644 --- a/tests/ActingAsTest.php +++ b/tests/ActingAsTest.php @@ -56,4 +56,4 @@ public function it_takes_the_user_from_the_request_when_ran_as_controller() $this->assertEquals('Alice From Request', $action->user()->name); } -} \ No newline at end of file +} diff --git a/tests/RunsAsObjectsTest.php b/tests/RunsAsObjectsTest.php index 43f05a9..2dd3038 100644 --- a/tests/RunsAsObjectsTest.php +++ b/tests/RunsAsObjectsTest.php @@ -134,6 +134,30 @@ public function it_can_run_like_an_invokable_object() $this->assertEquals(8, $response); } + /** @test */ + public function it_can_be_instantiated_statically() + { + $response = SimpleCalculator::make([ + 'operation' => 'addition', + 'left' => 3, + 'right' => 5, + ])->run(); + + $this->assertEquals(8, $response); + } + + /** @test */ + public function it_can_run_through_the_run_static_method() + { + $response = SimpleCalculator::run([ + 'operation' => 'addition', + 'left' => 3, + 'right' => 5, + ]); + + $this->assertEquals(8, $response); + } + /** @test */ public function it_can_override_all_attributes_with_a_given_array() { From 484d13f77bfafe0954fe90c467387c5763836956 Mon Sep 17 00:00:00 2001 From: Loris Leiva Date: Sun, 1 Mar 2020 13:06:30 +0000 Subject: [PATCH 3/3] Document new static helpers --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 206710c..4f350c9 100644 --- a/README.md +++ b/README.md @@ -319,13 +319,14 @@ Note that the `run` method also accepts additional attributes to be merged. ]); ``` -Alternatively, you can run a action like a function call. +Alternatively, you can run an action like a function call or with a simple static call. ```php -(new PublishANewArticle)([ - 'title' => 'My blog post', - 'body' => 'Lorem ipsum.', -]); +// Actions are invokable objects. +(new PublishANewArticle)([/* ... */]); + +// You can also use the `run` method statically. +PublishANewArticle::run([/* ... */]); ``` ## Actions as jobs