public static function isIdpValidForSp(
        $idpEntityId,
        $idpMdEntry,
        $spEntityId,
        $idps4Sp
    ) {
        $forSpsList = Null;

        if (isset($idpMdEntry[self::SP_LIST_KEY])) {
            $forSpsList = $idpMdEntry[self::SP_LIST_KEY];
        }

        // If there is an exclusive white list for this IDP, but this SP
        // is not included, then not valid
        if ($forSpsList !== Null && ! in_array($spEntityId, $forSpsList)) {
            return False;
        }

        $excludeByDefault = False;
        if (isset($idpMdEntry[self::EXCLUDE_KEY]) &&
            $idpMdEntry[self::EXCLUDE_KEY] === True) {
            $excludeByDefault = True;
        }

        // If the SP does not expect to know about certain IDP's and
        // this idp does not want to be seen without an explicit request, skip it.
        if ( ! $idps4Sp && $excludeByDefault === True) {
            return False;
        }

        // If the SP only wants to know about certain IDP's and this one
        // is not one of those, skip it.
        if ($idps4Sp) {
            if ( ! in_array($idpEntityId, $idps4Sp)) {
                return False;
            }
        }

        // Everything is OK, so add this idp as one of the auth sources
        return True;
    }