<?php
class RecursiveArrayObject extends ArrayObject {
public function getIterator () {
return new RecursiveArrayIterator ($this);
}
}
class myFilter extends FilterIterator {
private $oIt = null;
private $mFilter = null;
public function __construct ($oIt, $mFilter = null) {
parent::__construct ($oIt);
$this -> oIt = $oIt;
$this -> mFilter = $mFilter;
}
public function accept () {
if (is_string ($this -> mFilter) && ($this -> oIt -> current () !== $this -> mFilter)) {
return false;
}
return true;
}
}
abstract class oCore implements IteratorAggregate {
private static $aRef = array ();
private $mFilter = null;
private $aCanBeSet = array (
'FILTER'
);
public function __construct () {
if (array_key_exists ($this -> iOpId, self::$aRef)) {
throw new Exception ('OP '.$this -> iOpId.' has already been instanciated');
}
self::$aRef[$this -> iOpId] = $this -> aMods;
}
public function getIterator () {
if (!is_null ($this -> mFilter)) {
if (is_string ($this -> mFilter)) {
return new myFilter (new RecursiveIteratorIterator (new RecursiveArrayObject(self::$aRef), RecursiveIteratorIterator::SELF_FIRST), $this -> mFilter);
}
if (is_int ($this -> mFilter)) {
if (!isset (self::$aRef[$this -> mFilter])) {
throw new Exception ($this -> mFilter.' is not a valid OP');
}
return new RecursiveIteratorIterator (new RecursiveArrayObject(self::$aRef[$this -> mFilter]), RecursiveIteratorIterator::SELF_FIRST);
}
}
return new RecursiveIteratorIterator (new RecursiveArrayObject(self::$aRef), RecursiveIteratorIterator::SELF_FIRST);
}
public function __destruct() {
self::$aRef[$this -> iOpId] = null;
}
public function __set ($sProp, $mVal) {
if (!in_array ($sProp, $this -> aCanBeSet)) {
throw new Exception ($sProp.' is not a settable property');
}
switch ($sProp) {
case 'FILTER' :
$this -> mFilter = $mVal;
break;
}
}
}
class oSystem extends oCore {
private static $oInstance = null;
public function __construct () {
}
public static function getInstance () {
if (!isset(self::$oInstance)) {
self::$oInstance = new self;
}
return self::$oInstance;
}
public function __destruct () {
self::$oInstance = null;
}
}
class oOp extends oCore {
protected $aMods = array ();
protected $iOpId;
public function __construct ($iOpId, array $aMods) {
$this -> iOpId = $iOpId;
$this -> aMods = $aMods;
parent::__construct ();
}
}
/**
* EXEMPLES
*/
$a = new oOp (5489, array ('mod1','mod2','mod3'));
$b = new oOp (5580, array ('mod2','mod5','mod6'));
$oSys = new oSystem;
echo 'Traverse les OP à la recherche de tous les mods<br />';
foreach ($oSys as $sK => $mV) {
echo $sK, ' => ', $mV, '<br />';
}
echo '<br />Traverse les OP à la recherche d\'un mod<br />';
$oSys -> FILTER = 'mod2';
foreach ($oSys as $sK => $mV) {
echo $sK, ' => ', $mV, '<br />';
}
echo '<br />Cherche les mods d\'1 OP<br />';
$oSys -> FILTER = 5580;
foreach ($oSys as $sK => $mV) {
echo $sK, ' => ', $mV, '<br />';
}
?>