web/lib/Zend/Dom/Query/Result.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Dom
       
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    19  */
       
    20 
       
    21 /**
       
    22  * Results for DOM XPath query
       
    23  *
       
    24  * @package    Zend_Dom
       
    25  * @subpackage Query
       
    26  * @uses       Iterator
       
    27  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    28  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    29  * @version    $Id: Result.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    30  */
       
    31 class Zend_Dom_Query_Result implements Iterator,Countable
       
    32 {
       
    33     /**
       
    34      * Number of results
       
    35      * @var int
       
    36      */
       
    37     protected $_count;
       
    38 
       
    39     /**
       
    40      * CSS Selector query
       
    41      * @var string
       
    42      */
       
    43     protected $_cssQuery;
       
    44 
       
    45     /**
       
    46      * @var DOMDocument
       
    47      */
       
    48     protected $_document;
       
    49 
       
    50     /**
       
    51      * @var DOMNodeList
       
    52      */
       
    53     protected $_nodeList;
       
    54 
       
    55     /**
       
    56      * Current iterator position
       
    57      * @var int
       
    58      */
       
    59     protected $_position = 0;
       
    60 
       
    61     /**
       
    62      * @var DOMXPath
       
    63      */
       
    64     protected $_xpath;
       
    65 
       
    66     /**
       
    67      * XPath query
       
    68      * @var string
       
    69      */
       
    70     protected $_xpathQuery;
       
    71 
       
    72     /**
       
    73      * Constructor
       
    74      *
       
    75      * @param  string $cssQuery
       
    76      * @param  string|array $xpathQuery
       
    77      * @param  DOMDocument $document
       
    78      * @param  DOMNodeList $nodeList
       
    79      * @return void
       
    80      */
       
    81     public function  __construct($cssQuery, $xpathQuery, DOMDocument $document, DOMNodeList $nodeList)
       
    82     {
       
    83         $this->_cssQuery   = $cssQuery;
       
    84         $this->_xpathQuery = $xpathQuery;
       
    85         $this->_document   = $document;
       
    86         $this->_nodeList   = $nodeList;
       
    87     }
       
    88 
       
    89     /**
       
    90      * Retrieve CSS Query
       
    91      *
       
    92      * @return string
       
    93      */
       
    94     public function getCssQuery()
       
    95     {
       
    96         return $this->_cssQuery;
       
    97     }
       
    98 
       
    99     /**
       
   100      * Retrieve XPath query
       
   101      *
       
   102      * @return string
       
   103      */
       
   104     public function getXpathQuery()
       
   105     {
       
   106         return $this->_xpathQuery;
       
   107     }
       
   108 
       
   109     /**
       
   110      * Retrieve DOMDocument
       
   111      *
       
   112      * @return DOMDocument
       
   113      */
       
   114     public function getDocument()
       
   115     {
       
   116         return $this->_document;
       
   117     }
       
   118 
       
   119     /**
       
   120      * Iterator: rewind to first element
       
   121      *
       
   122      * @return void
       
   123      */
       
   124     public function rewind()
       
   125     {
       
   126         $this->_position = 0;
       
   127         return $this->_nodeList->item(0);
       
   128     }
       
   129 
       
   130     /**
       
   131      * Iterator: is current position valid?
       
   132      *
       
   133      * @return bool
       
   134      */
       
   135     public function valid()
       
   136     {
       
   137         if (in_array($this->_position, range(0, $this->_nodeList->length - 1)) && $this->_nodeList->length > 0) {
       
   138             return true;
       
   139         }
       
   140         return false;
       
   141     }
       
   142 
       
   143     /**
       
   144      * Iterator: return current element
       
   145      *
       
   146      * @return DOMElement
       
   147      */
       
   148     public function current()
       
   149     {
       
   150         return $this->_nodeList->item($this->_position);
       
   151     }
       
   152 
       
   153     /**
       
   154      * Iterator: return key of current element
       
   155      *
       
   156      * @return int
       
   157      */
       
   158     public function key()
       
   159     {
       
   160         return $this->_position;
       
   161     }
       
   162 
       
   163     /**
       
   164      * Iterator: move to next element
       
   165      *
       
   166      * @return void
       
   167      */
       
   168     public function next()
       
   169     {
       
   170         ++$this->_position;
       
   171         return $this->_nodeList->item($this->_position);
       
   172     }
       
   173 
       
   174     /**
       
   175      * Countable: get count
       
   176      *
       
   177      * @return int
       
   178      */
       
   179     public function count()
       
   180     {
       
   181         return $this->_nodeList->length;
       
   182     }
       
   183 }