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