First version
这个提交包含在:
106
src/Models/Ingredient.php
普通文件
106
src/Models/Ingredient.php
普通文件
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
namespace CookidooShoppingAdvanced\Models;
|
||||
|
||||
use CookidooShoppingAdvanced\Normalizer;
|
||||
|
||||
class Ingredient implements \JsonSerializable {
|
||||
private string $id;
|
||||
private Category $category;
|
||||
private string $name;
|
||||
private string $amount;
|
||||
private string $unit;
|
||||
|
||||
private string $cleanName = '';
|
||||
private array $merged = [];
|
||||
|
||||
public function __construct(
|
||||
Category $category,
|
||||
string $name,
|
||||
string $amount = '',
|
||||
string $unit = '')
|
||||
{
|
||||
$this->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,
|
||||
];
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户