1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
static function GenerateContainer(array $objects)
{
$_parentList = array(); // Contient la liste des objets parent
$_objectIdList = array(); // Contient la liste des id des objets qu'on manipule
$_objectRootList = array(); // Contient la lsite des object root
// On récupère la liste des object précédent un autre object
// ainsi que la liste des Id d'object (utilisé plus tard)
foreach($objects as $_object)
{
$_objectIdList[] = $_object->id;
if(!is_null($_object>parentId))
$_parentList[] = $_object->parentId;
else
$_objectRootList[] = $_object;
}
$_parentList = array_unique($_parentList);
// Pour chaque object, on génère la liste des object enfants
foreach($objects as $_object)
if(in_array($_object->id, $_parentList))
$_object->setChilds(self::_generateChildsContainer($_object->id,$objects));
// On recherche les object de départ (celle qui ont un précédent qui n'est pas dans la liste des object)
foreach($objects as $_object)
if(!is_null($_object->parentId) && !in_array($_object->parentId,$_objectIdList))
$_objectRootList[] = $_object;
}
static function _generateChildsContainer($objectId,array $objects)
{
$_childs = array();
foreach($objects as $_object)
if(!is_null($_object->parentId) && $objectId->id==$_object->parentId)
$_childs[] = $_object;
return $_childs;
} |
Partager