PHP Classes

File: classes/dom/dom-node-class-capable.php

Recommend this page to a friend!
  Classes of Gonzalo Chumillas   PHP Query   classes/dom/dom-node-class-capable.php   Download  
File: classes/dom/dom-node-class-capable.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP Query
Parse and access XML documents as arrays
Author: By
Last change:
Date: 8 years ago
Size: 3,768 bytes
 

Contents

Class file image Download
<?php
/**
 * This file contains the DomNodeClassCapable trait.
 *
 * PHP Version 5.3
 *
 * @category DOM
 * @package Dom
 * @author Gonzalo Chumillas <gonzalo@soloproyectos.com>
 * @license https://raw.github.com/soloproyectos/core/master/LICENSE BSD 2-Clause License
 * @link https://github.com/soloproyectos/core
 */
namespace com\soloproyectos\common\dom;

/**
 * DomNodeClassCapable trait.
 *
 * @category DOM
 * @package Dom
 * @author Gonzalo Chumillas <gonzalo@soloproyectos.com>
 * @license https://raw.github.com/soloproyectos/core/master/LICENSE BSD 2-Clause License
 * @link https://github.com/soloproyectos/core
 */
trait DomNodeClassCapable
{
   
/**
     * List of elements.
     *
     * @return array of DOMElement
     */
   
abstract public function elements();
   
   
/**
     * Adds a class name.
     *
     * @param string $className Class name
     *
     * @return DomNode
     */
   
public function addClass($className)
    {
       
$className = trim($className);
       
        if (
strlen($className) > 0) {
            foreach (
$this->elements() as $element) {
               
$classes = $this->_getClassMap($element);
               
                if (
array_search($className, $classes) === false) {
                   
array_push($classes, $className);
                   
$this->_setClassMap($element, $classes);
                }
            }
        }
       
        return
$this;
    }
   
   
/**
     * Removes a class name.
     *
     * @param string $className Class name
     *
     * @return DomNode
     */
   
public function removeClass($className)
    {
       
$className = trim($className);
       
        if (
strlen($className) > 0) {
            foreach (
$this->elements() as $element) {
               
$classes = $this->_getClassMap($element);
               
$offset = array_search($className, $classes);
               
                if (
$offset !== false) {
                   
array_splice($classes, $offset, 1);
                }
               
               
$this->_setClassMap($element, $classes);
            }
        }
       
        return
$this;
    }
   
   
/**
     * Has the node a class name?
     *
     * @param string $className Class name
     *
     * @return boolean
     */
   
public function hasClass($className)
    {
       
$className = trim($className);
       
        if (
strlen($className) > 0) {
            foreach (
$this->elements() as $element) {
               
$classes = $this->_getClassMap($element);
               
                return
array_search($className, $classes) !== false;
            }
        }
       
        return
false;
    }
   
   
/**
     * Gets the list of classes.
     *
     * @param DOMElement $element DOM element
     *
     * @return array of string
     */
   
private function _getClassMap($element)
    {
       
$ret = array();
       
$classAttr = $element->getAttribute("class");
       
        if (
strlen($classAttr) > 0) {
           
$classes = preg_split("/\s+/", $classAttr);
           
            foreach (
$classes as $className) {
                if (
strlen($className) > 0 && array_search($className, $ret) === false) {
                   
array_push($ret, $className);
                }
            }
        }
       
        return
$ret;
    }
   
   
/**
     * Sets the list of classes.
     *
     * @param DOMElement $element DOM element
     * @param array $classes List of classes
     *
     * @return void
     */
   
private function _setClassMap($element, $classes)
    {
       
$element->removeAttribute("class");
       
        if (
count($classes) > 0) {
           
$element->setAttribute("class", implode(" ", $classes));
        }
    }
}