Allow NodeList to be accessed via array like syntax. #3078

Closed
wants to merge 1 commit into
from

Projects

None yet

3 participants

@blainesch

If we do a execute a selector and get back a NodeList and want just the first element we can now access it like so:

<?php
use Zend\Dom\Query;

$dom = new Query($html);
$results = $dom->execute('.foo .bar a');

$count = count($results); // get number of matches: 4

$results[2]->getAttribute('id'); // access individually

foreach ($results as $result) { // or iterate
    // $result is a DOMElement
}
?>

Where before you could only iterate them which is really ineffective for a lot of projects.

Blaine Schmeisser Allow NodeList to be accessed via array like syntax.
If we do a execute a selector and get back a NodeList and want just the first element we can now access it like so:

~~~ php
<?php
use Zend\Dom\Query;

$dom = new Query($html);
$results = $dom->execute('.foo .bar a');

$count = count($results); // get number of matches: 4

$results[2]->getAttribute('id'); // access individually

foreach ($results as $result) { // or iterate
    // $result is a DOMElement
}
?>
~~~

Where before you could only iterate them which is really ineffective for a lot of projects.
29cfd0b
@joseym

👍

@weierophinney weierophinney added a commit that referenced this pull request Dec 10, 2012
@weierophinney weierophinney [#3078] fix failing tests in 5.4.5
- DOMXPath::query() no longer raises an exception on failure; it now
  raises an E_WARNING. Updated code to detect an E_WARNING and raise an
  exception when discovered.
a65ce08
@weierophinney
Zend Framework member

Found an issue with PHP >= 5.4.1 -- DOMXPath::query() no longer throws an exception when it encounters extensions it does not know about. Updated the code to use the ErrorHandler so it can detect these situations.

Also: merged only to develop, as this is a new feature. It will release with 2.1.0.

@ghost Unknown pushed a commit that referenced this pull request Jul 14, 2013
@weierophinney weierophinney [#3078] fix failing tests in 5.4.5
- DOMXPath::query() no longer raises an exception on failure; it now
  raises an E_WARNING. Updated code to detect an E_WARNING and raise an
  exception when discovered.
9f1e8b5
@ghost Unknown pushed a commit that referenced this pull request Jul 14, 2013
@weierophinney weierophinney Merge branch 'feature/dom-nodelist-access' into develop
Close #3078
493dadc
@weierophinney weierophinney added a commit to zendframework/zend-dom that referenced this pull request May 15, 2015
@weierophinney weierophinney [zendframework/zendframework#3078] fix failing tests in 5.4.5
- DOMXPath::query() no longer raises an exception on failure; it now
  raises an E_WARNING. Updated code to detect an E_WARNING and raise an
  exception when discovered.
11e62a7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment