phpstreams\Collectors::joining PHP Method

joining() public static method

Get a collector that concatenates all the elements in the stream.
public static joining ( string $delimiter = "" ) : phpstreams\Collector
$delimiter string [optional] A delimiter to insert between elements. Defaults to the empty string.
return phpstreams\Collector
    public static function joining($delimiter = "")
    {
        $first = true;
        return Collectors::reducing("", function ($current, $element) use(&$first, $delimiter) {
            if (!$first) {
                $current .= $delimiter;
            } else {
                $first = false;
            }
            return $current . $element;
        });
    }

Usage Example

Example #1
0
 public function testJoiningWithDelimiter()
 {
     $instance = Collectors::joining(",");
     $instance->add("a", "b");
     $instance->add("foo", "bar");
     $instance->add(1, 2);
     $this->assertEquals("b,bar,2", $instance->get());
 }