|
- <?php
-
- namespace App\Logic\Contract;
-
- use App\Models\Contract;
- use LogicException;
-
- abstract class ContractManager
- {
-
- // ------------MEMBER----
- protected Contract $contract;
- protected bool $initialized = false;
-
-
- // -------------PUBLIC--------
- public function initById(string $id): static
- {
- $contract = Contract::findOrFail($id);
- $this->contract = $contract;
- $this->initialized = true;
- return $this;
- }
-
- public function initForCreate(): static
- {
- $contract = new Contract();
- $contract->setId();
-
- $this->contract = $contract;
- $this->initialized = true;
- return $this;
- }
-
- public function fill(array $attr): static
- {
- if (!$this->initialized) {
- throw new LogicException("初期化ミス");
- }
-
- $this->contract->fill($attr);
- return $this;
- }
-
- public function id(): string
- {
- if (!$this->initialized) {
- throw new LogicException("初期化ミス");
- }
- return $this->contract->id;
- }
-
-
- // ----------------PROTECTED----------
- protected function save(): static
- {
- $this->contract->save();
- return $this;
- }
-
- /**
- * 契約識別子がUNIQUE判定する
- *
- * @param array $messages
- * @param boolean $forUpdate
- * @return boolean
- */
- protected function checkContractCode(array &$messages, bool $forUpdate): bool
- {
- $contractCode = $this->contract->contract_code;
-
- if ($contractCode === null) {
- return true;
- }
-
- $query = Contract::whereContractCode($contractCode);
- if ($forUpdate) {
- $query->whereNot(Contract::COL_NAME_ID, $this->contract->id);
- }
-
- if ($query->exists()) {
- $messages[Contract::COL_NAME_CONTRACT_CODE] = "すでに使われています";
- return false;
- }
- return true;
- }
- }
|