From a1b3a0cdde263487c7ffaec798a67786ea858a33 Mon Sep 17 00:00:00 2001 From: Timvde Date: Thu, 24 Oct 2019 11:28:53 +0200 Subject: [PATCH] Parse totalTime as input field --- lib/Service/RecipeService.php | 51 ++++++++++++++++++----------------- templates/content/edit.php | 5 ++++ templates/content/recipe.php | 8 ++++++ 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/lib/Service/RecipeService.php b/lib/Service/RecipeService.php index 0494e46f..9322a0c0 100644 --- a/lib/Service/RecipeService.php +++ b/lib/Service/RecipeService.php @@ -62,6 +62,29 @@ class RecipeService { return null; } + /** + * Validates that the json has a valid duration element in the given field, + * or nothing at all. + * + * @param array $json + * @param string $key + */ + private function validateDuration($json, $key) { + // Make sure we have a string and valid DateInterval + // regex validation from here: https://stackoverflow.com/a/32045167 + $interval_regex = "/^P(?!$)(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+S)?)?$/"; + if(isset($json[$key]) && is_string($json[$key])) { + $time_string = $this->cleanUpString($json[$key]); + if(preg_match_all($interval_regex, $time_string)) { + $json[$key] = $time_string; + } else { + $json[$key] = ""; + } + } else { + $json[$key] = ""; + } + } + /** * Checks the fields of a recipe and standardises the format * @@ -233,32 +256,10 @@ class RecipeService { } else { $json['url'] = ""; } - // Make sure 'prepTime' is a string and valid DateInterval - // regex validation from here: https://stackoverflow.com/a/32045167 - $interval_regex = "/^P(?!$)(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+S)?)?$/"; - if(isset($json['prepTime']) && is_string($json['prepTime'])) { - $prep_string = $this->cleanUpString($json['prepTime']); - if(preg_match_all($interval_regex, $prep_string)) { - $json['prepTime'] = $prep_string; - } else { - $json['prepTime'] = ""; - } - } else { - $json['prepTime'] = ""; - } - - // Make sure 'cookTime' is a string and valid DateInterval - if(isset($json['cookTime']) && is_string($json['cookTime'])) { - $cook_string = $this->cleanUpString($json['cookTime']); - if(preg_match_all($interval_regex, $cook_string)) { - $json['cookTime'] = $cook_string; - } else { - $json['cookTime'] = ""; - } - } else { - $json['cookTime'] = ""; - } + $this->validateDuration($json, 'prepTime'); + $this->validateDuration($json, 'cookTime'); + $this->validateDuration($json, 'totalTime'); return $json; } diff --git a/templates/content/edit.php b/templates/content/edit.php index 2b086cfd..0d841689 100644 --- a/templates/content/edit.php +++ b/templates/content/edit.php @@ -37,6 +37,11 @@ +
+ + +
+
diff --git a/templates/content/recipe.php b/templates/content/recipe.php index e71b24f2..f3f13d48 100644 --- a/templates/content/recipe.php +++ b/templates/content/recipe.php @@ -34,6 +34,14 @@

t('Cooking time')); ?>:

+ format('%i'); + $total_hours = $total_interval->format('%h'); + ?> +

t('Total time')); ?>:

+ +

t('Servings')); ?>: