/** * {@inheritdoc} */ public function next() { if ($this->currentPosition == -1) { throw new \LogicException('Cannot fetch an item from empty bag.'); } if ($this->currentPosition == 0) { $this->currentPosition = $this->items->getSize() - 1; return $this->items[0]; } $maxOffset = $this->currentPosition; $randomOffset = (int) ($this->generator->next() * $maxOffset); $item = $this->items[$randomOffset]; if ($randomOffset < $maxOffset) { $this->items[$randomOffset] = $this->items[$this->currentPosition]; $this->items[$this->currentPosition] = $item; } $this->currentPosition--; return $item; }
/** * Get the next item from bag. * * return scalar */ public function next() { $maxOffset = $this->storage->getCurrentPosition(); if ($maxOffset == Storage::EMPTY_BAG_STORAGE) { throw new \LogicException('Cannot fetch an item from empty bag.'); } if ($maxOffset == 0) { $item = $this->storage->getItem(0); $size = $this->storage->getItemCount(); $this->storage->setCurrentPosition($size - 1); return $item; } $multiplier = $this->numberGenerator->next(); $offset = (int) ($multiplier * $maxOffset); $item = $this->storage->getItem($offset); if ($offset < $maxOffset) { $this->storage->swapItems($offset, $maxOffset); } $this->storage->setCurrentPosition($maxOffset - 1); return $item; }