/** * Extend minimal Downline Tree Data (customer & parent) with depth and path. * * @param Request\ExpandMinimal $request * * @return Response\ExpandMinimal * * @deprecated use \Praxigento\Downline\Tool\ITree::expandMinimal instead */ public function expandMinimal(Request\ExpandMinimal $request) { $result = new Response\ExpandMinimal(); $keyCustomerId = $request->getKeyCustomerId(); $keyParentId = $request->getKeyParentId(); $treeIn = $request->getTree(); /** * Validate tree consistency: all parents should be customers too, create map for customers with invalid * parents to set this entries as orphans. */ $mapCusts = []; // registry for all customers. foreach ($treeIn as $ndx => $item) { $custId = is_null($keyCustomerId) ? $ndx : $item[$keyCustomerId]; $mapCusts[] = $custId; } $mapOrphans = []; // registry for orphan customers. foreach ($treeIn as $ndx => $item) { $custId = is_null($keyCustomerId) ? $ndx : $item[$keyCustomerId]; $parentId = !is_array($item) ? $item : $item[$keyParentId]; if (!in_array($parentId, $mapCusts)) { $msg = "Parent #{$parentId} for customer #{$custId} is not present in the minimal tree."; $msg .= " Customer #{$custId} is set as orphan."; $this->_logger->warning($msg); $mapOrphans[] = $custId; } } /* create tree (see http://stackoverflow.com/questions/2915748/how-can-i-convert-a-series-of-parent-child-relationships-into-a-hierarchical-tre) */ $flat = []; $tree = []; foreach ($treeIn as $ndx => $item) { $custId = is_null($keyCustomerId) ? $ndx : $item[$keyCustomerId]; $parentId = !is_array($item) ? $item : $item[$keyParentId]; /* filter orphans */ if (in_array($custId, $mapOrphans)) { $parentId = $custId; } /* map customers into tree */ if (!isset($flat[$custId])) { $flat[$custId] = []; } if ($custId != $parentId) { $flat[$parentId][$custId] =& $flat[$custId]; } else { /* root node */ $tree[$custId] =& $flat[$custId]; } } /* populate tree with depth/path/... and compose array to insert into DB */ $snapData = []; $this->_composeSnapData($snapData, $tree); $result->setSnapData($snapData); $result->markSucceed(); return $result; }