1 | <?php |
||
2 | |||
3 | namespace Astrotomic\Stancy\Factories; |
||
4 | |||
5 | use Astrotomic\Stancy\Contracts\Page as PageContract; |
||
6 | use Astrotomic\Stancy\Contracts\PageFactory as PageFactoryContract; |
||
7 | use Astrotomic\Stancy\Exceptions\SheetCollectionNotFoundException; |
||
8 | use Astrotomic\Stancy\Exceptions\SheetNotFoundException; |
||
9 | use RuntimeException; |
||
10 | use Spatie\Sheets\Facades\Sheets; |
||
11 | use Spatie\Sheets\Sheet; |
||
12 | |||
13 | class PageFactory implements PageFactoryContract |
||
14 | { |
||
15 | 25 | public function make(array $data = [], ?string $page = null): PageContract |
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
16 | { |
||
17 | 25 | return app(PageContract::class, [ |
|
18 | 25 | 'data' => $data, |
|
19 | 25 | 'page' => $page, |
|
20 | ]); |
||
21 | } |
||
22 | |||
23 | 17 | public function makeFromSheet(Sheet $sheet, ?string $page = null): PageContract |
|
24 | { |
||
25 | 17 | return static::make($sheet->toArray(), $page); |
|
26 | } |
||
27 | |||
28 | 16 | public function makeFromSheetName(string $collection, string $name, ?string $page = null): PageContract |
|
29 | { |
||
30 | try { |
||
31 | 16 | $sheet = Sheets::collection($collection)->get($name); |
|
32 | 1 | } catch (RuntimeException $exception) { |
|
33 | 1 | throw SheetCollectionNotFoundException::make($collection, $exception); |
|
34 | } |
||
35 | |||
36 | 15 | if ($sheet === null) { |
|
37 | 1 | throw SheetNotFoundException::make($collection, $name); |
|
38 | } |
||
39 | |||
40 | 14 | return static::makeFromSheet($sheet, $page); |
|
41 | } |
||
42 | } |
||
43 |