3 namespace Drupal\bootstrap\Utility;
5 use Drupal\bootstrap\Bootstrap;
6 use Drupal\Component\Utility\Unicode as CoreUnicode;
7 use Drupal\Component\Utility\Xss;
10 * Extends \Drupal\Component\Utility\Unicode.
14 class Unicode extends CoreUnicode {
17 * Extracts the hook name from a function name.
19 * @param string $string
20 * The function name to extract the hook name from.
21 * @param string $suffix
22 * A suffix hook ending (like "alter") to also remove.
23 * @param string $prefix
24 * A prefix hook beginning (like "form") to also remove.
27 * The extracted hook name.
29 public static function extractHook($string, $suffix = NULL, $prefix = NULL) {
30 $regex = '^(' . implode('|', array_keys(Bootstrap::getTheme()->getAncestry())) . ')';
31 $regex .= $prefix ? '_' . $prefix : '';
32 $regex .= $suffix ? '_|_' . $suffix . '$' : '';
33 return preg_replace("/$regex/", '', $string);
37 * Converts a callback to a string representation.
39 * @param array|string $callback
40 * The callback to convert.
42 * Flag determining whether or not to convert the callback to an array.
45 * The converted callback as a string or an array if $array is specified.
47 * @see \Drupal\bootstrap\Bootstrap::addCallback()
49 public static function convertCallback($callback, $array = FALSE) {
50 if (is_array($callback)) {
51 if (is_object($callback[0])) {
52 $callback[0] = get_class($callback[0]);
54 $callback = implode('::', $callback);
56 if ($callback[0] === '\\') {
57 $callback = self::substr($callback, 1);
59 if ($array && self::strpos($callback, '::') !== FALSE) {
60 $callback = explode('::', $callback);
66 * Determines if a string of text is considered "simple".
68 * @param string $string
69 * The string of text to check "simple" criteria on.
70 * @param int|false $length
71 * The length of characters used to determine whether or not $string is
72 * considered "simple". Set explicitly to FALSE to disable this criteria.
73 * @param array|false $allowed_tags
74 * An array of allowed tag elements. Set explicitly to FALSE to disable this
77 * A variable, passed by reference, that indicates whether or not the
78 * string contains HTML.
81 * Returns TRUE if the $string is considered "simple", FALSE otherwise.
83 public static function isSimple($string, $length = 250, $allowed_tags = NULL, &$html = FALSE) {
84 // Typecast to a string (if an object).
85 $string_clone = (string) $string;
87 // Use the advanced drupal_static() pattern.
88 static $drupal_static_fast;
89 if (!isset($drupal_static_fast)) {
90 $drupal_static_fast['strings'] = &drupal_static(__METHOD__);
92 $strings = &$drupal_static_fast['strings'];
93 if (!isset($strings[$string_clone])) {
94 $plain_string = strip_tags($string_clone);
96 if ($allowed_tags !== FALSE) {
97 $filtered_string = Xss::filter($string_clone, $allowed_tags);
98 $html = $filtered_string !== $plain_string;
99 $simple = $simple && $string_clone === $filtered_string;
101 if ($length !== FALSE) {
102 $simple = $simple && strlen($plain_string) <= intval($length);
104 $strings[$string_clone] = $simple;
106 return $strings[$string_clone];