-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathCollection.php
More file actions
109 lines (95 loc) · 4.37 KB
/
Collection.php
File metadata and controls
109 lines (95 loc) · 4.37 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
namespace Statamic\Eloquent\Collections;
use Illuminate\Database\Eloquent\Model;
use Statamic\Contracts\Entries\Collection as Contract;
use Statamic\Eloquent\Structures\CollectionStructure;
use Statamic\Entries\Collection as FileEntry;
class Collection extends FileEntry
{
protected $model;
public static function fromModel(Model $model)
{
return (new static)
->title($model->title ?? null)
->routes($model->settings['routes'] ?? null)
->requiresSlugs($model->settings['slugs'] ?? true)
->titleFormats($model->settings['title_formats'] ?? null)
->mount($model->settings['mount'] ?? null)
->dated($model->settings['dated'] ?? null)
->sites($model->settings['sites'] ?? null)
->template($model->settings['template'] ?? null)
->layout($model->settings['layout'] ?? null)
->cascade($model->settings['inject'] ?? [])
->searchIndex($model->settings['search_index'] ?? null)
->revisionsEnabled($model->settings['revisions'] ?? false)
->defaultPublishState($model->settings['default_status'] ?? true)
->originBehavior($model->settings['origin_behavior'] ?? 'select')
->structureContents($model->settings['structure'] ?? null)
->sortField($model->settings['sort_field'] ?? null)
->sortDirection($model->settings['sort_dir'] ?? null)
->taxonomies($model->settings['taxonomies'] ?? null)
->propagate($model->settings['propagate'] ?? null)
->futureDateBehavior($model->settings['future_date_behavior'] ?? null)
->pastDateBehavior($model->settings['past_date_behavior'] ?? null)
->previewTargets($model->settings['preview_targets'] ?? [])
->entryClass($model->settings['entry_class'] ?? null)
->handle($model->handle)
->model($model);
}
public function toModel()
{
return self::makeModelFromContract($this);
}
public static function makeModelFromContract(Contract $source)
{
$class = app('statamic.eloquent.collections.model');
$model = $class::firstOrNew(['handle' => $source->handle])->fill([
'title' => $source->title ?? $source->handle,
'settings' => [],
]);
$model->settings = array_merge($model->settings ?? [], [
'routes' => $source->routes,
'slugs' => $source->requiresSlugs(),
'title_formats' => collect($source->titleFormats())->filter(),
'mount' => $source->mount,
'dated' => $source->dated,
'sites' => $source->sites,
'template' => $source->template,
'layout' => $source->layout,
'inject' => $source->cascade,
'search_index' => $source->searchIndex,
'revisions' => $source->revisionsEnabled(),
'default_status' => $source->defaultPublishState,
'structure' => $source->structureContents(),
'sort_dir' => $source->customSortDirection(),
'sort_field' => $source->customSortField(),
'taxonomies' => $source->taxonomies,
'propagate' => $source->propagate(),
'past_date_behavior' => $source->pastDateBehavior(),
'future_date_behavior' => $source->futureDateBehavior(),
'preview_targets' => $source->previewTargets(),
'origin_behavior' => $source->originBehavior(),
'entry_class' => $source->entryClass(),
]);
return $model;
}
public function model($model = null)
{
if (func_num_args() === 0) {
return $this->model;
}
$this->model = $model;
if (! is_null($model)) {
$this->id($model->id);
}
return $this;
}
protected function makeStructureFromContents()
{
return (new CollectionStructure)
->handle($this->handle())
->expectsRoot($this->structureContents['root'] ?? false)
->showSlugs($this->structureContents['slugs'] ?? false)
->maxDepth($this->structureContents['max_depth'] ?? null);
}
}