Skip to content

Commit 9e77874

Browse files
committed
refactor: simplify filtering logic
1 parent 6575a7b commit 9e77874

File tree

2 files changed

+9
-27
lines changed

2 files changed

+9
-27
lines changed

resources/views/index.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
</ol>
8787
</nav>
8888

89-
<div>
89+
<div style="margin-bottom: 16px;">
9090
<input type="text" name="keyword" id="keyword" placeholder="keyword">
9191
<button type="button" id="keyword-button" >Search</button>
9292
<button type="button" id="keyword-reset-button" >Reset</button>

src/Controllers/ItemsController.php

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,24 @@ class ItemsController extends LfmController
1515
*
1616
* @return mixed
1717
*/
18-
public function getItems(Request $request)
18+
public function getItems()
1919
{
2020
$currentPage = self::getCurrentPageFromRequest();
2121

2222
$perPage = $this->helper->getPaginationPerPage();
2323
$items = array_merge($this->lfm->folders(), $this->lfm->files());
2424

25-
$items = array_map(function ($item) {
26-
return $item->fill()->attributes;
27-
}, $items);
28-
29-
$keyword = $request->keyword;
25+
$keyword = request()->get('keyword', "");
3026
if (!empty($keyword)) {
31-
$items = array_values(array_filter($items, function ($item) use ($keyword) {
32-
if ($this->like_match("%".$keyword."%", $item['name'])) {
33-
return true;
34-
} else {
35-
return false;
36-
}
37-
}));
27+
$items = array_filter($items, function ($item) use ($keyword) {
28+
return str_contains($item->name, $keyword);
29+
});
3830
}
3931

4032
return [
41-
'items' => array_slice($items, ($currentPage - 1) * $perPage, $perPage),
33+
'items' => array_map(function ($item) {
34+
return $item->fill()->attributes;
35+
}, array_slice($items, ($currentPage - 1) * $perPage, $perPage)),
4236
'paginator' => [
4337
'current_page' => $currentPage,
4438
'total' => count($items),
@@ -49,12 +43,6 @@ public function getItems(Request $request)
4943
];
5044
}
5145

52-
public function like_match($pattern, $subject)
53-
{
54-
$pattern = str_replace('%', '.*', preg_quote($pattern, '/'));
55-
return (bool) preg_match("/^{$pattern}$/i", $subject);
56-
}
57-
5846
public function move()
5947
{
6048
$items = request('items');
@@ -122,10 +110,4 @@ private static function getCurrentPageFromRequest()
122110

123111
return $currentPage;
124112
}
125-
126-
private static function getKeywordFromRequest()
127-
{
128-
$keyword = request()->get('keyword', "");
129-
return $keyword;
130-
}
131113
}

0 commit comments

Comments
 (0)