<?php
namespace Cookpedia;
class Entry {
private $hydrated = false;
private $data = [
'id' => 0,
'title' => null,
'latin_title' => null,
'category' => 0,
'calories' => 0,
'proteins' => 0,
'fats' => 0,
'carbohydrates' => 0,
'description' => null,
'tags' => null,
'measures' => [],
];
public function hydrate(PDO $dbh, int $id): void
{
$stmt = $dbh->prepare("SELECT `title`, `latin_title`, `category`, `calories`, `proteins`, `fats`, `carbohydrates`, `description`, `measures`, `tags` FROM `cook_encyclopedia_entries` WHERE `id`=:id");
$result = $stmt->execute($id);
$this->data = $result->fetch(PDO::FETCH_ASSOC);
$this->data['measures'] = $this->computeMeasures($this->data['measures']);
$this->hydrated = true;
}
public function getData(): array
{
if (!$this->hydrated) {
throw new \Exception('hydrate first');
}
return $this->data;
}
private function computeMeasures(string $measures): array
{
$measureArray = explode('|', $measures);
return [
'g_in_tsp' => $measureArray[0],
'g_in_tbsp' => $measureArray[1],
'g_in_glass' => $measureArray[2],
'g_in_cup' => $measureArray[3],
];
}
}
- Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
preferences:
202.13 ms | 403 KiB | 212 Q