5
5
namespace Kami \Cocktail \External \Model ;
6
6
7
7
use Kami \Cocktail \External \SupportsCSV ;
8
+ use Kami \Cocktail \External \SupportsDraft2 ;
8
9
use Kami \Cocktail \Models \ComplexIngredient ;
9
10
use Kami \Cocktail \External \SupportsDataPack ;
10
11
use Kami \Cocktail \Models \Image as ImageModel ;
11
12
use Kami \Cocktail \Models \Ingredient as IngredientModel ;
12
13
use Kami \Cocktail \Models \IngredientPrice as IngredientPriceModel ;
13
14
14
- readonly class Ingredient implements SupportsDataPack, SupportsCSV
15
+ readonly class Ingredient implements SupportsDataPack, SupportsDraft2, SupportsCSV
15
16
{
16
17
/**
17
18
* @param array<Image> $images
18
- * @param array<IngredientBasic > $ingredientParts
19
+ * @param array<Ingredient > $ingredientParts
19
20
* @param array<IngredientPrice> $prices
20
21
*/
21
22
private function __construct (
@@ -47,7 +48,7 @@ public static function fromModel(IngredientModel $model, bool $useFileURI = fals
47
48
})->toArray ();
48
49
49
50
$ ingredientParts = $ model ->ingredientParts ->map (function (ComplexIngredient $ part ) {
50
- return IngredientBasic ::fromModel ($ part ->ingredient );
51
+ return Ingredient ::fromModel ($ part ->ingredient );
51
52
})->toArray ();
52
53
53
54
$ ingredientPrices = $ model ->prices ->map (function (IngredientPriceModel $ price ) {
@@ -85,7 +86,12 @@ public static function fromDataPackArray(array $sourceArray): self
85
86
86
87
$ ingredientParts = [];
87
88
foreach ($ sourceArray ['ingredient_parts ' ] ?? [] as $ ingredient ) {
88
- $ ingredientParts [] = IngredientBasic::fromDataPackArray ($ ingredient );
89
+ $ ingredientParts [] = Ingredient::fromDataPackArray ($ ingredient );
90
+ }
91
+
92
+ $ ingredientPrices = [];
93
+ foreach ($ sourceArray ['prices ' ] ?? [] as $ price ) {
94
+ $ ingredientPrices [] = IngredientPrice::fromDataPackArray ($ price );
89
95
}
90
96
91
97
return new self (
@@ -101,7 +107,7 @@ public static function fromDataPackArray(array $sourceArray): self
101
107
updatedAt: $ sourceArray ['updated_at ' ] ?? null ,
102
108
images: $ images ,
103
109
ingredientParts: $ ingredientParts ,
104
- prices: [] ,
110
+ prices: $ ingredientPrices ,
105
111
calculatorId: $ sourceArray ['calculator_id ' ] ?? null ,
106
112
sugarContent: $ sourceArray ['sugar_g_per_ml ' ] ?? null ,
107
113
acidity: $ sourceArray ['acidity ' ] ?? null ,
@@ -158,4 +164,28 @@ public static function fromCSV(array $sourceArray): self
158
164
units: blank ($ sourceArray ['units ' ]) ? null : $ sourceArray ['units ' ],
159
165
);
160
166
}
167
+
168
+ public static function fromDraft2Array (array $ sourceArray ): self
169
+ {
170
+ return new self (
171
+ id: $ sourceArray ['_id ' ],
172
+ name: $ sourceArray ['name ' ] ?? '' ,
173
+ strength: $ sourceArray ['strength ' ] ?? 0.0 ,
174
+ description: $ sourceArray ['description ' ] ?? null ,
175
+ origin: $ sourceArray ['origin ' ] ?? null ,
176
+ category: $ sourceArray ['category ' ] ?? null ,
177
+ );
178
+ }
179
+
180
+ public function toDraft2Array (): array
181
+ {
182
+ return [
183
+ '_id ' => $ this ->id ,
184
+ 'name ' => $ this ->name ,
185
+ 'strength ' => $ this ->strength ,
186
+ 'description ' => $ this ->description ,
187
+ 'origin ' => $ this ->origin ,
188
+ 'category ' => $ this ->category ,
189
+ ];
190
+ }
161
191
}
0 commit comments