Skip to content

Commit

Permalink
refactor: Update Explode operation.
Browse files Browse the repository at this point in the history
Mimic the PHP core function "explode" by remove the value used to
explode the collection.

BREAKING CHANGE: yes
  • Loading branch information
drupol committed Sep 21, 2020
1 parent 1c669b2 commit d28abcd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 50 deletions.
41 changes: 16 additions & 25 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,25 +619,23 @@ public function it_can_explode(): void
15 => 'd',
],
1 => [
0 => 'o',
1 => 'm',
2 => ' ',
3 => 'p',
4 => 'i',
5 => 'e',
6 => 'c',
7 => 'e',
8 => ' ',
0 => 'm',
1 => ' ',
2 => 'p',
3 => 'i',
4 => 'e',
5 => 'c',
6 => 'e',
7 => ' ',
],
2 => [
0 => 'o',
1 => 'f',
2 => ' ',
3 => 't',
4 => 'e',
5 => 'x',
6 => 't',
7 => '.',
0 => 'f',
1 => ' ',
2 => 't',
3 => 'e',
4 => 'x',
5 => 't',
6 => '.',
],
]
);
Expand Down Expand Up @@ -2431,15 +2429,8 @@ public function it_can_use_with(): void
};

$this::with($stream)
->split(static function ($v): bool {
return "\n" === $v;
})
->explode("\n")
->last()
->map(static function (array $value): array {
array_shift($value);

return $value;
})
->unwrap()
->implode()
->shouldReturn('indent_size = 4');
Expand Down
46 changes: 21 additions & 25 deletions src/Operation/Explode.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Closure;
use Generator;
use Iterator;
use loophp\collection\Contract\Operation\Splitable;

/**
* @psalm-template TKey
Expand All @@ -27,33 +28,28 @@ public function __invoke(): Closure
* @psalm-return Closure(Iterator<TKey, T>): Generator<int, list<T>>
*/
static function (...$explodes): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int, list<T>>
*/
static function (Iterator $iterator) use ($explodes): Generator {
return yield from Split::of()(
...array_map(
/** @psalm-var Closure(Iterator<TKey, T>): Generator<int, list<T>> $split */
$split = Split::of()(Splitable::REMOVE)(
...array_map(
/**
* @param mixed $explode
* @psalm-param T $explode
*/
static function ($explode): Closure {
return
/**
* @param mixed $explode
* @psalm-param T $explode
* @param mixed $value
* @psalm-param T $value
*/
static function ($explode): Closure {
return
/**
* @param mixed $value
* @psalm-param T $value
*/
static function ($value) use ($explode): bool {
return $value === $explode;
};
},
$explodes
)
)($iterator);
};
static function ($value) use ($explode): bool {
return $value === $explode;
};
},
$explodes
)
);

return $split;
};
}
}

0 comments on commit d28abcd

Please sign in to comment.