/** * collects all elements into a structure defined by given collector * * This is a terminal operation. * * In case no collector is provided an instance of \stubbles\sequence\Collectors * will be returned which provides convenience methods for some common * collection operations. * * @param \stubbles\sequence\Collector $collector optional * @return mixed|\stubbles\sequence\Collectors */ public function collect(Collector $collector = null) { if (null === $collector) { return new Collectors($this); } foreach ($this->elements as $key => $element) { $collector->accumulate($element, $key); } return $collector->finish(); }