FluentDOM\Query::toggleClass PHP 메소드

toggleClass() 공개 메소드

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
리턴 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;
    }