repository = $repository; } /** * @return string */ public function getId(): string { return 'date'; } /** * @param InvoiceModel $model */ public function setModel(InvoiceModel $model) { $this->model = $model; } /** * @return string */ public function getInvoiceNumber(): string { $loops = 0; $increaseBy = 0; $result = date('ymd', $this->model->getInvoiceDate()->getTimestamp()); // in the case that someone configured a weird format, that should not result in an endless loop while ($this->repository->hasInvoice($result) && $loops++ < 99) { $suffix = str_pad((string) ++$increaseBy, 2, '0', STR_PAD_LEFT); $result = date('ymd', $this->model->getInvoiceDate()->getTimestamp()) . '-' . $suffix; } return $result; } }