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

Breadcrumb

  1. Drupal Core 11.1.x
  2. Matches.php

class Matches

Selector matches class

@author Marco Marchiò <marco.mm89@gmail.com>

Hierarchy

  • class \Peast\Selector\Matches

Expanded class hierarchy of Matches

4 files declare their use of Matches
Combinator.php in vendor/mck89/peast/lib/Peast/Selector/Node/Combinator.php
Group.php in vendor/mck89/peast/lib/Peast/Selector/Node/Group.php
PseudoSelector.php in vendor/mck89/peast/lib/Peast/Selector/Node/Part/PseudoSelector.php
Selector.php in vendor/mck89/peast/lib/Peast/Selector/Node/Selector.php
1 string reference to 'Matches'
PregMatchParameterOutTypeExtension::isStaticMethodSupported in vendor/composer/pcre/src/PHPStan/PregMatchParameterOutTypeExtension.php

File

vendor/mck89/peast/lib/Peast/Selector/Matches.php, line 19

Namespace

Peast\Selector
View source
class Matches {
    
    /**
     * Matches array
     *
     * @var array
     */
    protected $matches;
    
    /**
     * Class constructor
     *
     * @param array $matches Matches
     */
    public function __construct($matches = array()) {
        $this->matches = $matches;
    }
    
    /**
     * Adds a new match
     *
     * @param Node $node
     * @param Node|null $parent
     */
    public function addMatch(Node $node, $parent = null) {
        $this->matches[] = array(
            $node,
            $parent,
        );
    }
    
    /**
     * Returns the matches
     *
     * @returns array
     */
    public function getMatches() {
        return $this->matches;
    }
    
    /**
     * Returns the matched nodes
     *
     * @return array
     */
    public function getNodes() {
        return array_map(function ($m) {
            return $m[0];
        }, $this->matches);
    }
    
    /**
     * Filters the matches using the given function, if it returns
     * a false value the match will be removed. The function will
     * receive the node and its parent as arguments.
     *
     * @param callable $fn Filter function
     * @return $this
     */
    public function filter(callable $fn) {
        $newMatches = array();
        foreach ($this->matches as $match) {
            if ($fn($match[0], $match[1])) {
                $newMatches[] = $match;
            }
        }
        $this->matches = $newMatches;
        return $this;
    }
    
    /**
     * Replaces all the matches with the result of the given function.
     * The function will receive the node and its parent as arguments
     * and must return an array of matches
     *
     * @param callable $fn Map function
     *
     * @return $this
     */
    public function map(callable $fn) {
        $newMatches = array();
        foreach ($this->matches as $match) {
            $res = $fn($match[0], $match[1]);
            if ($res) {
                $newMatches = array_merge($newMatches, $res);
            }
        }
        $this->matches = $newMatches;
        return $this->unique();
    }
    
    /**
     * Merges the current object with the other given Matches objects
     *
     * @param Matches[] $matchesArr Array of Matches to merge
     *
     * @return $this
     */
    public function merge($matchesArr) {
        foreach ($matchesArr as $matches) {
            foreach ($matches->getMatches() as $match) {
                $this->addMatch($match[0], $match[1]);
            }
        }
        return $this->unique();
    }
    
    /**
     * Remove all duplicated matches
     *
     * @return $this
     */
    public function unique() {
        $newMatches = array();
        $newNodes = array();
        foreach ($this->matches as $match) {
            if (!in_array($match[0], $newNodes, true)) {
                $newMatches[] = $match;
                $newNodes[] = $match[0];
            }
        }
        $this->matches = $newMatches;
        return $this;
    }
    
    /**
     * Returns a clone of the current object
     *
     * @return Matches
     */
    public function createClone() {
        return new self($this->matches);
    }
    
    /**
     * Returns the number of matches
     *
     * @return int
     */
    public function count() {
        return count($this->matches);
    }
    
    /**
     * Returns the match at the given index
     *
     * @param int $index Index
     *
     * @return array
     *
     * @throws \Exception
     */
    public function get($index) {
        $index = (int) $index;
        if (!isset($this->matches[$index])) {
            throw new \Exception("Invalid index {$index}");
        }
        return $this->matches[$index];
    }

}

Members

Title Sort descending Modifiers Object type Summary
Matches::$matches protected property Matches array
Matches::addMatch public function Adds a new match
Matches::count public function Returns the number of matches
Matches::createClone public function Returns a clone of the current object
Matches::filter public function Filters the matches using the given function, if it returns
a false value the match will be removed. The function will
receive the node and its parent as arguments.
Matches::get public function Returns the match at the given index
Matches::getMatches public function Returns the matches
Matches::getNodes public function Returns the matched nodes
Matches::map public function Replaces all the matches with the result of the given function.
The function will receive the node and its parent as arguments
and must return an array of matches
Matches::merge public function Merges the current object with the other given Matches objects
Matches::unique public function Remove all duplicated matches
Matches::__construct public function Class constructor

API Navigation

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