Skip to content

Commit

Permalink
Merge pull request #36 from lorisleiva/add-static-helpers
Browse files Browse the repository at this point in the history
Add static helpers
  • Loading branch information
lorisleiva authored Mar 1, 2020
2 parents 96c2dd2 + 484d13f commit 1cf1b60
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion src/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +40,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());
Expand All @@ -58,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();
Expand Down Expand Up @@ -119,8 +128,17 @@ 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
));
}

public static function __callStatic($method, $parameters)
{
return (new static)->$method(...$parameters);
}
}
2 changes: 1 addition & 1 deletion tests/ActingAsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
24 changes: 24 additions & 0 deletions tests/RunsAsObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 1cf1b60

Please sign in to comment.