Last active
February 12, 2016 11:08
-
-
Save useless-stuff/f3f49f870442d45f2874 to your computer and use it in GitHub Desktop.
PHP - SeekableIterator
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 ObjectPicker | |
*/ | |
class ObjectPicker implements SeekableIterator{ | |
protected $data = array(); | |
protected $position = 0; | |
/** | |
* CompanySkills constructor. | |
* @param array $data | |
*/ | |
public function __construct(array $data) | |
{ | |
$this->data = $data; | |
} | |
/** | |
* Return the current element | |
* @link http://php.net/manual/en/iterator.current.php | |
* @return mixed Can return any type. | |
* @since 5.0.0 | |
*/ | |
public function current() | |
{ | |
return $this->data[$this->position]; | |
} | |
/** | |
* Move forward to next element | |
* @link http://php.net/manual/en/iterator.next.php | |
* @return void Any returned value is ignored. | |
* @since 5.0.0 | |
*/ | |
public function next() | |
{ | |
++$this->position; | |
} | |
/** | |
* Return the key of the current element | |
* @link http://php.net/manual/en/iterator.key.php | |
* @return mixed scalar on success, or null on failure. | |
* @since 5.0.0 | |
*/ | |
public function key() | |
{ | |
return $this->position; | |
} | |
/** | |
* Checks if current position is valid | |
* @link http://php.net/manual/en/iterator.valid.php | |
* @return boolean The return value will be casted to boolean and then evaluated. | |
* Returns true on success or false on failure. | |
* @since 5.0.0 | |
*/ | |
public function valid() | |
{ | |
return isset($this->data[$this->position]); | |
} | |
/** | |
* Rewind the Iterator to the first element | |
* @link http://php.net/manual/en/iterator.rewind.php | |
* @return void Any returned value is ignored. | |
* @since 5.0.0 | |
*/ | |
public function rewind() | |
{ | |
$this->position = 0; | |
} | |
/** | |
* @param int $position | |
* @throws RuntimeException§ | |
*/ | |
public function seek($position) | |
{ | |
if(is_int($position)){ | |
$this->position = $position; | |
if(!$this->valid()){ | |
throw new \OutOfBoundsException('Required position "'.$position.'" is not available '.__METHOD__); | |
} | |
}else{ | |
throw new \RuntimeException('Position "'.$position.'" must to be integer - '.__METHOD__); | |
} | |
} | |
} | |
// Generate fake data | |
$objects = array(); | |
for($i=1;$i<11;$i++) | |
{ | |
$obj = new stdClass(); | |
$obj->name = 'obj_'.$i; | |
$objects[] = $obj; | |
} | |
// Normal | |
$objectPicker = new ObjectPicker($objects); | |
foreach($objectPicker as $object){ | |
print_r($object); | |
} | |
/* | |
* output: | |
[ | |
{ | |
name: "obj_1" | |
}, | |
{ | |
name: "obj_2" | |
}, | |
{ | |
name: "obj_3" | |
}, | |
{ | |
name: "obj_4" | |
}, | |
{ | |
name: "obj_5" | |
}, | |
{ | |
name: "obj_6" | |
}, | |
{ | |
name: "obj_7" | |
}, | |
{ | |
name: "obj_8" | |
}, | |
{ | |
name: "obj_9" | |
}, | |
{ | |
name: "obj_10" | |
} | |
] | |
*/ | |
// Seek | |
$objectPicker->seek(5); | |
while($objectPicker->valid()){ | |
print_r($objectPicker->current()); | |
$objectPicker->next(); | |
} | |
/* | |
[ | |
{ | |
name: "obj_6" | |
}, | |
{ | |
name: "obj_7" | |
}, | |
{ | |
name: "obj_8" | |
}, | |
{ | |
name: "obj_9" | |
}, | |
{ | |
name: "obj_10" | |
} | |
] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment