|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Psi\Component\ResourceBrowser; |
|
6
|
|
|
|
|
7
|
|
|
use Psi\Component\ResourceBrowser\Filter\Filter\PassThroughFilter; |
|
8
|
|
|
use Psi\Component\ResourceBrowser\Filter\FilterInterface; |
|
9
|
|
|
use Puli\Repository\Api\ResourceRepository; |
|
10
|
|
|
|
|
11
|
|
|
final class Browser |
|
12
|
|
|
{ |
|
13
|
|
|
private $repository; |
|
14
|
|
|
private $nbColumns; |
|
15
|
|
|
private $path; |
|
16
|
|
|
private $filter; |
|
17
|
|
|
|
|
18
|
|
|
private function __construct(ResourceRepository $repository, FilterInterface $filter, string $path, int $nbColumns) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->repository = $repository; |
|
21
|
|
|
$this->nbColumns = $nbColumns; |
|
22
|
|
|
$this->path = $path; |
|
23
|
|
|
$this->filter = $filter; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public static function createFromOptions(ResourceRepository $repository, array $options = []) |
|
27
|
|
|
{ |
|
28
|
|
|
$defaultOptions = [ |
|
29
|
|
|
'path' => '/', |
|
30
|
|
|
'nb_columns' => 4, |
|
31
|
|
|
'filter' => new PassThroughFilter(), |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
if ($diff = array_diff(array_keys($options), array_keys($defaultOptions))) { |
|
35
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
36
|
|
|
'Invalid options: "%s". Valid options: "%s"', |
|
37
|
|
|
implode('", "', $diff), |
|
38
|
|
|
implode('", "', array_keys($defaultOptions)) |
|
39
|
|
|
)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$options = array_merge($defaultOptions, $options); |
|
43
|
|
|
|
|
44
|
|
|
$browser = new self( |
|
45
|
|
|
$repository, |
|
46
|
|
|
$options['filter'], |
|
47
|
|
|
$options['path'], |
|
48
|
|
|
$options['nb_columns'] |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
return $browser; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function getColumns() |
|
55
|
|
|
{ |
|
56
|
|
|
$columnPaths = $this->getColumnPaths($this->path); |
|
57
|
|
|
|
|
58
|
|
|
return $this->getColumnsForPaths($columnPaths); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getColumnsForDisplay() |
|
62
|
|
|
{ |
|
63
|
|
|
$columnPaths = $this->getColumnPaths($this->path); |
|
64
|
|
|
|
|
65
|
|
|
if (count($columnPaths) > $this->nbColumns) { |
|
66
|
|
|
$columnPaths = array_slice($columnPaths, -$this->nbColumns); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $this->getColumnsForPaths($columnPaths); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getCurrentColumn() |
|
73
|
|
|
{ |
|
74
|
|
|
return new Column($this->repository->get($this->path), $this->filter); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getPath() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->path; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function getMaxColumns() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->nbColumns; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
private function getColumnsForPaths(array $columnPaths) |
|
88
|
|
|
{ |
|
89
|
|
|
$columns = []; |
|
90
|
|
|
|
|
91
|
|
|
foreach ($columnPaths as $columnPath) { |
|
92
|
|
|
$resource = $this->repository->get($columnPath); |
|
93
|
|
|
$columns[] = new Column($resource, $this->filter); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $columns; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
private function getColumnPaths($path) |
|
100
|
|
|
{ |
|
101
|
|
|
$columnNames = ['/']; |
|
102
|
|
|
|
|
103
|
|
|
if ($path !== '/') { |
|
104
|
|
|
$columnNames = explode('/', ltrim($path, '/')); |
|
105
|
|
|
array_unshift($columnNames, '/'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
$columnPaths = []; |
|
110
|
|
|
|
|
111
|
|
|
foreach ($columnNames as $columnName) { |
|
112
|
|
|
if ($columnName !== '/') { |
|
113
|
|
|
$elements[] = $columnName; |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$columnPaths[] = empty($elements) ? '/' : '/' . implode('/', $elements); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $columnPaths; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.