Parse totalTime as input field

This commit is contained in:
Timvde 2019-10-24 11:28:53 +02:00
Родитель 9dcec58884
Коммит a1b3a0cdde
3 изменённых файлов: 39 добавлений и 25 удалений

Просмотреть файл

@ -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;
}

Просмотреть файл

@ -37,6 +37,11 @@
<input type="text" name="cookTime" value="<?php if(isset($_['cookTime'])) { echo $_['cookTime']; } ?>" placeholder="PT1H30M">
</fieldset>
<fieldset>
<label><?php p($l->t('Total Time')); ?></label>
<input type="text" name="totalTime" value="<?php if(isset($_['totalTime'])) { echo $_['totalTime']; } ?>" placeholder="PT1H30M">
</fieldset>
<fieldset>
<label><?php p($l->t('Keywords (comma-separated)')); ?></label>
<input type="text" name="keywords" value="<?php if(isset($_['keywords'])) { echo $_['keywords']; } ?>">

Просмотреть файл

@ -34,6 +34,14 @@
<p><strong><?php p($l->t('Cooking time')); ?>: </strong><?php echo $cook_hours . ':' . $cook_mins; ?></p>
<?php } ?>
<?php if(isset($_['totalTime']) && $_['totalTime']) {
$total_interval = new DateInterval($_['totalTime']);
$total_mins = $total_interval->format('%i');
$total_hours = $total_interval->format('%h');
?>
<p><strong><?php p($l->t('Total time')); ?>: </strong><?php echo $total_hours . ':' . $total_mins; ?></p>
<?php } ?>
<p><strong><?php p($l->t('Servings')); ?>: </strong><?php echo $_['recipeYield']; ?></p>
</div>
</header>