Updated importers, exporters, PHP 8.x
This commit is contained in:
45
src/Ingredient/Importer/JsonImporter.php
Normal file
45
src/Ingredient/Importer/JsonImporter.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\Juicer\Ingredient\Importer;
|
||||
|
||||
use NoccyLabs\Juicer\Ingredient\Ingredient;
|
||||
use NoccyLabs\Juicer\Ingredient\IngredientInterface;
|
||||
|
||||
class JsonImporter
|
||||
{
|
||||
/**
|
||||
* Import ingredient from json
|
||||
*
|
||||
* @param string The json string to parse and import
|
||||
* @return IngredientInterface
|
||||
*/
|
||||
public function import(string $json): IngredientInterface
|
||||
{
|
||||
$data = json_decode($json);
|
||||
|
||||
$ingredient = new Ingredient($data->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);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user