-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip * Fix styling Co-authored-by: freekmurze <freekmurze@users.noreply.github.com>
- Loading branch information
1 parent
af2d196
commit 9910a85
Showing
7 changed files
with
124 additions
and
6 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
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
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,19 @@ | ||
<?php | ||
|
||
namespace Spatie\CollectionMacros\Macros; | ||
|
||
class IfMacro | ||
{ | ||
public function __invoke() | ||
{ | ||
return function ( | ||
mixed $if, | ||
mixed $then = null, | ||
mixed $else = null, | ||
): mixed { | ||
return value($if, $this) | ||
? value($then, $this) | ||
: value($else, $this); | ||
}; | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
|
||
namespace Spatie\CollectionMacros\Test\Macros; | ||
|
||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Str; | ||
use Spatie\CollectionMacros\Test\TestCase; | ||
|
||
class IfTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_will_return_the_right_branch() | ||
{ | ||
$this->assertTrue(collect()->if(true, then: true, else: false)); | ||
$this->assertFalse(collect()->if(false, then: true, else: false)); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @dataProvider sentences | ||
*/ | ||
public function it_will_pass_the_collection_to_the_branches(string $sentence, string $modifiedSentence) | ||
{ | ||
$collection = collect([$sentence]) | ||
->if( | ||
fn (Collection $collection) => $collection->contains('this is the value'), | ||
then: fn (Collection $collection) => $collection->map(fn (string $item) => strtoupper($item)), | ||
else: fn (Collection $collection) => $collection->map(fn (string $item) => Str::kebab($item)) | ||
); | ||
|
||
$this->assertEquals($modifiedSentence, $collection[0]); | ||
} | ||
|
||
public function sentences(): array | ||
{ | ||
return [ | ||
['this is the value', 'THIS IS THE VALUE'], | ||
['this is another value', 'this-is-another-value'], | ||
]; | ||
} | ||
|
||
/** @test */ | ||
public function the_branches_are_optional() | ||
{ | ||
$result = collect(['this is a value']) | ||
->if( | ||
false, | ||
then: fn (Collection $collection) => 'something', | ||
); | ||
|
||
$this->assertNull($result); | ||
} | ||
} |