Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x

Query.php

Same filename in this branch
  1. 11.1.x vendor/guzzlehttp/psr7/src/Query.php
  2. 11.1.x core/lib/Drupal/Core/Config/Entity/Query/Query.php
  3. 11.1.x core/lib/Drupal/Core/Entity/Query/Null/Query.php
  4. 11.1.x core/lib/Drupal/Core/Entity/Query/Sql/Query.php
  5. 11.1.x core/lib/Drupal/Core/Entity/KeyValueStore/Query/Query.php
  6. 11.1.x core/lib/Drupal/Core/Database/Query/Query.php
  7. 11.1.x core/modules/workspaces/src/EntityQuery/Query.php

Namespace

Peast

File

vendor/mck89/peast/lib/Peast/Query.php

View source
<?php


/**
 * This file is part of the Peast package
 *
 * (c) Marco Marchiò <marco.mm89@gmail.com>
 *
 * For the full copyright and license information refer to the LICENSE file
 * distributed with this source code
 */
namespace Peast;


/**
 * Nodes query class
 * 
 * @author Marco Marchiò <marco.mm89@gmail.com>
 */
class Query implements \IteratorAggregate, \Countable {
    
    /**
     * Current matches
     *
     * @var Selector\Matches
     */
    protected $matches;
    
    /**
     * Options array
     *
     * @var array
     */
    protected $options;
    
    /**
     * Class constructor. Available options are:
     * - encoding: selectors encoding. If not specified the
     *   parser will assume UTF-8.
     *
     * @param Syntax\Node\Program $root     Root node
     * @param array               $options  Options array
     */
    public function __construct(Syntax\Node\Program $root, $options = array()) {
        $this->matches = new Selector\Matches();
        $this->matches
            ->addMatch($root);
        $this->options = $options;
    }
    
    /**
     * Finds nodes matching the given selector starting from the
     * current matched nodes, if any, or from the root
     *
     * @param string $selector Selector
     *
     * @return $this
     *
     * @throws Selector\Exception
     */
    public function find($selector) {
        $parser = new Selector\Parser($selector, $this->options);
        $selector = $parser->parse();
        $this->matches = $selector->exec($this->matches);
        return $this;
    }
    
    /**
     * Executes the given selector on the current nodes and filters
     * out the nodes which don't match
     *
     * @param string $selector Selector
     *
     * @return $this
     *
     * @throws Selector\Exception
     */
    public function filter($selector) {
        $parser = new Selector\Parser($selector, $this->options);
        $selector = $parser->parse(true);
        $this->matches
            ->filter(function ($node, $parent) use ($selector) {
            $newMatch = new Selector\Matches();
            $newMatch->addMatch($node, $parent);
            return $selector->exec($newMatch)
                ->getMatches();
        });
        return $this;
    }
    
    /**
     * Returns the number of matched nodes
     *
     * @return int
     */
    public function count() {
        return $this->matches
            ->count();
    }
    
    /**
     * Returns the node at the given index
     *
     * @param int $index Index
     *
     * @return array
     *
     * @throws \Exception
     */
    public function get($index) {
        return $this->matches
            ->get($index)[0];
    }
    
    /**
     * Returns the nodes iterator
     *
     * @return \ArrayIterator
     */
    public function getIterator() {
        return new \ArrayIterator($this->matches
            ->getNodes());
    }

}

Classes

Title Deprecated Summary
Query Nodes query class

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal