Constraints   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 117
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0
wmc 26
lcom 0
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B filterNode() 0 18 6
A assertNode() 0 11 3
A filterNodeList() 0 7 4
B filterCallable() 0 15 7
B filterCallableArray() 0 8 5
A hasOption() 0 3 1
1
<?php
2
/**
3
 * Abstract utility class that provides several constraints/validations
4
 *
5
 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
6
 * @copyright Copyright (c) 2009-2017 FluentDOM Contributors
7
 */
8
9
namespace FluentDOM\Utility {
10
11
  /**
12
   * Abstract utility class that provides several constraints/validations
13
   */
14
  abstract class Constraints {
15
16
17
    /**
18
     * Check if the DOMNode is DOMElement or DOMText with content.
19
     * It returns the node or NULL.
20
     *
21
     * @param mixed $node
22
     * @param bool $ignoreTextNodes
23
     * @return \DOMElement|\DOMText|\DOMCdataSection
24
     */
25 7
    public static function filterNode($node, $ignoreTextNodes = FALSE) {
26
      if (
27 7
        $node instanceof \DOMElement ||
28
        (
29 5
          !$ignoreTextNodes &&
30
          (
31 3
            $node instanceof \DOMCdataSection ||
32
            (
33 2
              $node instanceof \DOMText &&
34 1
              !$node->isWhitespaceInElementContent()
35
            )
36
          )
37
        )
38
      ) {
39 4
        return $node;
40
      }
41 3
      return NULL;
42
    }
43
44
    /**
45
     * @param mixed $node
46
     * @param string $message
47
     * @return bool
48
     * @throws \InvalidArgumentException
49
     */
50 5
    public static function assertNode($node, $message = 'DOMNode expected, got: %s.'): bool {
51 5
      if (!($node instanceof \DOMNode)) {
52 2
        throw new \InvalidArgumentException(
53 2
          sprintf(
54 2
            $message,
55 2
            is_object($node) ? get_class($node) : gettype($node)
56
          )
57
        );
58
      }
59 3
      return TRUE;
60
    }
61
62
    /**
63
     * Check if $elements is a traversable node list. It returns
64
     * the $elements or NULL
65
     *
66
     * @param mixed $elements
67
     * @return \Traversable|array
68
     */
69 4
    public static function filterNodeList($elements) {
70 4
      if ($elements instanceof \Traversable ||
71 3
          is_array($elements)) {
72 2
        return empty($elements) ? new \EmptyIterator() : $elements;
73
      }
74 2
      return NULL;
75
    }
76
77
    /**
78
     * check if parameter is a valid callback function. It returns
79
     * the callable or NULL.
80
     *
81
     * If $silent is disabled, an exception is thrown for invalid callbacks
82
     *
83
     * @param mixed $callback
84
     * @param bool $allowGlobalFunctions
85
     * @param bool $silent (no InvalidArgumentException)
86
     * @throws \InvalidArgumentException
87
     * @return callable|NULL
88
     */
89 9
    public static function filterCallable($callback, $allowGlobalFunctions = FALSE, $silent = TRUE) {
90 9
      if ($callback instanceof \Closure) {
91 1
        return $callback;
92
      }
93
      if (
94 8
        (is_string($callback) && $allowGlobalFunctions) ||
95 7
        self::filterCallableArray($callback)
96
      ) {
97 2
        return is_callable($callback) ? $callback : NULL;
98
      }
99 6
      if ($silent) {
100 5
        return NULL;
101
      }
102 1
      throw new \InvalidArgumentException('Invalid callback argument');
103
    }
104
105
    /**
106
     * Return TRUE if the $callback is an array that can be an
107
     *
108
     * @param mixed $callback
109
     * @return bool
110
     */
111 5
    private static function filterCallableArray($callback) {
112
      return (
113 5
        is_array($callback) &&
114 3
        count($callback) === 2 &&
115 1
        (is_object($callback[0]) || is_string($callback[0])) &&
116 5
        is_string($callback[1])
117
      );
118
    }
119
120
    /**
121
     * Check options bitmask for an option
122
     *
123
     * @param int $options
124
     * @param int $option
125
     * @return bool
126
     */
127 2
    public static function hasOption($options, $option): bool {
128 2
      return ($options & $option) === $option;
129
    }
130
  }
131
}