FluentDOM\Query::toggleClass PHP Method

toggleClass() public method

Adds the specified classes if the switch is TRUE, removes the specified classes if the switch is FALSE, toggles the specified classes if the switch is NULL.
public toggleClass ( string | callable $class, null | boolean $switch = NULL ) : Query
$class string | callable
$switch null | boolean toggle if NULL, add if TRUE, remove if FALSE
return Query
    public function toggleClass($class, $switch = NULL)
    {
        $callback = Constraints::isCallable($class);
        $this->each(function (\DOMElement $node, $index) use($class, $switch, $callback) {
            if ($callback) {
                $classString = $callback($node, $index, $node->getAttribute('class'));
            } else {
                $classString = $class;
            }
            if (empty($classString) && !(bool) $switch) {
                if ($node->hasAttribute('class')) {
                    $node->removeAttribute('class');
                }
            } else {
                $modified = $this->changeClassString($node->getAttribute('class'), $classString, $switch);
                if (FALSE !== $modified) {
                    if (empty($modified)) {
                        $node->removeAttribute('class');
                    } else {
                        $node->setAttribute('class', $modified);
                    }
                }
            }
        }, TRUE);
        return $this;
    }