summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorArthur Schiwon <blizzz@owncloud.com>2014-01-15 17:55:05 +0100
committerArthur Schiwon <blizzz@owncloud.com>2014-02-11 10:49:34 +0100
commit41e8d44cf7261e05a40842ab56d4d4c3f61cf083 (patch)
treeffdeaa9831c864528e8e4c482abe13e8134c26c7 /lib
parent1d0a2365631955944d746b4567cac85eb10a80db (diff)
downloadnextcloud-server-41e8d44cf7261e05a40842ab56d4d4c3f61cf083.tar.gz
nextcloud-server-41e8d44cf7261e05a40842ab56d4d4c3f61cf083.zip
move sorter into a class
Diffstat (limited to 'lib')
-rw-r--r--lib/private/share/searchresultsorter.php53
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/private/share/searchresultsorter.php b/lib/private/share/searchresultsorter.php
new file mode 100644
index 00000000000..27f94a694ac
--- /dev/null
+++ b/lib/private/share/searchresultsorter.php
@@ -0,0 +1,53 @@
+<?php
+/**
+ * Copyright (c) 2014 Arthur Schiwon <blizzz@owncloud.bzoc>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ *
+ */
+namespace OC\Share;
+
+class SearchResultSorter {
+ private $search;
+ private $encoding;
+ private $key;
+
+ /**
+ * @param $search the search term as was given by the user
+ * @param $key the array key containing the value that should be compared
+ * against
+ * @param $encoding optional, encoding to use, defaults to UTF-8
+ */
+ public function __construct($search, $key, $encoding = 'UTF-8') {
+ $this->encoding = $encoding;
+ $this->key = $key;
+ $this->search = mb_strtolower($search, $this->encoding);
+ }
+
+ /**
+ * User and Group names matching the search term at the beginning shall appear
+ * on top of the share dialog.
+ * Callback function for usort. http://php.net/usort
+ */
+ public function sort($a, $b) {
+ if(!isset($a[$this->key]) || !isset($b[$this->key])) {
+ \OCP\Util::writeLog('core', 'Sharing: cannot sort due to missing'.
+ 'array key', \OC_Log::ERROR);
+ return 0;
+ }
+ $nameA = mb_strtolower($a[$this->key], $this->encoding);
+ $nameB = mb_strtolower($b[$this->key], $this->encoding);
+ $i = mb_strpos($nameA, $this->search, 0, $this->encoding);
+ $j = mb_strpos($nameB, $this->search, 0, $this->encoding);
+
+ if($i === $j) {
+ return 0;
+ } elseif ($i === 0) {
+ return -1;
+ } else {
+ return 1;
+ }
+ }
+}
+