685476e7890504db7a46f34ba9b66f2546b7ef2c
[yaffs-website] / web / themes / contrib / bootstrap / src / Utility / Unicode.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\bootstrap\Utility\Unicode.
5  */
6
7 namespace Drupal\bootstrap\Utility;
8
9 use Drupal\bootstrap\Bootstrap;
10 use Drupal\Component\Utility\Xss;
11
12 /**
13  * Extends \Drupal\Component\Utility\Unicode.
14  *
15  * @ingroup utility
16  */
17 class Unicode extends \Drupal\Component\Utility\Unicode {
18
19   /**
20    * Extracts the hook name from a function name.
21    *
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.
28    *
29    * @return string
30    *   The extracted hook name.
31    */
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);
37   }
38
39   /**
40    * Converts a callback to a string representation.
41    *
42    * @param array|string $callback
43    *   The callback to convert.
44    * @param bool $array
45    *   Flag determining whether or not to convert the callback to an array.
46    *
47    * @return string
48    *   The converted callback as a string or an array if $array is specified.
49    *
50    * @see \Drupal\bootstrap\Bootstrap::addCallback()
51    */
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]);
56       }
57       $callback = implode('::', $callback);
58     }
59     if ($callback[0] === '\\') {
60       $callback = self::substr($callback, 1);
61     }
62     if ($array && self::strpos($callback, '::') !== FALSE) {
63       $callback = explode('::', $callback);
64     }
65     return $callback;
66   }
67
68   /**
69    * Determines if a string of text is considered "simple".
70    *
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
78    *   criteria.
79    * @param bool $html
80    *   A variable, passed by reference, that indicates whether or not the
81    *   string contains HTML.
82    *
83    * @return bool
84    *   Returns TRUE if the $string is considered "simple", FALSE otherwise.
85    */
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;
89
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__);
94     }
95     $strings = &$drupal_static_fast['strings'];
96     if (!isset($strings[$string_clone])) {
97       $plain_string = strip_tags($string_clone);
98       $simple = TRUE;
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;
103       }
104       if ($length !== FALSE) {
105         $simple = $simple && strlen($plain_string) <= intval($length);
106       }
107       $strings[$string_clone] = $simple;
108     }
109     return $strings[$string_clone];
110   }
111
112 }