Skip to content

Commit

Permalink
Simplify the method of When use.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhichao-poper committed Nov 11, 2020
1 parent 0c5837c commit 0a363e6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 44 deletions.
22 changes: 9 additions & 13 deletions src/When.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,20 @@
class When
{
private $whenCondition;
private $whenValue;
private $successValue;
private $failValue;

/**
* When constructor.
*
* @param $value
* @param mixed ...$args
* @param $whenCondition
* @param null $param
*/
public function __construct($value, ...$args)
public function __construct($whenCondition, $param = null)
{
if ($value instanceof Closure) {
$this->whenCondition = (bool)$value(...$args);
} else {
$this->whenCondition = (bool)$value;
}

$this->whenCondition = (bool)$whenCondition;
$this->whenValue = $param ?? $whenCondition;
$this->successValue = new Meaningless;
$this->failValue = new Meaningless;
}
Expand Down Expand Up @@ -90,15 +87,14 @@ public function fail($value): self
/**
* Send result.
*
* @param bool $handle
* @return mixed
*/
public function result($handle = false)
public function result()
{
$result = $this->whenCondition === true ? $this->successValue : $this->failValue;

if ($handle && $result instanceof Closure) {
return $result();
if ($result instanceof Closure) {
return $result($this->whenValue);
}

return $result;
Expand Down
41 changes: 10 additions & 31 deletions tests/Unit/WhenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,6 @@ class WhenTest extends TestCase
private $successString = 'This is success string';
private $failString = 'This is fail string';

public function test_make_closure()
{
$when = When::make(function () {
return true;
})->success($this->successString);

self::assertEquals($this->successString, $when->result());
}

public function test_make_closure_with_args()
{
$when = When::make(function ($arg1, $arg2) {
return $arg1 && $arg2;
}, true, false)->success($this->successString)->fail($this->failString);

self::assertEquals($this->failString, $when->result());
self::assertNotEquals($this->successString, $when->result());
}

public function test_fail()
{
$when = When::make(false)->success($this->successString)->fail($this->failString);
Expand All @@ -47,22 +28,20 @@ public function test_success()

public function test_success_and_fail_closure()
{
$when = When::make(true)->success(function () {
return $this->successString;
})->fail(function () {
return $this->failString;
$when = When::make('name')->success(function ($value) {
return $value.':success';
})->fail(function ($value) {
return $value.':fail';
});

self::assertEquals($this->successString, $when->result()());
self::assertEquals($this->successString, $when->result(true));
self::assertEquals('name:success', $when->result());

$when = When::make(false)->success(function () {
return $this->successString;
})->fail(function () {
return $this->failString;
$when = When::make(false, 'name')->success(function ($value) {
return $value.':success';
})->fail(function ($value) {
return $value.':fail';
});

self::assertEquals($this->failString, $when->result()());
self::assertEquals($this->failString, $when->result(true));
self::assertEquals('name:fail', $when->result());
}
}

0 comments on commit 0a363e6

Please sign in to comment.