Skip to content

Commit 28bb5c3

Browse files
authored
Merge pull request #20 from wiz-develop/endou-mame/issue19
2 parents 814e79a + c14d880 commit 28bb5c3

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/Collection/ArrayList.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,23 @@ final public function toArray(): array
8383
// NOTE: IArrayListFactory
8484
// -------------------------------------------------------------------------
8585
#[Override]
86-
final public static function from(array $elements): static
86+
final public static function from(iterable $elements): static
8787
{
88-
return new static($elements);
88+
if (is_array($elements)) {
89+
return new static($elements);
90+
}
91+
92+
return new static(iterator_to_array($elements));
8993
}
9094

9195
#[Override]
92-
final public static function tryFrom(array $elements): Result
96+
final public static function tryFrom(iterable $elements): Result
9397
{
98+
$elements = is_array($elements) ? $elements : iterator_to_array($elements);
99+
94100
return static::isValid($elements)
95101
->andThen(static fn () => static::isValidCount($elements))
96-
->andThen(static fn () => Result\ok(static::from($elements)));
102+
->andThen(static fn () => Result\ok(new static($elements)));
97103
}
98104

99105
#[Override]

src/Collection/List/IArrayListFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ interface IArrayListFactory
2020
*
2121
* @template TFromValue of TValue
2222
*
23-
* @param array<int,TFromValue> $elements
23+
* @param iterable<int,TFromValue> $elements
2424
* @return static<TFromValue>
2525
*/
26-
public static function from(array $elements): static;
26+
public static function from(iterable $elements): static;
2727

2828
/**
2929
* 信頼できないプリミティブ値からインスタンスを生成する
3030
*
3131
* @template TTryFromValue of TValue
3232
*
33-
* @param array<int,TTryFromValue> $elements
33+
* @param iterable<int,TTryFromValue> $elements
3434
* @return Result<static<TTryFromValue>,ValueObjectError>
3535
*/
36-
public static function tryFrom(array $elements): Result;
36+
public static function tryFrom(iterable $elements): Result;
3737

3838
/**
3939
* 空のコレクションを作成する

0 commit comments

Comments
 (0)