f0d507e3877ca96e2674903ab1c5e4e90bba5221
[yaffs-website] / web / themes / contrib / bootstrap / src / Utility / Unicode.php
1 <?php
2
3 namespace Drupal\bootstrap\Utility;
4
5 use Drupal\bootstrap\Bootstrap;
6 use Drupal\Component\Utility\Unicode as CoreUnicode;
7 use Drupal\Component\Utility\Xss;
8
9 /**
10  * Extends \Drupal\Component\Utility\Unicode.
11  *
12  * @ingroup utility
13  */
14 class Unicode extends CoreUnicode {
15
16   /**
17    * Extracts the hook name from a function name.
18    *
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.
25    *
26    * @return string
27    *   The extracted hook name.
28    */
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);
34   }
35
36   /**
37    * Converts a callback to a string representation.
38    *
39    * @param array|string $callback
40    *   The callback to convert.
41    * @param bool $array
42    *   Flag determining whether or not to convert the callback to an array.
43    *
44    * @return string
45    *   The converted callback as a string or an array if $array is specified.
46    *
47    * @see \Drupal\bootstrap\Bootstrap::addCallback()
48    */
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]);
53       }
54       $callback = implode('::', $callback);
55     }
56     if ($callback[0] === '\\') {
57       $callback = self::substr($callback, 1);
58     }
59     if ($array && self::strpos($callback, '::') !== FALSE) {
60       $callback = explode('::', $callback);
61     }
62     return $callback;
63   }
64
65   /**
66    * Determines if a string of text is considered "simple".
67    *
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
75    *   criteria.
76    * @param bool $html
77    *   A variable, passed by reference, that indicates whether or not the
78    *   string contains HTML.
79    *
80    * @return bool
81    *   Returns TRUE if the $string is considered "simple", FALSE otherwise.
82    */
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;
86
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__);
91     }
92     $strings = &$drupal_static_fast['strings'];
93     if (!isset($strings[$string_clone])) {
94       $plain_string = strip_tags($string_clone);
95       $simple = TRUE;
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;
100       }
101       if ($length !== FALSE) {
102         $simple = $simple && strlen($plain_string) <= intval($length);
103       }
104       $strings[$string_clone] = $simple;
105     }
106     return $strings[$string_clone];
107   }
108
109 }