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

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.     #[\ReturnTypeWillChange]
  63.     public function getIterator()
  64.     {
  65.         return $this->getRows();
  66.     }
  67.     /**
  68.      * {@inheritDoc}
  69.      *
  70.      * @api
  71.      */
  72.     public function getColumnNames()
  73.     {
  74.         $columnNames = [];
  75.         foreach ($this->rows as $row) {
  76.             foreach ($row as $columns) {
  77.                 if ('jcr:path' !== substr($columns['dcr:name'], -8)
  78.                     && 'jcr:score' !== substr($columns['dcr:name'], -9)
  79.                 ) {
  80.                     // skip the meta information path and score that is also in the raw result table
  81.                     $columnNames[] = $columns['dcr:name'];
  82.                 }
  83.             }
  84.         }
  85.         return array_unique($columnNames);
  86.     }
  87.     /**
  88.      * {@inheritDoc}
  89.      *
  90.      * @api
  91.      */
  92.     public function getRows()
  93.     {
  94.         return $this->factory->get(RowIterator::class, [$this->objectmanager$this->rows]);
  95.     }
  96.     /**
  97.      * {@inheritDoc}
  98.      *
  99.      * @api
  100.      */
  101.     public function getNodes($prefetch false)
  102.     {
  103.         if ($prefetch !== true) {
  104.             return $this->factory->get(NodeIterator::class, [$this->objectmanager$this->rows]);
  105.         }
  106.         $paths = [];
  107.         foreach ($this->getRows() as $row) {
  108.             $paths[] = $row->getPath();
  109.         }
  110.         return $this->objectmanager->getNodesByPath($paths);
  111.     }
  112.     /**
  113.      * {@inheritDoc}
  114.      *
  115.      * @api
  116.      */
  117.     public function getSelectorNames()
  118.     {
  119.         $selectorNames = [];
  120.         foreach ($this->rows as $row) {
  121.             foreach ($row as $column) {
  122.                 if (array_key_exists('dcr:selectorName'$column)) {
  123.                     $selectorNames[] = $column['dcr:selectorName'];
  124.                 }
  125.             }
  126.         }
  127.         return array_unique($selectorNames);
  128.     }
  129. }