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\Contracts\SitemapFactory as SitemapFactoryContract; |
||
8 | use Illuminate\Support\Str; |
||
9 | use Spatie\Sheets\Facades\Sheets; |
||
10 | use Spatie\Sheets\Sheet; |
||
11 | use Spatie\Sitemap\Sitemap; |
||
12 | |||
13 | class SitemapFactory implements SitemapFactoryContract |
||
14 | { |
||
15 | /** @var PageFactoryContract */ |
||
16 | protected $pageFactory; |
||
17 | |||
18 | 4 | public function __construct(PageFactoryContract $pageFactory) |
|
19 | { |
||
20 | 4 | $this->pageFactory = $pageFactory; |
|
21 | 4 | } |
|
22 | |||
23 | /** |
||
24 | * @param PageContract[] $pages |
||
25 | * |
||
26 | * @return Sitemap |
||
27 | */ |
||
28 | 2 | public function makeFromPages(array $pages): Sitemap |
|
29 | { |
||
30 | 2 | $sitemap = Sitemap::create(); |
|
31 | |||
32 | 2 | foreach($pages as $page) { |
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
33 | 2 | $sitemap->add($page->toSitemapItem()); |
|
34 | } |
||
35 | |||
36 | 2 | return $sitemap; |
|
37 | } |
||
38 | |||
39 | /** |
||
40 | * @param string[] $list |
||
41 | * |
||
42 | * @return Sitemap |
||
43 | */ |
||
44 | 2 | public function makeFromSheetList(array $list): Sitemap |
|
45 | { |
||
46 | 2 | $pages = []; |
|
47 | |||
48 | 2 | foreach($list as $entry) { |
|
0 ignored issues
–
show
|
|||
49 | 2 | if (Str::contains($entry, ':')) { |
|
50 | 1 | [$collection, $path] = explode(':', $entry); |
|
51 | |||
52 | 1 | $pages[] = $this->sheetToPage(Sheets::collection($collection)->get($path)); |
|
53 | |||
54 | 1 | continue; |
|
55 | } |
||
56 | |||
57 | 1 | $pages = array_merge($pages, $this->sheetsToPages(Sheets::collection($entry)->all()->all())); |
|
58 | } |
||
59 | |||
60 | 2 | return $this->makeFromPages($pages); |
|
61 | } |
||
62 | |||
63 | 1 | public function makeFromSheetCollectionName(string $name): Sitemap |
|
64 | { |
||
65 | 1 | return $this->makeFromSheetList([$name]); |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param Sheet[] $sheets |
||
70 | * |
||
71 | * @return PageContract[] |
||
72 | */ |
||
73 | 1 | protected function sheetsToPages(array $sheets): array |
|
74 | { |
||
75 | return array_map(function(Sheet $sheet): PageContract { |
||
0 ignored issues
–
show
|
|||
76 | 1 | return $this->sheetToPage($sheet); |
|
77 | 1 | }, $sheets); |
|
78 | } |
||
79 | |||
80 | 2 | protected function sheetToPage(Sheet $sheet): PageContract |
|
81 | { |
||
82 | 2 | return $this->pageFactory->makeFromSheet($sheet); |
|
83 | } |
||
84 | } |
||
85 |