-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Sheets.php
64 lines (50 loc) · 1.64 KB
/
Sheets.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace Spatie\Sheets;
use Illuminate\Support\Collection;
use RuntimeException;
class Sheets implements Repository
{
/** @var \Spatie\Sheets\Repository[] */
protected $collections;
/** @var string|null */
protected $defaultCollection;
public function collection(string $name): Repository
{
if (! isset($this->collections[$name])) {
throw new RuntimeException("Collection `{$name}` doesn't exist");
}
return $this->collections[$name];
}
public function registerCollection(string $name, Repository $repository)
{
$this->collections[$name] = $repository;
}
public function get(string $path): ?Sheet
{
return $this->defaultCollection()->get($path);
}
public function all(): Collection
{
return $this->defaultCollection()->all();
}
public function setDefaultCollection(string $defaultCollection)
{
if (! isset($this->collections[$defaultCollection])) {
throw new RuntimeException("Can't set default collection `{$defaultCollection}` because it isn't registered.");
}
$this->defaultCollection = $defaultCollection;
}
public function getRegisteredCollectionNames(): array
{
return array_keys($this->collections);
}
protected function defaultCollection(): Repository
{
if (empty($this->collections)) {
throw new RuntimeException("Can't retrieve a default collection if no collections are registered.");
}
return $this->collection(
$this->defaultCollection ?? $this->getRegisteredCollectionNames()[0]
);
}
}