2012-08-07 11 views
19

Cuando trato de tomar algún contenido de la página inexistente cojo este error:¿Cómo puedo verificar con seguridad si el nodo está vacío o no? (Symfony 2 orugas)

The current node list is empty. 
500 Internal Server Error - InvalidArgumentException 

¿Cómo puedo comprobar con seguridad que existe este contenido, o no? Aquí algunos ejemplos que no funciona:

if($crawler->filter('.PropertyBody')->eq(2)->text()){ 
    // bla bla 
} 

if(!empty($crawler->filter('.PropertyBody')->eq(2)->text())){ 
    // bla bla 
} 
if(($crawler->filter('.PropertyBody')->eq(2)->text()) != null){ 
    // bla bla 
} 

Gracias, lo ayudaron a mí mismo con:

$count = $crawler->filter('.PropertyBody')->count(); 
if($count > 2){ 
    $marks = $crawler->filter('.PropertyBody')->eq(2)->text(); 
} 
+1

Gracias hombre. ¡Tu solución me salvó mucho! –

+0

¡El conteo de comprobación ayudó! –

Respuesta

5

¿Has probado algo como esto?

$text = null; 
if (!empty($body = $crawler->filter('.PropertyBody'))) { 
    if (!empty($node = $body->eq(2))) { 
     $text = $node->text(); 
    } 
} 

$this->assertContains('yourText', $text); 
+2

la función vacía todavía se saltó. –

+1

tampoco funcionó para mí. Pero usar -> contar() hizo el truco – SuN

5
$marks = ($crawler->filter('.PropertyBody')->count()) ? $crawler->filter('.PropertyBody')->eq(2)->text() : ''; 
0
try { 
    $text = $crawler->filter('.PropertyBody')->eq(2)->text(); 
} catch (\InvalidArgumentException $e) { 
    // Handle the current node list is empty.. 
} 
Cuestiones relacionadas