Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Last active January 18, 2020 12:56
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save JeffreyWay/64bc405f0389bc18414d65be8c55c25d to your computer and use it in GitHub Desktop.
Save JeffreyWay/64bc405f0389bc18414d65be8c55c25d to your computer and use it in GitHub Desktop.
Laracasts Iterator Lesson Examples: https://laracasts.com/series/how-do-i/episodes/3
<?php
class Collection implements IteratorAggregate
{
/**
* The collection contents.
*
* @var array
*/
protected $items;
/**
* Create a new counter instance.
*
* @param array $items
*/
public function __construct($items = [])
{
$this->items = $items;
}
/**
* Retrieve the iterator.
*
* @return ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->items);
}
}
// Usage:
$collection = new Collection(['a', 'b', 'c']);
foreach ($collection as $item) {
var_dump($item);
}
<?php
class Collection implements Iterator
{
/**
* The collection contents.
*
* @var array
*/
protected $items;
/**
* Create a new counter instance.
*
* @param array $items
*/
public function __construct($items = [])
{
$this->items = $items;
}
/**
* Fetch the current item.
*
* @return mixed
*/
public function current()
{
return current($this->items);
}
/**
* Get the key for the current item.
*
* @return mixed
*/
public function key()
{
return key($this->items);
}
/**
* Move the pointer to the next item.
*
* @return mixed
*/
public function next()
{
return next($this->items);
}
/**
* Rewind the pointer to the first item.
*
* @return integer
*/
public function rewind()
{
return reset($this->items);
}
/**
* Determine if there are more items to iterate over.
*
* @return boolean
*/
public function valid()
{
return current($this->items);
}
}
// Usage:
$collection = new Collection(['a', 'b', 'c']);
foreach ($collection as $item) {
var_dump($item);
}
<?php
class Counter implements Iterator
{
/**
* The initial count.
*
* @var integer
*/
protected $start;
/**
* The count to end at.
*
* @var integer
*/
protected $end;
/**
* The current count.
*
* @var integer
*/
protected $current;
/**
* Create a new counter instance.
*
* @param integer $start
* @param integer $end
*/
public function __construct($start, $end)
{
$this->start = $start;
$this->end = $end;
$this->current = $start;
}
/**
* Prepare a new counter.
*
* @param integer $start
* @param integer $end
* @return static
*/
public static function start($start, $end)
{
return new static($start, $end);
}
/**
* Fetch the current count.
*
* @return integer
*/
public function current()
{
return $this->current;
}
/**
* Get the key for the current item.
*
* @return integer
*/
public function key()
{
return $this->current;
}
/**
* Move the pointer to the next item.
*
* @return integer
*/
public function next()
{
return $this->current++;
}
/**
* Rewind the pointer to the first item.
*
* @return integer
*/
public function rewind()
{
return $this->current = $this->start;
}
/**
* Determine if there are more items to iterate over.
*
* @return boolean
*/
public function valid()
{
return $this->current <= $this->end;
}
}
// Usage:
// Or: range(1, 10) :)
foreach (Counter::start(1, 10) as $number) {
var_dump($number);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment