← All changes
|
backend/src/Services/FieldOptions/FieldOptionsService.php
+93
-0
0.6.1
→
1.5
View file →
| @@ -1,0 +1,93 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace IvyForms\Services\FieldOptions; | |
| 4 | + | |
| 5 | +use IvyForms\Common\Exceptions\InvalidArgumentException; | |
| 6 | +use IvyForms\Repository\FieldOptions\FieldOptionsRepositoryInterface; | |
| 7 | +use IvyForms\Entity\FieldOptions\FieldOptions; | |
| 8 | + | |
| 9 | +class FieldOptionsService | |
| 10 | +{ | |
| 11 | + /** | |
| 12 | + * @var FieldOptionsRepositoryInterface | |
| 13 | + */ | |
| 14 | + private FieldOptionsRepositoryInterface $fieldOptionsRepository; | |
| 15 | + | |
| 16 | + /** | |
| 17 | + * @param FieldOptionsRepositoryInterface $fieldOptionsRepository | |
| 18 | + */ | |
| 19 | + public function __construct(FieldOptionsRepositoryInterface $fieldOptionsRepository) | |
| 20 | + { | |
| 21 | + $this->fieldOptionsRepository = $fieldOptionsRepository; | |
| 22 | + } | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * @param FieldOptions $option | |
| 26 | + * @return int | |
| 27 | + */ | |
| 28 | + public function addOption(FieldOptions $option): int | |
| 29 | + { | |
| 30 | + return $this->fieldOptionsRepository->add($option); | |
| 31 | + } | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * Batch add FieldOptions entities. | |
| 35 | + * @param FieldOptions[] $options | |
| 36 | + * @return array<int> Inserted IDs | |
| 37 | + */ | |
| 38 | + public function addOptions(array $options): array | |
| 39 | + { | |
| 40 | + return $this->fieldOptionsRepository->addMany($options); | |
| 41 | + } | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * @param FieldOptions $option | |
| 45 | + * @return bool | |
| 46 | + */ | |
| 47 | + public function updateOption(FieldOptions $option): bool | |
| 48 | + { | |
| 49 | + return $this->fieldOptionsRepository->update($option->getId(), $option); | |
| 50 | + } | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * Batch update FieldOptions entities. | |
| 54 | + * @param FieldOptions[] $options | |
| 55 | + * @return int Number of updated rows | |
| 56 | + */ | |
| 57 | + public function updateOptions(array $options): int | |
| 58 | + { | |
| 59 | + return $this->fieldOptionsRepository->updateMany($options); | |
| 60 | + } | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * @param int $fieldId | |
| 64 | + * @return bool | |
| 65 | + */ | |
| 66 | + public function deleteOptionsByFieldId(int $fieldId): bool | |
| 67 | + { | |
| 68 | + return $this->fieldOptionsRepository->deleteByFieldId($fieldId); | |
| 69 | + } | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * Delete all options for multiple field IDs in one query. | |
| 73 | + * @param int[] $fieldIds | |
| 74 | + * @return int Number of deleted options | |
| 75 | + * @throws InvalidArgumentException | |
| 76 | + */ | |
| 77 | + public function deleteOptionsByFieldIds(array $fieldIds): int | |
| 78 | + { | |
| 79 | + if (empty($fieldIds)) { | |
| 80 | + return 0; | |
| 81 | + } | |
| 82 | + return $this->fieldOptionsRepository->deleteByFieldIds($fieldIds); | |
| 83 | + } | |
| 84 | + | |
| 85 | + /** | |
| 86 | + * @param int $fieldId | |
| 87 | + * @return array<FieldOptions> | |
| 88 | + */ | |
| 89 | + public function getByFieldId(int $fieldId): array | |
| 90 | + { | |
| 91 | + return $this->fieldOptionsRepository->getByFieldId($fieldId); | |
| 92 | + } | |
| 93 | +} | |