Je suis en train d'accéder aux clés de tableau d'un tableau:les meilleures pratiques pour vérifier plusieurs array_key_exists en PHP
$attributes = $xml['SAMLP:RESPONSE']['SAML:ASSERTION']['SAML:ATTRIBUTESTATEMENT']['SAML:ATTRIBUTE']['SAML:ATTRIBUTEVALUE'];
La façon dont je l'ai fait fonctionne très bien si la réponse sort bien comme je m'y attendais. Sinon, je vais obtenir quelque chose comme ceci:
Undefined index: samlp: RÉPONSE
J'ai essayé:
try {
$attributes = $xml['SAMLP:RESPONSE']['SAML:ASSERTION']['SAML:ATTRIBUTESTATEMENT']['SAML:ATTRIBUTE']['SAML:ATTRIBUTEVALUE'];
} catch (Exception $e) {
Helper::console("Bad SAML RESPONSE.");
dd('Sorry, we could not find your data. Please contact Business Customer Service at 015332266.');
}
J'essaie d'éviter de faire beaucoup
if (array_key_exists('SAMLP:RESPONSE', $xml)) {
if (array_key_exists('SAML:ASSERTION', $xml['SAMLP:RESPONSE'])) {
if (array_key_exists('SAML:ATTRIBUTESTATEMENT', $xml['SAMLP:RESPONSE']['SAML:ASSERTION'])) {
if (array_key_exists('SAML:ATTRIBUTE', $xml['SAMLP:RESPONSE']['SAML:ASSERTION']['SAML:ATTRIBUTESTATEMENT'])) {
if (array_key_exists('SAML:ATTRIBUTEVALUE', $xml['SAMLP:RESPONSE']['SAML:ASSERTION']['SAML:ATTRIBUTESTATEMENT']['SAML:ATTRIBUTE'])) {
$attributes = $xml['SAMLP:RESPONSE']['SAML:ASSERTION']['SAML:ATTRIBUTESTATEMENT']['SAML:ATTRIBUTE']['SAML:ATTRIBUTEVALUE'];
}else{
Helper::console("['SAML:ATTRIBUTEVALUE'] key not exist");
dd('Sorry, we could not find your data. Please contact Business Customer Service at 015332266.');
}
}else{
Helper::console("['SAML:ATTRIBUTE'] key not exist");
dd('Sorry, we could not find your data. Please contact Business Customer Service at 015332266.');
}
}else{
Helper::console("['SAML:ATTRIBUTESTATEMENT'] key not exist");
dd('Sorry, we could not find your data. Please contact Business Customer Service at 015332266.');
}
}else{
Helper::console("['SAMLP:RESPONSE'] key not exist");
dd('Sorry, we could not find your data. Please contact Business Customer Service at 015332266.');
}
} else {
Helper::console('SAMLP:RESPONSE key not exist');
dd('Sorry, we could not find your data. Please contact Business Customer Service at 015332266.');
}
Ce qui serait la bonne façon ou la meilleure pratique pour vérifier quelque chose comme ça? Comment irait-on et le faire?