ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ContentListingFormatter.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
11 {
15  private $directory;
19  private $recursive;
20 
25  public function __construct($directory, $recursive)
26  {
27  $this->directory = $directory;
28  $this->recursive = $recursive;
29  }
30 
38  public function formatListing(array $listing)
39  {
40  $listing = array_values(
41  array_map(
42  [$this, 'addPathInfo'],
43  array_filter($listing, [$this, 'isEntryOutOfScope'])
44  )
45  );
46 
47  return $this->sortListing($listing);
48  }
49 
50  private function addPathInfo(array $entry)
51  {
52  return $entry + Util::pathinfo($entry['path']);
53  }
54 
62  private function isEntryOutOfScope(array $entry)
63  {
64  if (empty($entry['path']) && $entry['path'] !== '0') {
65  return false;
66  }
67 
68  if ($this->recursive) {
69  return $this->residesInDirectory($entry);
70  }
71 
72  return $this->isDirectChild($entry);
73  }
74 
82  private function residesInDirectory(array $entry)
83  {
84  if ($this->directory === '') {
85  return true;
86  }
87 
88  return strpos($entry['path'], $this->directory . '/') === 0;
89  }
90 
98  private function isDirectChild(array $entry)
99  {
100  return Util::dirname($entry['path']) === $this->directory;
101  }
102 
108  private function sortListing(array $listing)
109  {
110  usort(
111  $listing,
112  function ($a, $b) {
113  return strcasecmp($a['path'], $b['path']);
114  }
115  );
116 
117  return $listing;
118  }
119 }
static dirname($path)
Get a normalized dirname from a path.
Definition: Util.php:45
residesInDirectory(array $entry)
Check if the entry resides within the parent directory.
isDirectChild(array $entry)
Check if the entry is a direct child of the directory.
formatListing(array $listing)
Format contents listing.
static pathinfo($path)
Get normalized pathinfo.
Definition: Util.php:17
isEntryOutOfScope(array $entry)
Determine if the entry is out of scope.