-
Notifications
You must be signed in to change notification settings - Fork 554
/
BlockRepository.php
108 lines (86 loc) · 3.51 KB
/
BlockRepository.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
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
<?php
namespace A17\Twill\Repositories;
use A17\Twill\Facades\TwillBlocks;
use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Repositories\Behaviors\HandleFiles;
use A17\Twill\Repositories\Behaviors\HandleMedias;
use A17\Twill\Services\Blocks\Block as BlockConfig;
use Illuminate\Config\Repository as Config;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use ReflectionException;
class BlockRepository extends ModuleRepository
{
use HandleMedias;
use HandleFiles;
protected Config $config;
public function __construct(Config $config)
{
$blockModel = twillModel('block');
$this->model = new $blockModel();
$this->config = $config;
}
public function getCrops(string $role): array
{
return TwillBlocks::getAllCropConfigs()[$role];
}
public function hydrate(TwillModelContract $model, array $fields): TwillModelContract
{
if (Schema::hasTable(config('twill.related_table', 'twill_related'))) {
$relatedItems = Collection::make();
Collection::make($fields['browsers'])->each(function ($items, $browserName) use (&$relatedItems) {
Collection::make($items)->each(function ($item) use ($browserName, &$relatedItems) {
try {
// @todo: Repository could be null.
$repository = $this->getModelRepository($item['endpointType'] ?? $browserName);
$relatedItems->push(
(object) [
'related' => $repository->getById($item['id']),
'browser_name' => $browserName,
]
);
} catch (ReflectionException $reflectionException) {
Log::error($reflectionException);
}
});
});
$model->setRelation('relatedItems', $relatedItems);
}
return parent::hydrate($model, $fields);
}
public function afterSave(TwillModelContract $model, array $fields): void
{
if (Schema::hasTable(config('twill.related_table', 'twill_related'))) {
$model->clearAllRelated();
if (isset($fields['browsers'])) {
Collection::make($fields['browsers'])->each(function ($items, $browserName) use ($model) {
$model->saveRelated($items, $browserName);
});
}
}
parent::afterSave($model, $fields);
}
public function afterDelete(TwillModelContract $object): void
{
$object->medias()->sync([]);
$object->files()->sync([]);
if (Schema::hasTable(config('twill.related_table', 'twill_related'))) {
$object->clearAllRelated();
}
}
public function buildFromCmsArray(array $block, bool $repeater = false): array
{
$blockInstance = BlockConfig::getForComponent($block['type'], $repeater);
$block['type'] = $blockInstance->name;
$block['instance'] = $blockInstance;
$block['content'] = empty($block['content']) ? new \stdClass() : (object) $block['content'];
if ($block['browsers'] ?? null) {
$browsers = Collection::make($block['browsers'])->map(function ($items) {
return Collection::make($items)->pluck('id');
})->toArray();
$block['content']->browsers = $browsers;
}
return $block;
}
}