PluginProbe
WPIDE – File Manager & Code Editor / 3.5.10
WPIDE – File Manager & Code Editor v3.5.10
3.5.10 3.5.9 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 All 56 releases
wpide / vendor / league / flysystem / src / Util / ContentListingFormatter.php

ContentListingFormatter.php in WPIDE – File Manager & Code Editor 3.5.10, at vendor/league/flysystem/src/Util/ContentListingFormatter.php

123 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace League\Flysystem\Util;
4
5 use League\Flysystem\Util;
6
7 /**
8 * @internal
9 */
10 class ContentListingFormatter
11 {
12 /**
13 * @var string
14 */
15 private $directory;
16
17 /**
18 * @var bool
19 */
20 private $recursive;
21
22 /**
23 * @var bool
24 */
25 private $caseSensitive;
26
27 /**
28 * @param string $directory
29 * @param bool $recursive
30 */
31 public function __construct($directory, $recursive, $caseSensitive = true)
32 {
33 $this->directory = rtrim($directory, '/');
34 $this->recursive = $recursive;
35 $this->caseSensitive = $caseSensitive;
36 }
37
38 /**
39 * Format contents listing.
40 *
41 * @param array $listing
42 *
43 * @return array
44 */
45 public function formatListing(array $listing)
46 {
47 $listing = array_filter(array_map([$this, 'addPathInfo'], $listing), [$this, 'isEntryOutOfScope']);
48
49 return $this->sortListing(array_values($listing));
50 }
51
52 private function addPathInfo(array $entry)
53 {
54 return $entry + Util::pathinfo($entry['path']);
55 }
56
57 /**
58 * Determine if the entry is out of scope.
59 *
60 * @param array $entry
61 *
62 * @return bool
63 */
64 private function isEntryOutOfScope(array $entry)
65 {
66 if (empty($entry['path']) && $entry['path'] !== '0') {
67 return false;
68 }
69
70 if ($this->recursive) {
71 return $this->residesInDirectory($entry);
72 }
73
74 return $this->isDirectChild($entry);
75 }
76
77 /**
78 * Check if the entry resides within the parent directory.
79 *
80 * @param array $entry
81 *
82 * @return bool
83 */
84 private function residesInDirectory(array $entry)
85 {
86 if ($this->directory === '') {
87 return true;
88 }
89
90 return $this->caseSensitive
91 ? strpos($entry['path'], $this->directory . '/') === 0
92 : stripos($entry['path'], $this->directory . '/') === 0;
93 }
94
95 /**
96 * Check if the entry is a direct child of the directory.
97 *
98 * @param array $entry
99 *
100 * @return bool
101 */
102 private function isDirectChild(array $entry)
103 {
104 return $this->caseSensitive
105 ? $entry['dirname'] === $this->directory
106 : strcasecmp($this->directory, $entry['dirname']) === 0;
107 }
108
109 /**
110 * @param array $listing
111 *
112 * @return array
113 */
114 private function sortListing(array $listing)
115 {
116 usort($listing, function ($a, $b) {
117 return strcasecmp($a['path'], $b['path']);
118 });
119
120 return $listing;
121 }
122 }
123