4 * Contains \Drupal\bootstrap\Utility\Unicode.
7 namespace Drupal\bootstrap\Utility;
9 use Drupal\bootstrap\Bootstrap;
10 use Drupal\Component\Utility\Xss;
13 * Extends \Drupal\Component\Utility\Unicode.
17 class Unicode extends \Drupal\Component\Utility\Unicode {
20 * Extracts the hook name from a function name.
22 * @param string $string
23 * The function name to extract the hook name from.
24 * @param string $suffix
25 * A suffix hook ending (like "alter") to also remove.
26 * @param string $prefix
27 * A prefix hook beginning (like "form") to also remove.
30 * The extracted hook name.
32 public static function extractHook($string, $suffix = NULL, $prefix = NULL) {
33 $regex = '^(' . implode('|', array_keys(Bootstrap::getTheme()->getAncestry())) . ')';
34 $regex .= $prefix ? '_' . $prefix : '';
35 $regex .= $suffix ? '_|_' . $suffix . '$' : '';
36 return preg_replace("/$regex/", '', $string);
40 * Converts a callback to a string representation.
42 * @param array|string $callback
43 * The callback to convert.
45 * Flag determining whether or not to convert the callback to an array.
48 * The converted callback as a string or an array if $array is specified.
50 * @see \Drupal\bootstrap\Bootstrap::addCallback()
52 public static function convertCallback($callback, $array = FALSE) {
53 if (is_array($callback)) {
54 if (is_object($callback[0])) {
55 $callback[0] = get_class($callback[0]);
57 $callback = implode('::', $callback);
59 if ($callback[0] === '\\') {
60 $callback = self::substr($callback, 1);
62 if ($array && self::strpos($callback, '::') !== FALSE) {
63 $callback = explode('::', $callback);
69 * Determines if a string of text is considered "simple".
71 * @param string $string
72 * The string of text to check "simple" criteria on.
73 * @param int|FALSE $length
74 * The length of characters used to determine whether or not $string is
75 * considered "simple". Set explicitly to FALSE to disable this criteria.
76 * @param array|FALSE $allowed_tags
77 * An array of allowed tag elements. Set explicitly to FALSE to disable this
80 * A variable, passed by reference, that indicates whether or not the
81 * string contains HTML.
84 * Returns TRUE if the $string is considered "simple", FALSE otherwise.
86 public static function isSimple($string, $length = 250, $allowed_tags = NULL, &$html = FALSE) {
87 // Typecast to a string (if an object).
88 $string_clone = (string) $string;
90 // Use the advanced drupal_static() pattern.
91 static $drupal_static_fast;
92 if (!isset($drupal_static_fast)) {
93 $drupal_static_fast['strings'] = &drupal_static(__METHOD__);
95 $strings = &$drupal_static_fast['strings'];
96 if (!isset($strings[$string_clone])) {
97 $plain_string = strip_tags($string_clone);
99 if ($allowed_tags !== FALSE) {
100 $filtered_string = Xss::filter($string_clone, $allowed_tags);
101 $html = $filtered_string !== $plain_string;
102 $simple = $simple && $string_clone === $filtered_string;
104 if ($length !== FALSE) {
105 $simple = $simple && strlen($plain_string) <= intval($length);
107 $strings[$string_clone] = $simple;
109 return $strings[$string_clone];