Skip to content

Commit

Permalink
If a string is passed as parameter, it will be passed through mb_str_…
Browse files Browse the repository at this point in the history
…split().
  • Loading branch information
drupol committed Sep 9, 2019
1 parent ca60af0 commit b2007be
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
18 changes: 13 additions & 5 deletions spec/drupol/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ public function it_can_be_constructed_from_array(): void
$this
->beConstructedThrough('with', [\range('A', 'E')]);

$this->shouldImplement(Collection::class);

$this
->all()
->shouldReturn(['A', 'B', 'C', 'D', 'E']);
Expand All @@ -176,18 +178,24 @@ public function it_can_be_constructed_with_a_closure(): void
$this->shouldImplement(Collection::class);
}

public function it_can_be_constructed_with_an_array(): void
public function it_can_be_constructed_with_an_arrayObject(): void
{
$this
->beConstructedThrough('with', [['1', '2', '3']]);
->beConstructedThrough('with', [new \ArrayObject([1, 2, 3])]);

$this->shouldImplement(Collection::class);
}

public function it_can_be_constructed_with_an_arrayObject(): void
public function it_can_be_constructed_with_integer(): void
{
$this
->beConstructedThrough('with', [new \ArrayObject([1, 2, 3])]);
->beConstructedThrough('with', [1]);

$this->shouldImplement(Collection::class);

$this
->all()
->shouldReturn([1]);
}

public function it_can_be_instantiated_with_withClosure(): void
Expand Down Expand Up @@ -295,7 +303,7 @@ public function it_can_convert_use_a_string_as_parameter(): void
->beConstructedThrough('with', ['foo']);

$this
->shouldIterateAs(['foo']);
->shouldIterateAs([0 => 'f', 1 => 'o', 2 => 'o']);
}

public function it_can_count_its_items(): void
Expand Down
8 changes: 8 additions & 0 deletions src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public function __construct($data = [])
}
};

break;
case \is_string($data):
$this->source = static function () use ($data) {
foreach (mb_str_split($data) as $key => $value) {
yield $key => $value;
}
};

break;

default:
Expand Down

0 comments on commit b2007be

Please sign in to comment.