id = uniqid(); $this->category = $category; $this->name = $name; $this->amount = $amount; $this->unit = $unit; $this->merged[] = clone $this; } public function isValid(): bool { return !empty($this->name); } public function getId(): string { return $this->id; } public function getCategory(): Category { return $this->category; } public function getName(): string { return $this->name; } public function getCleanName(): string { if (!empty($this->cleanName)) { return $this->cleanName; } $this->cleanName = Normalizer::normalize($this->name); return $this->cleanName; } public function setCleanName(string $cleanName): void { $this->cleanName = $cleanName; } public function getAmount(): string { return $this->amount; } public function setAmount(string $amount): void { $this->amount = $amount; } public function getUnit(): string { return $this->unit; } // debug info public function addMerged(Ingredient $ingredient) { $this->merged[] = $ingredient; } public function getMerged(): array { if (count($this->merged) === 1) { return []; } return $this->merged; } public function __toString(): string { $amount = $this->getAmount() ?: ''; $unit = $this->getUnit() ?: ''; $amountAndUnit = trim(sprintf('%s %s', $amount, $unit)); if (!empty($amountAndUnit)) { $amountAndUnit = ' ' . $amountAndUnit; } return $this->getName() . $amountAndUnit; } public function jsonSerialize() { return [ 'name' => $this->name, 'amount' => $this->amount, 'unit' => $this->unit, 'category' => $this->category, ]; } }