Last active
February 16, 2016 10:51
-
-
Save useless-stuff/4b493b123a63953a955b to your computer and use it in GitHub Desktop.
PHP - Iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class AdsTypeCollection | |
*/ | |
class AdsTypeCollection implements Iterator{ | |
const PANORAMA_980x120 = 'PANORAMA_980x120'; | |
const TOP_BANNER = 'TOP_BANNER_930x180'; | |
const NETBOARD = 'NETBOARD_580x400'; | |
const VERTICAL_RECTANGLE = 'VERTICAL_RECTANGLE_240x400'; | |
private $position = 0; | |
private $array = array( | |
self::PANORAMA_980x120, | |
self::TOP_BANNER, | |
self::NETBOARD, | |
self::VERTICAL_RECTANGLE | |
); | |
/** | |
* AdsTypeCollection constructor. | |
*/ | |
public function __construct() { | |
$this->position = 0; | |
} | |
/** | |
* | |
*/ | |
function rewind() { | |
$this->position = 0; | |
} | |
/** | |
* @return mixed | |
*/ | |
function current() { | |
return $this->array[$this->position]; | |
} | |
/** | |
* @return int | |
*/ | |
function key() { | |
return $this->position; | |
} | |
/** | |
* | |
*/ | |
function next() { | |
++$this->position; | |
} | |
/** | |
* @return bool | |
*/ | |
function valid() { | |
return isset($this->array[$this->position]); | |
} | |
} | |
$adsType = new AdsTypeCollection(); | |
while($adsType->valid()){ | |
echo $adsType->current().PHP_EOL; | |
$adsType->next(); | |
} | |
foreach($adsType as $adType){ | |
echo $adsType->current().PHP_EOL; | |
} | |
// Output: | |
/* | |
PANORAMA_980x120 | |
TOP_BANNER_930x180 | |
NETBOARD_580x400 | |
VERTICAL_RECTANGLE_240x400 | |
PANORAMA_980x120 | |
TOP_BANNER_930x180 | |
NETBOARD_580x400 | |
VERTICAL_RECTANGLE_240x400 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment