Dans le cadre de ma croisade pour faire connaitre les itérateurs, voici un package utilisant l'idée de FhX dans son package de gestion des modules :
http://www.phpcs.com/codes/PHP5-GESTION-MODULES-OBJET_40315.aspx
J'ai utilisé le même squelette, en ajoutant quelques niveaux, en transformant les modules en utilisateurs, et en ajoutant des possibilités offertes par les itérateurs.
Mon idée de base était la gestion d'une collection d'instances possedant, elles, des modules (d'où la profondeur supplémentaire). Pour faire simple, j'ai pris comme instances des classes utilisateurs, et pour modules de ces instances, leurs propriétés : ID, EMAIL, NOM...
Je crée ma collection d'utilisateurs dans la classe abstraite oCore, et je l'utilise via oSystem.
oSystem permet de traverser ma collection comme je le veux.
Comme mon package précédent, SMARTDIR, j'utilise des filtres pour traverser à ma guise ma collection.
Je pense que l'exemple est assez clair. Si vous voulez plus d'explication...demandez-moi :-)
Source / Exemple :
<?php
if (!class_exists ('RecursiveArrayIterator')) { // PHP5 < 5.1
class RecursiveArrayIterator extends ArrayIterator implements RecursiveIterator {
private $ref;
function hasChildren () {
return is_array ($this -> current ());
}
function getChildren () {
if ($this -> current () instanceof self) {
return $this -> current ();
}
if (empty ($this -> ref)) {
$this -> ref = new ReflectionClass ($this);
}
return $this -> ref -> newInstance($this -> current ());
}
}
}
class myFilter extends FilterIterator {
private $oIt = null;
private $mFilter = null;
private $mSubFilter = null;
public function __construct ($oIt, $mFilter = null, $mSubFilter = null) {
parent::__construct ($oIt);
$this -> oIt = $oIt;
$this -> mFilter = $mFilter;
$this -> mSubFilter = $mSubFilter;
}
public function accept () {
if (is_string ($this -> mFilter)) {
if ($this -> oIt -> key () === $this -> mFilter && $this -> oIt -> getDepth () === 1) {
return true;
}
if ($this -> oIt -> key () !== $this -> mFilter && $this -> oIt -> getDepth () === 1) {
return false;
}
if ($this -> oIt -> getDepth () > 1 && $this -> oIt -> getSubIterator (1) -> key () === $this -> mFilter) {
if (!is_null ($this -> mSubFilter)) {
if ($this -> key () === $this -> mSubFilter) {
return true;
}
return false;
}
return true;
}
return false;
}
return true;
}
}
abstract class oCore extends RecursiveArrayIterator {
private static $aRef = array ();
private $mFilter = null;
private $mSubFilter = null;
private $aCanBeSet = array (
'FILTER',
'SUBFILTER'
);
const ERROR_USER_ALREADY_EXISTS = '{__USER__} has already been instanciated';
const ERROR_USER_NOT_EXISTS = '{__USER__} is not an existing User';
const ERROR_NOT_SETABLE = '{__PROP__} is not a setable property';
const ERROR_ID_IS_NULL = 'Cannot collect a user with a null ID';
public function __construct () {
if (is_null ($this -> iUserId)) {
throw new Exception (self::ERROR_ID_IS_NULL);
}
if (array_key_exists ($this -> iUserId, self::$aRef)) {
throw new Exception (str_replace ('{__USER__}', $this -> iUserId, self::ERROR_USER_ALREADY_EXISTS));
}
self::$aRef[$this -> iUserId] = $this -> aFields;
}
public function getProps () {
if (!is_null ($this -> mFilter)) {
if (is_string ($this -> mFilter)) {
return new myFilter (new RecursiveIteratorIterator (new RecursiveArrayIterator (self::$aRef), true), $this -> mFilter, $this -> mSubFilter);
}
if (is_int ($this -> mFilter)) {
if (!isset (self::$aRef[$this -> mFilter])) {
throw new Exception (str_replace ('{__USER__}', $this -> iUserId, self::ERROR_USER_NOT_EXISTS));
}
return new RecursiveIteratorIterator (new RecursiveArrayIterator(self::$aRef[$this -> mFilter]), true);
}
}
return new RecursiveIteratorIterator (new RecursiveArrayIterator (self::$aRef), true);
}
public function __destruct() {
self::$aRef[$this -> iUserId] = null;
}
public function __set ($sProp, $mVal) {
if (!in_array ($sProp, $this -> aCanBeSet)) {
throw new Exception (str_replace ('{__PROP__}', $sProp, self::ERROR_NOT_SETABLE));
}
switch ($sProp) {
case 'FILTER' :
$this -> mFilter = $mVal;
break;
case 'SUBFILTER' :
$this -> mSubFilter = $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 oUser extends oCore {
protected $aFields = array ();
private $oDB = null;
protected $iUserId;
const ERROR_NO_DB_OBJECT = 'variable passed is not an object';
const ERROR_WRONG_SET_PROP = '{__PROP__} is not a valid setable property';
const ERROR_WRONG_GET_PROP = '{__PROP__} is not a valid getable property';
const ERROR_ID_NOT_INTEGER = '{__VAL__} is not a valid value por USER_ID : must be an integer';
private $aUserCanBeGet = array (
'NO_DB_OBJECT',
'USER_ID'
);
private $aUserCanBeSet = array (
'FIELD',
'USER_ID'
);
public function __construct ($oDB = null) {
/*
if (!is_object ($oDB)) {
throw new Exception (self::ERROR_NO_DB_OBJECT);
}
}
public function collect () {
parent::__construct ();
}
public function __get ($sProp) {
if (!in_array ($sProp, $this -> aUserCanBeGet)) {
throw new Exception (str_replace ('{__PROP__}', $sProp, self::ERROR_WRONG_GET_PROP));
}
switch ($sProp) {
case 'NO_DB_OBJECT' :
case 'WRONG_PROP' :
return $this -> aExceptions[$sProp];
break;
default :
return false;
break;
}
}
public function __set ($sProp, $mVal) {
if (!in_array ($sProp, $this -> aUserCanBeSet) && false === array_key_exists($sProp, $this -> aFields)) {
throw new Exception (str_replace ('{__PROP__}', $sProp, self::ERROR_WRONG_SET_PROP));
}
switch ($sProp) {
case 'FIELD' :
$this -> aFields[$mVal] = true;
break;
case 'USER_ID' :
if (!is_int ($mVal)) {
throw new Exception (str_replace ('{__VAL__}', $mVal, self::ERROR_ID_NOT_INTEGER));
}
$this -> iUserId = $mVal;
break;
default :
$this -> aFields[$sProp] = $mVal;
break;
}
}
}
/**
try {
$a = new oUser;
$a -> USER_ID = 666;
$a -> FIELD = 'EMAIL';
$a -> FIELD = 'NOM';
$a -> FIELD = 'PRENOM';
$a -> EMAIL = array ('VALUE' => 'a.a@a.com', 'TYPE' => 'STRING', 'PATTERN' => 'EMAIL');
$a -> NOM = array ('VALUE' => 'Durand', 'TYPE' => 'STRING','PATTERN' => 'TEXT');
$a -> PRENOM = array ('VALUE' => 'Paul', 'TYPE' => 'STRING', 'PATTERN' => 'TEXT');
$a -> collect ();
} catch (Exception $e) {
echo $e -> getMessage (), ' => ', $e -> getLine ();
}
try {
$b = new oUser;
$b -> USER_ID = 999;
$b -> FIELD = 'EMAIL';
$b -> FIELD = 'NOM';
$b -> FIELD = 'PRENOM';
$b -> EMAIL = array ('VALUE' => 'b.b@b.com', 'TYPE' => 'STRING', 'PATTERN' => 'EMAIL');
$b -> NOM = array ('VALUE' => 'Dubois', 'TYPE' => 'STRING','PATTERN' => 'TEXT');
$b -> PRENOM = array ('VALUE' => 'René', 'TYPE' => 'STRING', 'PATTERN' => 'TEXT');
$b -> collect ();
} catch (Exception $e) {
echo $e -> getMessage (), ' => ', $e -> getLine ();
}
try {
$oSys = new oSystem;
} catch (Exception $e) {
echo $e -> getMessage (), ' => ', $e -> getLine ();
}
echo '<strong>All Properties</strong><br />';
try {
$iIt = $oSys -> getProps ();
foreach ($iIt as $sK => $sV) {
echo str_repeat (' ', $iIt -> getDepth ()), $sK, ' => ', $sV, '<br />';
}
} catch (Exception $e) {
echo $e -> getMessage (), ' => ', $e -> getLine ();
}
echo '<br /><br /><strong>All Emails</strong><br />';
try {
$oSys -> FILTER = 'EMAIL';
$iIt = $oSys -> getProps ();
foreach ($iIt as $sK => $sV) {
echo str_repeat (' ', $iIt -> getDepth () - 1), $sK, ' => ', $sV, '<br />';
//echo $sK, ' => ', $sV, '<br />'; PHP 5 < 5.1
}
} catch (Exception $e) {
echo $e -> getMessage (), ' => ', $e -> getLine ();
}
echo '<br /><br /><strong>All Emails Value</strong><br />';
try {
$oSys -> FILTER = 'EMAIL';
$oSys -> SUBFILTER = 'VALUE';
$iIt = $oSys -> getProps ();
foreach ($iIt as $sK => $sV) {
echo str_repeat (' ', $iIt -> getDepth () - 1), $sK, ' => ', $sV, '<br />';
//echo $sK, ' => ', $sV, '<br />'; // PHP 5 < 5.1
}
} catch (Exception $e) {
echo $e -> getMessage (), ' => ', $e -> getLine ();
}
echo '<br /><br /><strong>Only user ID 666</strong><br />';
try {
$oSys -> FILTER = 666;
$oSys -> SUBFILTER = null;
$iIt = $oSys -> getProps ();
foreach ($iIt as $sK => $sV) {
echo str_repeat (' ', $iIt -> getDepth ()), $sK, ' => ', $sV, '<br />';
}
} catch (Exception $e) {
echo $e -> getMessage (), ' => ', $e -> getLine ();
}
?>
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.