isCallable() public static méthode

If $silent is disabled, an exception is thrown for invalid callbacks
public static isCallable ( mixed $callback, boolean $allowGlobalFunctions = FALSE, boolean $silent = TRUE ) : callable | null
$callback mixed
$allowGlobalFunctions boolean
$silent boolean (no InvalidArgumentException)
Résultat callable | null
Exemple #1
0
 /**
  * @param mixed $content
  * @param bool $includeTextNodes
  * @param int $limit
  * @return array|\Traversable null
  */
 private function getNodeList($content, $includeTextNodes = TRUE, $limit = -1)
 {
     if ($callback = Constraints::isCallable($content)) {
         $content = $callback();
     }
     if ($content instanceof \DOMElement) {
         return array($content);
     } elseif ($includeTextNodes && Constraints::isNode($content)) {
         return array($content);
     } elseif (Constraints::isNodeList($content)) {
         return $this->getLimitedArray($content, $limit);
     }
     return NULL;
 }
Exemple #2
0
 /**
  * Execute a function within the context of every matched element.
  *
  * If $elementsOnly is a callable the return value defines if
  * it is called for that node.
  *
  * If $elementsOnly is set to TRUE, only element nodes are used.
  *
  * @param callable $function
  * @param callable|bool|NULL $elementsFilter
  * @return $this
  */
 public function each(callable $function, $elementsFilter = NULL)
 {
     if (TRUE === $elementsFilter) {
         $filter = function ($node) {
             return $node instanceof \DOMElement;
         };
     } else {
         $filter = Constraints::isCallable($elementsFilter);
     }
     foreach ($this->_nodes as $index => $node) {
         if (NULL === $filter || $filter($node, $index)) {
             call_user_func($function, $node, $index);
         }
     }
     return $this;
 }
Exemple #3
0
 /**
  * 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.
  *
  * @example toggleClass.php Usage Example: FluentDOM\Query::toggleClass()
  * @param string|callable $class
  * @param NULL|boolean $switch 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;
 }