Location: PHPKode > scripts > Anti_Util_CircularReference > anti_util_circularreference/Anti/Util/CircularReference.php
<?php
/**
 * @author  Christian Reinecke <hide@address.com>
 * @since   2009-04-30
 * @version 1.0
 */
abstract class Anti_Util_CircularReference
{
    /**
     * @throws Anti_Util_CircularReference_Exception
     * @param array $waypoints
     * @return null
     */
    public static function checkArray(array $waypoints)
    {
        foreach ($waypoints as $index => $waypoint) {
            list ($x, $y) = $waypoint;
            $waypointIds   = array($x, $y);
            $waypointTrace = array(array("index" => $index, "waypoint" => $waypoint));
            self::_buildWaypointRoad($y, $waypoints, $waypointIds, $waypointTrace);
        }
    }

    /**
     * @throws Anti_Util_CircularReference_Exception
     * @param int $z previous waypoint ($y)
     * @param array $waypoints
     * @param array $waypointIds
     * @param array $waypointTrace
     */
    protected static function _buildWaypointRoad($z, array &$waypoints, array $waypointIds, array $waypointTrace)
    {
        if (count($waypointIds) != count(array_unique($waypointIds))) {
            throw new Anti_Util_CircularReference_Exception("circular reference found", 0, $waypoints, $waypointTrace);
        }
        foreach ($waypoints as $index => $waypoint) {
            list ($x, $y) = $waypoint;
            if ($z == $x) {
                $tmpWaypointIds     = $waypointIds;
                $tmpWaypointIds[]   = $y;
                $tmpWaypointTrace   = $waypointTrace;
                $tmpWaypointTrace[] = array("index" => $index, "waypoint" => $waypoint);

                self::_buildWaypointRoad($y, $waypoints, $tmpWaypointIds, $tmpWaypointTrace);
            }
        }
    }
}
?>
Return current item: Anti_Util_CircularReference