hi,
es ist ja eigentlich ein shuffler, oder? der memorized, und zwar serialisiert. also eigentlich ein SerializedShuffleMemorizer.
<?php
/**
* Saves List in serialized File an picks new Item every call until End, then shuffles new.
*/
class MemorizedShuffle
{
private $_serFileName = "";
private $_toShuffle = "";
private $_shuffled = array();
private $_nextItem = "";
/**
* Constructor
*@param string filename, array List to Shuffle
* @return void
*/
public function __construct($serFileName, $toShuffle) {
$this->_serFileName = $serFileName;
$this->_toShuffle = $toShuffle;
if(!file_exists($this->_serFileName)) {
$this->_createNewShuffledFile();
}
$this->_read();
}
private function _createNewShuffledFile() {
$this->_shuffled = $this->_toShuffle;
shuffle($this->_shuffled);
$this->_write();
}
private function _write() {
if (false === file_put_contents($this->_serFileName, serialize($this->_shuffled))) {
throw new Exception ("Could not write");
};
}
private function _read() {
$this->_shuffled = unserialize(file_get_contents($this->_serFileName));
if (false === $this->_shuffled) {
throw new Exception ("Could not read");
}
}
public function getNext() {
//~ var_dump($this->_shuffled);
if (count($this->_shuffled) === 0) {
$this->_createNewShuffledFile();
}
$actualItem = array_shift($this->_shuffled);
$this->_write();
return $actualItem;
}
};
$myShuffler = new MemorizedShuffle("ColorsShuffled.ser", array("blau", "rot", "grün", "schwarz"));
var_dump($myShuffler->getNext());
mfg
tami