PluginProbe
WPIDE – File Manager & Code Editor / 3.5.5
WPIDE – File Manager & Code Editor v3.5.5
3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 3.4 All 54 releases
wpide / App / Utils / Collection.php

Collection.php in WPIDE – File Manager & Code Editor 3.5.5, at App/Utils/Collection.php

85 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPIDE\App\Utils;
4
5 use WPIDE\App\Services\Storage\DirectoryCollection;
6
7 trait Collection
8 {
9 protected $items = [];
10
11 public function add($obj)
12 {
13 return $this->items[] = $obj;
14 }
15
16 public function delete($obj)
17 {
18 foreach ($this->items as $key => $item) {
19 if ($item === $obj) {
20 unset($this->items[$key]);
21 }
22 }
23 }
24
25 public function all(): array
26 {
27 return $this->items;
28 }
29
30 public function get($id, $key = 'id'): array
31 {
32 return array_filter( $this->items, function($item) use (&$id, &$key) {
33 return $item[$key] == $id;
34 });
35 }
36
37 public function length(): int
38 {
39 return count($this->items);
40 }
41
42 public function jsonSerialize(): array
43 {
44 return $this->items;
45 }
46
47 public function filter(callable $callback): DirectoryCollection
48 {
49 $this->items = array_filter($this->items, $callback);
50
51 return $this;
52 }
53
54 public function map(callable $callback): DirectoryCollection
55 {
56 $this->items = array_map($callback, $this->items);
57
58 return $this;
59 }
60
61 public function merge($collection): DirectoryCollection
62 {
63
64 $this->items = array_merge($this->items, $collection->items);
65
66 return $this;
67 }
68
69 public function sortByValue($value, $desc = false): DirectoryCollection
70 {
71
72 usort($this->items, function ($a, $b) use ($value) {
73 return
74 ($a['type'] <=> $b['type']) * 1000 +
75 ($a[$value] <=> $b[$value]);
76 });
77
78 if ($desc) {
79 $this->items = array_reverse($this->items);
80 }
81
82 return $this;
83 }
84 }
85