From bfc9efea927040f1893aea37234fd20d1431e096 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Mon, 3 Aug 2015 16:11:02 -0600 Subject: [PATCH] added new arrayFilterRecursive --- system/src/Grav/Common/Utils.php | 95 ++++++++++++++++++++++---------- 1 file changed, 66 insertions(+), 29 deletions(-) diff --git a/system/src/Grav/Common/Utils.php b/system/src/Grav/Common/Utils.php index 7262c498c..5c38f97a9 100644 --- a/system/src/Grav/Common/Utils.php +++ b/system/src/Grav/Common/Utils.php @@ -13,8 +13,9 @@ abstract class Utils use GravTrait; /** - * @param string $haystack - * @param string $needle + * @param string $haystack + * @param string $needle + * * @return bool */ public static function startsWith($haystack, $needle) @@ -27,14 +28,17 @@ abstract class Utils return $status; } } + return $status; } + return $needle === '' || strpos($haystack, $needle) === 0; } /** - * @param string $haystack - * @param string $needle + * @param string $haystack + * @param string $needle + * * @return bool */ public static function endsWith($haystack, $needle) @@ -47,14 +51,17 @@ abstract class Utils return $status; } } + return $status; } + return $needle === '' || substr($haystack, -strlen($needle)) === $needle; } /** - * @param string $haystack - * @param string $needle + * @param string $haystack + * @param string $needle + * * @return bool */ public static function contains($haystack, $needle) @@ -67,11 +74,12 @@ abstract class Utils * * @param object $obj1 * @param object $obj2 + * * @return object */ public static function mergeObjects($obj1, $obj2) { - return (object) array_merge((array) $obj1, (array) $obj2); + return (object)array_merge((array)$obj1, (array)$obj2); } /** @@ -82,6 +90,7 @@ abstract class Utils * @param string $ending * @param bool $exact * @param bool $considerHtml + * * @return string */ public static function truncateHtml($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) @@ -100,34 +109,41 @@ abstract class Utils // if there is any html-tag in this line, handle it and add it (uncounted) to the output if (!empty($line_matchings[1])) { // if it's an "empty element" with or without xhtml-conform closing slash - if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) { + if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', + $line_matchings[1])) { // do nothing - // if tag is a closing tag - } else if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) { - // delete tag from $open_tags list - $pos = array_search($tag_matchings[1], $open_tags); - if ($pos !== false) { - unset($open_tags[$pos]); + // if tag is a closing tag + } else { + if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) { + // delete tag from $open_tags list + $pos = array_search($tag_matchings[1], $open_tags); + if ($pos !== false) { + unset($open_tags[$pos]); + } + // if tag is an opening tag + } else { + if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) { + // add tag to the beginning of $open_tags list + array_unshift($open_tags, strtolower($tag_matchings[1])); + } } - // if tag is an opening tag - } else if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) { - // add tag to the beginning of $open_tags list - array_unshift($open_tags, strtolower($tag_matchings[1])); } // add html-tag to $truncate'd text $truncate .= $line_matchings[1]; } // calculate the length of the plain text part of the line; handle entities as one character - $content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2])); - if ($total_length+$content_length> $length) { + $content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', + $line_matchings[2])); + if ($total_length + $content_length > $length) { // the number of characters which are left $left = $length - $total_length; $entities_length = 0; // search for html entities - if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) { + if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, + PREG_OFFSET_CAPTURE)) { // calculate the real length of all entities in the legal range foreach ($entities[0] as $entity) { - if ($entity[1]+1-$entities_length <= $left) { + if ($entity[1] + 1 - $entities_length <= $left) { $left--; $entities_length += strlen($entity[0]); } else { @@ -136,7 +152,7 @@ abstract class Utils } } } - $truncate .= substr($line_matchings[2], 0, $left+$entities_length); + $truncate .= substr($line_matchings[2], 0, $left + $entities_length); // maximum length is reached, so get off the loop break; } else { @@ -172,6 +188,7 @@ abstract class Utils $truncate .= ''; } } + return $truncate; } @@ -208,7 +225,7 @@ abstract class Utils if ($force_download) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); - header('Content-Disposition: attachment; filename='.$file_parts['basename']); + header('Content-Disposition: attachment; filename=' . $file_parts['basename']); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); @@ -243,14 +260,14 @@ abstract class Utils * Return the mimetype based on filename * * @param $extension Extension of file (eg .txt) + * * @return string */ public static function getMimeType($extension) { $extension = strtolower($extension); - switch($extension) - { + switch ($extension) { case "js": return "application/x-javascript"; @@ -348,6 +365,7 @@ abstract class Utils * Normalize path by processing relative `.` and `..` syntax and merging path * * @param $path + * * @return string */ public static function normalizePath($path) @@ -366,6 +384,7 @@ abstract class Utils array_push($ret, $segment); } } + return $root . implode('/', $ret); } @@ -383,10 +402,9 @@ abstract class Utils asort($offsets); $timezone_list = array(); - foreach( $offsets as $timezone => $offset ) - { + foreach ($offsets as $timezone => $offset) { $offset_prefix = $offset < 0 ? '-' : '+'; - $offset_formatted = gmdate( 'H:i', abs($offset) ); + $offset_formatted = gmdate('H:i', abs($offset)); $pretty_offset = "UTC${offset_prefix}${offset_formatted}"; @@ -396,4 +414,23 @@ abstract class Utils return $timezone_list; } + + public static function arrayFilterRecursive(Array $source, $fn) + { + $result = array(); + foreach ($source as $key => $value) + { + if (is_array($value)) + { + $result[$key] = static::arrayFilterRecursive($value, $fn); + continue; + } + if ($fn($key, $value)) + { + $result[$key] = $value; // KEEP + continue; + } + } + return $result; + } }