Skip to content

Commit

Permalink
Use ::with() method everywhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 19, 2019
1 parent 534f374 commit 748a618
Show file tree
Hide file tree
Showing 24 changed files with 59 additions and 59 deletions.
14 changes: 7 additions & 7 deletions spec/drupol/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function it_can_apply(): void
return $item;
};

$this::withArray(\range(1, 20))
$this::with(\range(1, 20))
->apply($applyCallback1)
->walk($walkCallback2)
->filter(static function ($item) {
Expand All @@ -113,7 +113,7 @@ public function it_can_apply(): void
public function it_can_be_constructed_from_array(): void
{
$this
->beConstructedThrough('withArray', [\range('A', 'E')]);
->beConstructedThrough('with', [\range('A', 'E')]);

$this
->all()
Expand Down Expand Up @@ -142,7 +142,7 @@ public function it_can_be_constructed_with_a_closure(): void
public function it_can_be_constructed_with_an_array(): void
{
$this
->beConstructedThrough('withArray', [['1', '2', '3']]);
->beConstructedThrough('with', [['1', '2', '3']]);
$this->shouldImplement(CollectionInterface::class);
}

Expand All @@ -167,7 +167,7 @@ public function it_can_be_instantiated_with_withClosure(): void
};

$this
->beConstructedThrough('withClosure', [$fibonacci]);
->beConstructedThrough('with', [$fibonacci]);

$this
->limit(10)
Expand Down Expand Up @@ -544,7 +544,7 @@ public function it_can_pluck(): void
'bar' => 2,
],
],
Collection::withArray(
Collection::with(
[
'foo' => [
'bar' => 3,
Expand Down Expand Up @@ -769,7 +769,7 @@ public function it_can_walk(): void
->all()
->shouldReturn(['A' => 'AA', 'B' => 'BB', 'C' => 'CC', 'D' => 'DD', 'E' => 'EE']);

$this::withArray(\range(1, 10))
$this::with(\range(1, 10))
->walk(static function ($item) {
return $item * 2;
}, static function ($item) {
Expand All @@ -778,7 +778,7 @@ public function it_can_walk(): void
->all()
->shouldReturn(\range(3, 21, 2));

$this::withArray(\range(1, 10))
$this::with(\range(1, 10))
->walk(static function ($item) {
return $item;
}, static function ($item) {
Expand Down
56 changes: 28 additions & 28 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,34 +369,6 @@ public static function with($data = []): CollectionInterface
return self::withArray(self::getArrayableItems($data));
}

/**
* @param array $data
*
* @return \drupol\collection\Contract\Collection
*/
public static function withArray(array $data): CollectionInterface
{
return self::withClosure(static function () use ($data) {
yield from $data;
});
}

/**
* Create a new collection instance.
*
* @param callable $callable
*
* @return \drupol\collection\Contract\Collection
*/
public static function withClosure(callable $callable): CollectionInterface
{
$instance = new static();

$instance->source = $callable;

return $instance;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -456,4 +428,32 @@ private function makeIterator($source): \Iterator

return new ArrayIterator((array) $source);
}

/**
* @param array $data
*
* @return \drupol\collection\Contract\Collection
*/
private static function withArray(array $data): CollectionInterface
{
return self::withClosure(static function () use ($data) {
yield from $data;
});
}

/**
* Create a new collection instance.
*
* @param callable $callable
*
* @return \drupol\collection\Contract\Collection
*/
private static function withClosure(callable $callable): CollectionInterface
{
$instance = new static();

$instance->source = $callable;

return $instance;
}
}
2 changes: 1 addition & 1 deletion src/Operation/Append.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function run(CollectionInterface $collection): CollectionInterface
{
$items = $this->parameters;

return Collection::withClosure(
return Collection::with(
static function () use ($items, $collection) {
foreach ($collection as $item) {
yield $item;
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function run(CollectionInterface $collection): CollectionInterface
return Collection::with();
}

return Collection::withClosure(
return Collection::with(
static function () use ($size, $collection) {
$iterator = $collection->getIterator();

Expand All @@ -34,7 +34,7 @@ static function () use ($size, $collection) {
$values[$iterator->key()] = $iterator->current();
}

yield Collection::withArray($values);
yield Collection::with($values);
}
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Collapse.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class Collapse extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
return Collection::withClosure(
return Collection::with(
static function () use ($collection) {
foreach ($collection as $item) {
if (\is_array($item) || $item instanceof CollectionInterface) {
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Combine.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function run(CollectionInterface $collection): CollectionInterface
{
$keys = $this->parameters;

return Collection::withClosure(
return Collection::with(
static function () use ($keys, $collection) {
$original = $collection->getIterator();
$keysIterator = Collection::with($keys)->getIterator();
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function run(CollectionInterface $collection): CollectionInterface
};
}

return Collection::withClosure(
return Collection::with(
static function () use ($callback, $collection) {
foreach ($collection->getIterator() as $key => $value) {
if (true === (bool) $callback($value, $key)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Flatten.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function run(CollectionInterface $collection): CollectionInterface
{
$depth = $this->parameters[0];

return Collection::withClosure(
return Collection::with(
static function () use ($depth, $collection) {
$iterator = $collection->getIterator();

Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Flip.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class Flip extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
return Collection::withClosure(
return Collection::with(
static function () use ($collection) {
foreach ($collection as $key => $value) {
yield $value => $key;
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Forget.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function run(CollectionInterface $collection): CollectionInterface
{
$keys = $this->parameters;

return Collection::withClosure(
return Collection::with(
static function () use ($keys, $collection) {
$keys = \array_flip($keys);

Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Keys.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class Keys extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
return Collection::withClosure(
return Collection::with(
static function () use ($collection) {
foreach ($collection as $key => $value) {
yield $key;
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function run(CollectionInterface $collection): CollectionInterface
{
$limit = $this->parameters[0];

return Collection::withClosure(
return Collection::with(
static function () use ($limit, $collection) {
$iterator = $collection->getIterator();

Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function run(CollectionInterface $collection): CollectionInterface
{
$sources = $this->parameters;

return Collection::withClosure(
return Collection::with(
static function () use ($sources, $collection) {
foreach ($collection as $item) {
yield $item;
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Normalize.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class Normalize extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
return Collection::withClosure(
return Collection::with(
static function () use ($collection) {
foreach ($collection as $item) {
yield $item;
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Nth.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function run(CollectionInterface $collection): CollectionInterface
{
[$step, $offset] = $this->parameters;

return Collection::withClosure(
return Collection::with(
static function () use ($step, $offset, $collection) {
$position = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Only.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function run(CollectionInterface $collection): CollectionInterface
{
$keys = $this->parameters;

return Collection::withClosure(
return Collection::with(
static function () use ($keys, $collection) {
if ([] === $keys) {
yield from $collection;
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Pad.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function run(CollectionInterface $collection): CollectionInterface
{
[$size, $value] = $this->parameters;

return Collection::withClosure(
return Collection::with(
static function () use ($size, $value, $collection) {
$y = 0;

Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Pluck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function run(CollectionInterface $collection): CollectionInterface
[$key, $default] = $this->parameters;
$operation = $this;

return Collection::withClosure(
return Collection::with(
static function () use ($key, $default, $collection, $operation) {
$key = \is_string($key) ? \explode('.', \trim($key, '.')) : $key;

Expand Down Expand Up @@ -57,7 +57,7 @@ private function pick($target, array $key, $default = null)
$result[] = $this->pick($item, $key);
}

return \in_array('*', $key, true) ? Collection::withArray($result)->collapse() : $result;
return \in_array('*', $key, true) ? Collection::with($result)->collapse() : $result;
}

if ((true === \is_array($target)) && (true === \array_key_exists($segment, $target))) {
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Prepend.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function run(CollectionInterface $collection): CollectionInterface
{
$items = $this->parameters;

return Collection::withClosure(
return Collection::with(
static function () use ($items, $collection) {
foreach ($items as $item) {
yield $item;
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function run(CollectionInterface $collection): CollectionInterface
{
[$start, $end, $step] = $this->parameters;

return Collection::withClosure(
return Collection::with(
static function () use ($start, $end, $step) {
for ($current = $start; $current < $end; $current += $step) {
yield $current;
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Skip.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function run(CollectionInterface $collection): CollectionInterface
{
$counts = $this->parameters;

return Collection::withClosure(
return Collection::with(
static function () use ($counts, $collection) {
$iterator = $collection->getIterator();
$counts = \array_sum($counts);
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Slice.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function run(CollectionInterface $collection): CollectionInterface
{
[$offset, $length] = $this->parameters;

return Collection::withClosure(
return Collection::with(
static function () use ($offset, $length, $collection) {
if (null === $length) {
yield from $collection->skip($offset);
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Walk.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function run(CollectionInterface $collection): CollectionInterface
{
$callbacks = $this->parameters;

return Collection::withClosure(
return Collection::with(
static function () use ($callbacks, $collection) {
$callback = static function ($carry, $callback) {
return $callback($carry);
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function run(CollectionInterface $collection): CollectionInterface
{
$iterables = $this->parameters;

return Collection::withClosure(
return Collection::with(
static function () use ($iterables, $collection) {
$iterators =
Collection::empty()
Expand Down

0 comments on commit 748a618

Please sign in to comment.