-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathMatcher.php
More file actions
128 lines (90 loc) · 3.88 KB
/
Matcher.php
File metadata and controls
128 lines (90 loc) · 3.88 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
declare(strict_types=1);
namespace Kami\Cocktail\External;
use Illuminate\Support\Facades\DB;
use Kami\Cocktail\Services\IngredientService;
use Kami\Cocktail\OpenAPI\Schemas\IngredientRequest as IngredientDTO;
class Matcher
{
/** @var array<string, int> */
private array $matchedIngredients = [];
/** @var array<string, int> */
private array $matchedCocktails = [];
/** @var array<string, int> */
private array $matchedGlasses = [];
/** @var array<string, int> */
private array $matchedMethods = [];
public function __construct(private readonly int $barId, private readonly IngredientService $ingredientService)
{
}
public function matchCocktailByName(string $name): ?int
{
$matchName = mb_strtolower($name, 'UTF-8');
if (isset($this->matchedCocktails[$matchName])) {
return $this->matchedCocktails[$matchName];
}
$this->matchedCocktails = DB::table('cocktails')->select('id', 'name')->where('bar_id', $this->barId)->get()->map(function ($row) {
$row->name = mb_strtolower((string) $row->name, 'UTF-8');
return $row;
})->pluck('id', 'name')->toArray();
$existingCocktail = $this->matchedCocktails[$matchName] ?? null;
if ($existingCocktail) {
$this->matchedCocktails[$matchName] = $existingCocktail;
return $existingCocktail;
}
return null;
}
public function matchOrCreateIngredientByName(IngredientDTO $ingredient): int
{
$matchName = mb_strtolower($ingredient->name, 'UTF-8');
if (isset($this->matchedIngredients[$matchName])) {
return $this->matchedIngredients[$matchName];
}
$this->matchedIngredients = DB::table('ingredients')->select('id', 'name')->where('bar_id', $this->barId)->get()->map(function ($row) {
$row->name = mb_strtolower((string) $row->name, 'UTF-8');
return $row;
})->pluck('id', 'name')->toArray();
$existingIngredient = $this->matchedIngredients[$matchName] ?? null;
if ($existingIngredient) {
$this->matchedIngredients[$matchName] = $existingIngredient;
return $existingIngredient;
}
$newIngredient = $this->ingredientService->createIngredient($ingredient);
$this->matchedIngredients[$matchName] = $newIngredient->id;
return $newIngredient->id;
}
public function matchGlassByName(string $name): ?int
{
$matchName = mb_strtolower($name, 'UTF-8');
if (isset($this->matchedGlasses[$matchName])) {
return $this->matchedGlasses[$matchName];
}
$this->matchedGlasses = DB::table('glasses')->select('id', 'name')->where('bar_id', $this->barId)->get()->map(function ($row) {
$row->name = mb_strtolower((string) $row->name, 'UTF-8');
return $row;
})->pluck('id', 'name')->toArray();
$existingGlass = $this->matchedGlasses[$matchName] ?? null;
if ($existingGlass) {
$this->matchedGlasses[$matchName] = $existingGlass;
return $existingGlass;
}
return null;
}
public function matchMethodByName(string $name): ?int
{
$matchName = mb_strtolower($name, 'UTF-8');
if (isset($this->matchedMethods[$matchName])) {
return $this->matchedMethods[$matchName];
}
$this->matchedMethods = DB::table('cocktail_methods')->select('id', 'name')->where('bar_id', $this->barId)->get()->map(function ($row) {
$row->name = mb_strtolower((string) $row->name, 'UTF-8');
return $row;
})->pluck('id', 'name')->toArray();
$existingMethod = $this->matchedMethods[$matchName] ?? null;
if ($existingMethod) {
$this->matchedMethods[$matchName] = $existingMethod;
return $existingMethod;
}
return null;
}
}