/** * Takes a Seq of nested CollectionInterface, SplFixedArray, ArrayObject, ArrayIterator or array, and flattens * it to a new Sequence * Note: This is a slight deviation from the CollectionInterface spec, as flatten should always receive the * the same type as itself. However this has been relaxed in this case, as we know that Seq is * can always hold more than one value. Therefor converting single collections into a sequence is * safe. As arrays are simply unwrapped Seq, we can also safely accept them for flattening. * The outcome will always be the expected result, even with the relaxation of the rules: * A de-nested Seq. * We do not, however, know if a traversable will ever terminate, so we will not attempt to flatten those. * If you have a nested sequence of transversable, iterate over them with ->flatMap using unary('iterator_to_array'). * @return Seq */ public function flatten() { $output = []; foreach (I($this->hash) as $value) { $array = __PRIVATE__::getArrayFrom($value); $output[] = $array !== null ? $array : __CONTRACT__::contentIsA(FoldableInterface::class, $value); } return static::from(call_user_func_array('array_merge', $output)); }
/** * @covers ::getArrayFrom * @dataProvider getArrayFromProvider */ public function test_getArrayFrom($container, $expected) { self::assertSame($expected, internal::getArrayFrom($container)); }