vendor/jackalope/jackalope/src/Jackalope/Query/QueryResult.php line 20

Open in your IDE?
  1. <?php
  2. namespace Jackalope\Query;
  3. use Iterator;
  4. use Jackalope\ObjectManager;
  5. use Jackalope\FactoryInterface;
  6. use PHPCR\Query\QueryResultInterface;
  7. use IteratorAggregate;
  8. use PHPCR\RepositoryException;
  9. /**
  10.  * {@inheritDoc}
  11.  *
  12.  * @license http://www.apache.org/licenses Apache License Version 2.0, January 2004
  13.  * @license http://opensource.org/licenses/MIT MIT License
  14.  *
  15.  * @api
  16.  */
  17. class QueryResult implements IteratorAggregateQueryResultInterface
  18. {
  19.     /**
  20.      * @var ObjectManager
  21.      */
  22.     protected $objectmanager;
  23.     /**
  24.      * @var FactoryInterface
  25.      */
  26.     protected $factory;
  27.     /**
  28.      * Storing the query result raw data
  29.      * @see QueryInterface::query()
  30.      * @var array
  31.      */
  32.     protected $rows = [];
  33.     /**
  34.      * Create a new query result from raw data from transport.
  35.      *
  36.      * @see QueryInterface::query() The raw data format
  37.      *
  38.      * @param FactoryInterface $factory       the object factory
  39.      * @param array            $rawData       the data as returned by the transport
  40.      * @param ObjectManager    $objectManager
  41.      */
  42.     public function __construct(FactoryInterface $factory$rawDataObjectManager $objectManager)
  43.     {
  44.         $this->factory $factory;
  45.         $this->rows $rawData;
  46.         $this->objectmanager $objectManager;
  47.     }
  48.     /**
  49.      * Implement the IteratorAggregate interface and returns exactly the same
  50.      * iterator as QueryResult::getRows()
  51.      *
  52.      * @return Iterator implementing <b>SeekableIterator</b> and <b>Countable</b>.
  53.      *      Keys are the row position in this result set, Values are the
  54.      *      RowInterface instances.
  55.      *
  56.      * @throws RepositoryException if this call is the second time
  57.      *      getIterator(), getRows() or getNodes() has been called on the same
  58.      *      QueryResult object or if another error occurs.
  59.      *
  60.      * @api
  61.      */
  62.     public function getIterator()
  63.     {
  64.         return $this->getRows();
  65.     }
  66.     /**
  67.      * {@inheritDoc}
  68.      *
  69.      * @api
  70.      */
  71.     public function getColumnNames()
  72.     {
  73.         $columnNames = [];
  74.         foreach ($this->rows as $row) {
  75.             foreach ($row as $columns) {
  76.                 if ('jcr:path' !== substr($columns['dcr:name'], -8)
  77.                     && 'jcr:score' !== substr($columns['dcr:name'], -9)
  78.                 ) {
  79.                     // skip the meta information path and score that is also in the raw result table
  80.                     $columnNames[] = $columns['dcr:name'];
  81.                 }
  82.             }
  83.         }
  84.         return array_unique($columnNames);
  85.     }
  86.     /**
  87.      * {@inheritDoc}
  88.      *
  89.      * @api
  90.      */
  91.     public function getRows()
  92.     {
  93.         return $this->factory->get(RowIterator::class, [$this->objectmanager$this->rows]);
  94.     }
  95.     /**
  96.      * {@inheritDoc}
  97.      *
  98.      * @api
  99.      */
  100.     public function getNodes($prefetch false)
  101.     {
  102.         if ($prefetch !== true) {
  103.             return $this->factory->get(NodeIterator::class, [$this->objectmanager$this->rows]);
  104.         }
  105.         $paths = [];
  106.         foreach ($this->getRows() as $row) {
  107.             $paths[] = $row->getPath();
  108.         }
  109.         return $this->objectmanager->getNodesByPath($paths);
  110.     }
  111.     /**
  112.      * {@inheritDoc}
  113.      *
  114.      * @api
  115.      */
  116.     public function getSelectorNames()
  117.     {
  118.         $selectorNames = [];
  119.         foreach ($this->rows as $row) {
  120.             foreach ($row as $column) {
  121.                 if (array_key_exists('dcr:selectorName'$column)) {
  122.                     $selectorNames[] = $column['dcr:selectorName'];
  123.                 }
  124.             }
  125.         }
  126.         return array_unique($selectorNames);
  127.     }
  128. }