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

Breadcrumb

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

class Attribute

Same name in this branch
  1. 11.1.x vendor/phpstan/phpdoc-parser/src/Ast/Attribute.php \PHPStan\PhpDocParser\Ast\Attribute
  2. 11.1.x vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/Annotation/Attribute.php \Doctrine\Common\Annotations\Annotation\Attribute
  3. 11.1.x vendor/nikic/php-parser/lib/PhpParser/Node/Attribute.php \PhpParser\Node\Attribute
  4. 11.1.x vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/Attribute.php \SlevomatCodingStandard\Helpers\Attribute
  5. 11.1.x core/lib/Drupal/Core/Template/Attribute.php \Drupal\Core\Template\Attribute

Selector part attribute class

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

Hierarchy

  • class \Peast\Selector\Node\Part\Part
    • class \Peast\Selector\Node\Part\Attribute extends \Peast\Selector\Node\Part\Part

Expanded class hierarchy of Attribute

16 string references to 'Attribute'
Attribute::getType in vendor/nikic/php-parser/lib/PhpParser/Node/Attribute.php
Gets the type of the node.
AttributeClassLoader::supports in vendor/symfony/routing/Loader/AttributeClassLoader.php
AttributeDirectoryLoader::supports in vendor/symfony/routing/Loader/AttributeDirectoryLoader.php
AttributeFileLoader::supports in vendor/symfony/routing/Loader/AttributeFileLoader.php
claro_preprocess_input in core/themes/claro/claro.theme
Implements template_preprocess_HOOK() for input.

... See full list

File

vendor/mck89/peast/lib/Peast/Selector/Node/Part/Attribute.php, line 20

Namespace

Peast\Selector\Node\Part
View source
class Attribute extends Part {
    
    /**
     * Priority
     *
     * @var int
     */
    protected $priority = 4;
    
    /**
     * Attribute names
     *
     * @var array
     */
    protected $names = array();
    
    /**
     * Attribute match operator
     *
     * @var array
     */
    protected $operator = null;
    
    /**
     * Attribute value
     *
     * @var mixed
     */
    protected $value = null;
    
    /**
     * Case insensitive flag
     *
     * @var bool
     */
    protected $caseInsensitive = false;
    
    /**
     * Regex flag
     *
     * @var bool
     */
    protected $regex = false;
    
    /**
     * Adds a name
     *
     * @param string $name Name
     *
     * @return $this
     */
    public function addName($name) {
        $this->names[] = $name;
        return $this;
    }
    
    /**
     * Sets the operator
     *
     * @param string $operator Operator
     *
     * @return $this
     */
    public function setOperator($operator) {
        $this->operator = $operator;
        return $this;
    }
    
    /**
     * Sets the value
     *
     * @param mixed $value Value
     *
     * @return $this
     */
    public function setValue($value) {
        $this->value = $value;
        return $this;
    }
    
    /**
     * Sets the case insensitive flag
     *
     * @param bool $caseInsensitive Case insensitive flag
     *
     * @return $this
     */
    public function setCaseInsensitive($caseInsensitive) {
        $this->caseInsensitive = $caseInsensitive;
        return $this;
    }
    
    /**
     * Sets the regex flag
     *
     * @param bool $regex Regex flag
     *
     * @return $this
     */
    public function setRegex($regex) {
        $this->regex = $regex;
        return $this;
    }
    
    /**
     * Returns true if the selector part matches the given node,
     * false otherwise
     *
     * @param Node $node    Node
     * @param Node $parent  Parent node
     *
     * @return bool
     */
    public function check(Node $node, $parent = null) {
        $attr = $node;
        foreach ($this->names as $name) {
            $attrFound = false;
            if ($attr instanceof Node) {
                $props = Utils::getNodeProperties($attr);
                foreach ($props as $prop) {
                    if ($prop["name"] === $name) {
                        $attrFound = true;
                        $attr = $attr->{$prop["getter"]}();
                        break;
                    }
                }
            }
            if (!$attrFound) {
                return false;
            }
        }
        $bothStrings = is_string($attr) && is_string($this->value);
        switch ($this->operator) {
            case "=":
                if ($bothStrings) {
                    if ($this->regex) {
                        return preg_match($this->value, $attr);
                    }
                    return $this->compareStr($this->value, $attr, $this->caseInsensitive, true, true);
                }
                if (is_int($attr) && is_float($this->value)) {
                    return (double) $attr === $this->value;
                }
                return $attr === $this->value;
            case "<":
                if (is_float($this->value) && !is_float($attr) && !is_int($attr) && !is_string($attr)) {
                    return false;
                }
                return $attr < $this->value;
            case ">":
                if (is_float($this->value) && !is_float($attr) && !is_int($attr) && !is_string($attr)) {
                    return false;
                }
                return $attr > $this->value;
            case "<=":
                if (is_float($this->value) && !is_float($attr) && !is_int($attr) && !is_string($attr)) {
                    return false;
                }
                return $attr <= $this->value;
            case ">=":
                if (is_float($this->value) && !is_float($attr) && !is_int($attr) && !is_string($attr)) {
                    return false;
                }
                return $attr >= $this->value;
            case "^=":
            case "\$=":
            case "*=":
                return $this->compareStr($this->value, $attr, $this->caseInsensitive, $this->operator === "^=", $this->operator === "\$=");
            default:
                return true;
        }
    }
    
    /**
     * Compares two strings
     *
     * @param string $v1                Search value
     * @param string $v2                Compare value
     * @param bool   $caseInsensitive   True if the search must be case insensitive
     * @param bool   $matchStart        True if the search must be executed from the
     *                                  beginning of the string
     * @param bool   $matchEnd          True if the search must be executed from the
     *                                  end of the string
     *
     * @return bool
     */
    protected function compareStr($v1, $v2, $caseInsensitive, $matchStart, $matchEnd) {
        $regex = "#" . ($matchStart ? "^" : "") . preg_quote($v1) . ($matchEnd ? "\$" : "") . "#u" . ($caseInsensitive ? "i" : "");
        return (bool) preg_match($regex, $v2);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
Attribute::$caseInsensitive protected property Case insensitive flag
Attribute::$names protected property Attribute names
Attribute::$operator protected property Attribute match operator
Attribute::$priority protected property Priority Overrides Part::$priority
Attribute::$regex protected property Regex flag
Attribute::$value protected property Attribute value
Attribute::addName public function Adds a name
Attribute::check public function Returns true if the selector part matches the given node,
false otherwise
Overrides Part::check
Attribute::compareStr protected function Compares two strings
Attribute::setCaseInsensitive public function Sets the case insensitive flag
Attribute::setOperator public function Sets the operator
Attribute::setRegex public function Sets the regex flag
Attribute::setValue public function Sets the value
Part::getPriority public function

API Navigation

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