2017-07-19 1 views
2

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?

Répondre

2

Bien longtemps, un isset fonctionnera:

if(isset($xml['SAMLP:RESPONSE'] 
      ['SAML:ASSERTION'] 
      ['SAML:ATTRIBUTESTATEMENT'] 
      ['SAML:ATTRIBUTE'] 
      ['SAML:ATTRIBUTEVALUE'])) { 

    $attributes = $xml['SAMLP:RESPONSE'] 
         ['SAML:ASSERTION'] 
         ['SAML:ATTRIBUTESTATEMENT'] 
         ['SAML:ATTRIBUTE'] 
         ['SAML:ATTRIBUTEVALUE']; 
} 

Ou en PHP 7 le Null coalescing operator assignera si elle est définie ou attribuer une autre valeur sinon:

$attributes = $xml['SAMLP:RESPONSE'] 
        ['SAML:ASSERTION'] 
        ['SAML:ATTRIBUTESTATEMENT'] 
        ['SAML:ATTRIBUTE'] 
        ['SAML:ATTRIBUTEVALUE'] ?? null; 

Vous pouvez également consulter la Getter fonction de How to write getter/setter to access multi-level array by key names? et le transmettre quelque chose comme:

$path = "SAMLP:RESPONSE.SAML:ASSERTION.SAML:ATTRIBUTESTATEMENT.SAML:ATTRIBUTE.SAML:ATTRIBUTEVALUE"; 
$attributes = get($path, $xml); //returns NULL if the path doesn't exist 

J'ai utilisé un séparateur ., mais vous pouvez utiliser n'importe quel séparateur autre que : comme / ou - à la place.