|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace App\Src\UseCases\Infra\Sql; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use App\Src\UseCases\Domain\Context\Model\Characteristic; |
|
8
|
|
|
use App\Src\UseCases\Domain\Ports\CharacteristicsRepository; |
|
9
|
|
|
use App\Src\UseCases\Infra\Sql\Model\CharacteristicsModel; |
|
10
|
|
|
|
|
11
|
|
|
class CharacteristicsRepositorySql implements CharacteristicsRepository |
|
12
|
|
|
{ |
|
13
|
|
|
public function getByType(string $type, bool $isMain): array |
|
14
|
|
|
{ |
|
15
|
|
|
$list = CharacteristicsModel::query() |
|
16
|
|
|
->where('type', $type) |
|
17
|
|
|
->where('main', $isMain) |
|
18
|
|
|
->where('visible', true) |
|
19
|
|
|
->orderBy('priority') |
|
20
|
|
|
->get(); |
|
21
|
|
|
return $list->toArray(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function getAllByType(string $type): array |
|
25
|
|
|
{ |
|
26
|
|
|
$list = CharacteristicsModel::query() |
|
27
|
|
|
->where('type', $type) |
|
28
|
|
|
->orderBy('priority') |
|
29
|
|
|
->where('visible', true) |
|
30
|
|
|
->get(); |
|
31
|
|
|
return $list->toArray(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getByPageId(int $pageId):?Characteristic |
|
35
|
|
|
{ |
|
36
|
|
|
$c = CharacteristicsModel::query() |
|
37
|
|
|
->where('page_id', $pageId) |
|
38
|
|
|
->first(); |
|
39
|
|
|
if(!isset($c)){ |
|
40
|
|
|
return null; |
|
41
|
|
|
} |
|
42
|
|
|
return $c->toDomain(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function save(Characteristic $c) |
|
46
|
|
|
{ |
|
47
|
|
|
$memento = $c->memento(); |
|
48
|
|
|
$characteristicModel = new CharacteristicsModel(); |
|
49
|
|
|
$characteristicModel->page_label = $memento->title(); |
|
50
|
|
|
$characteristicModel->pretty_page_label = $memento->title(); |
|
51
|
|
|
$characteristicModel->main = false; |
|
52
|
|
|
$characteristicModel->priority = 100000; |
|
53
|
|
|
$characteristicModel->uuid = $memento->id(); |
|
54
|
|
|
$characteristicModel->code = $memento->title(); |
|
55
|
|
|
$characteristicModel->type = $memento->type(); |
|
56
|
|
|
$characteristicModel->visible = $memento->visible(); |
|
57
|
|
|
$characteristicModel->icon = $memento->icon(); |
|
58
|
|
|
$characteristicModel->page_id = $memento->pageId(); |
|
59
|
|
|
$characteristicModel->save(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getBy(array $conditions): ?Characteristic |
|
63
|
|
|
{ |
|
64
|
|
|
$characteristicModel = CharacteristicsModel::query() |
|
65
|
|
|
->when(isset($conditions['type']), function ($query) use($conditions){ |
|
66
|
|
|
$query->where('type', $conditions['type']); |
|
67
|
|
|
}) |
|
68
|
|
|
->when(isset($conditions['title']), function ($query) use($conditions){ |
|
69
|
|
|
$query->where('code', $conditions['title']); |
|
70
|
|
|
}) |
|
71
|
|
|
->first(); |
|
72
|
|
|
if(!isset($characteristicModel)){ |
|
73
|
|
|
return null; |
|
74
|
|
|
} |
|
75
|
|
|
return $characteristicModel->toDomain(); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|