From 104856f4259950f35e15e1410d07fb4a32fc0c10 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Mon, 1 Mar 2021 14:41:21 +0100 Subject: [PATCH] Updated importers, exporters, PHP 8.x --- composer.json | 2 +- doc/json-schemas.md | 77 +++++++++++++++++++++++ examples/bases.php | 6 +- src/Ingredient/Importer/JsonImporter.php | 45 +++++++++++++ src/Recipe/Exporter/XmlExporter.php | 0 src/Recipe/Importer/ImporterInterface.php | 15 +++++ src/Recipe/Importer/JsonImporter.php | 4 +- src/Recipe/Importer/XmlImporter.php | 57 +++++++++++++++++ 8 files changed, 200 insertions(+), 6 deletions(-) create mode 100644 doc/json-schemas.md create mode 100644 src/Ingredient/Importer/JsonImporter.php create mode 100644 src/Recipe/Exporter/XmlExporter.php create mode 100644 src/Recipe/Importer/ImporterInterface.php create mode 100644 src/Recipe/Importer/XmlImporter.php diff --git a/composer.json b/composer.json index feaee42..a9483a1 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,6 @@ } }, "require": { - "php": "~7.0" + "php": "~7.0|~8.0" } } diff --git a/doc/json-schemas.md b/doc/json-schemas.md new file mode 100644 index 0000000..095db91 --- /dev/null +++ b/doc/json-schemas.md @@ -0,0 +1,77 @@ +# Json Schemas + +Juicer comes with a number of schemas to validate data. + +## Definitions + +**Flavoring Key**: + A combination of the BrandKey and a unique combination of letters and + optional version info. Ex. `FW-VBIC` for FlavorWest Vanilla Bean Ice + Cream, or `TFA-MSHM-DX` for TFA DX Marshmallow. Max length is 32 chars. + +## Conventions + +* Flavoring keys always start with a vendor prefix of 2-4 characters. +* Variation of a flavor are appended, such as `DX` or `V2` +* Flavoring names should follow existing use as far as possible, while + making it easy to associate similar flavorings from various vendors. + +Exammples: + + FW-VBIC FlavorWest Vanilla Bean Ice Cream + TFA-VBIC The Flavor Artisan Vanilla Bean Ice Cream + CAP-VCUS Capella Vanilla Custard + CAP-VCUS-V2 Capella Vanilla Custard V2 + TFA-MSHM The Flavor Artisan Marshmallow + TFA-MSHM-DX The Flavor Artisan Marshmallow DX + FA-FUJA FlavourArt Fuji Apple + +## ingredient.schema.json + +Defines an ingredient. + + { + "flavoringKey": "TFA-WCHO", + "brandKey": "TFA", + "brandName": "The Flavor Apprentice", + "flavoringName": "White Chocolate", + "ingredients": [ + "Ethyl Alcohol", + "Vanillin", + "Acetoin" + ], + "attributes": { + "suggestedPercent": 2, + "suggestedSteep": 7, + "specificGravity": 1.004, + "base": "PG100", + "isAdditive": false + }, + "flavorProfiles": [ + "White Chocolate" + ], + "warnings": [], + "notes": [] + } + +## recipe.schema.json + +Defines a recipe + + { + "url": "https://vape.noccy.com/recipe/tasty", + "recipeName": "Tasty", + "author": "Noccy", + "recommendedSteep": 0, + "ingredients": [ + { + "flavor": "FW Vanilla Bean Ice Cream", + "percent": 1.5 + }, + { + "flavor": "FW Real Lemonade", + "percent": 1 + } + ] + } + diff --git a/examples/bases.php b/examples/bases.php index 77f8e8f..618cb15 100644 --- a/examples/bases.php +++ b/examples/bases.php @@ -8,20 +8,20 @@ use NoccyLabs\Juicer\Ingredient\NicotineBase; printf("Apparent Specific Gravity of base mixes:\n\n"); printf(" VG | PG | ASG\n"); printf(" ------|------|------------------------\n"); -for ($pg = 0; $pg < 100; $pg += 10) { +for ($pg = 0; $pg <= 100; $pg += 10) { $base = new Base("PG{$pg}"); printf(" %3d%% | %3d%% | %.4fg/mL\n", 100-$pg, $pg, $base->getSpecificGravity()); } echo "\n\n"; -$asgstrength = [ 24, 20, 18, 15 ]; +$asgstrength = [ 24, 20, 18, 15, 0 ]; $asgheader = join("", array_map(function ($s) { return sprintf("%2dmg ", $s); } , $asgstrength)); printf("Apparent Specific Gravity of nicotine bases:\n\n"); printf(" VG | PG | %s\n", $asgheader); printf(" ------|------|-------------------------------------\n"); -for ($pg = 0; $pg < 100; $pg += 10) { +for ($pg = 0; $pg <= 100; $pg += 10) { printf(" %3d%% | %3d%% |", 100-$pg, $pg); $base = new Base("PG{$pg}"); foreach($asgstrength as $s) { diff --git a/src/Ingredient/Importer/JsonImporter.php b/src/Ingredient/Importer/JsonImporter.php new file mode 100644 index 0000000..364b529 --- /dev/null +++ b/src/Ingredient/Importer/JsonImporter.php @@ -0,0 +1,45 @@ +flavoringName, $data->brandKey); + + print_r($data); + + return $ingredient; + } + + /** + * Import ingredient from json contained in a file + * + * @param string The filename to read and import + * @return IngredientInterface + */ + public function readFromFile(string $filename): IngredientInterface + { + $fd = fopen($filename, "r"); + if (!$fd) { + throw new \InvalidArgumentException(); + } + $json = fread($fd, filesize($filename)); + fclose($fd); + + return $this->import($json); + } + +} \ No newline at end of file diff --git a/src/Recipe/Exporter/XmlExporter.php b/src/Recipe/Exporter/XmlExporter.php new file mode 100644 index 0000000..e69de29 diff --git a/src/Recipe/Importer/ImporterInterface.php b/src/Recipe/Importer/ImporterInterface.php new file mode 100644 index 0000000..61b6455 --- /dev/null +++ b/src/Recipe/Importer/ImporterInterface.php @@ -0,0 +1,15 @@ +import($json); } -} \ No newline at end of file +} diff --git a/src/Recipe/Importer/XmlImporter.php b/src/Recipe/Importer/XmlImporter.php new file mode 100644 index 0000000..78c7b3a --- /dev/null +++ b/src/Recipe/Importer/XmlImporter.php @@ -0,0 +1,57 @@ +setRecipeName((string)$data->recipe); + $recipe->setRecipeAuthor((string)$data->author); + $recipe->setDescription((string)$data->description); + + foreach ((array)@$data->ingredients->ingredient as $ingredientData) { + $ingredient = new Ingredient($ingredientData->flavor, $ingredientData->brand, $ingredientData->percent); + $recipe->addIngredient($ingredient); + } + + return $recipe; + } + + /** + * Import a recipe from json contained in a file + * + * @param string The filename to read and import + * @return RecipeInterface + */ + public function readFromFile(string $filename): RecipeInterface + { + $fd = fopen($filename, "r"); + if (!$fd) { + throw new \InvalidArgumentException(); + } + $xml = fread($fd, filesize($filename)); + fclose($fd); + + return $this->import($xml); + } + +}